自分のブログから転載。
https://blog.haniokasai.com/2016/11/nginx.html
jenkinsサーバーを運用していたのですが、広告をいれてみたいなぁと思い?手法を検討。
リバースプロキシで広告を埋め込む方法を探しました。
http_sub_moduleで置き換えられるらしいのですが、わざわざmake時にconfigureしてあげるのも嫌だと思います。
調べてみると、nginx-fullには、ngx_http_substitutions_filter_moduleが入っていて、これを使えばいけました。
apt install nginx-full
んで、/etc/nginx/conf.dにリバースプロキシーの設定を書く。
server {
listen 80;
server_name jenkins.haniokasai.com;
location / {
proxy_pass http://localhost:8080;
proxy_set_header Accept-Encoding "";//gzipがなんとからしい
subs_filter_types text/html; //対象のデータ形式を決める
subs_filter '</header>' '</header><script async src=\"//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js\"></script><ins class=\"adsbygoogle\" style=\"display:block\" data-ad-client=\"かきかえて\" data-ad-slot=\"かきかえて\" data-ad-format=\"auto\"></ins><script>(adsbygoogle = window.adsbygoogle || []).push({});</script>';
}
}
これで、nginxをリスタートをすると表示されるでしょう