それマグで!

知識はカップより、マグでゆっくり頂きます。 takuya_1stのブログ

習慣に早くから配慮した者は、 おそらく人生の実りも大きい。

nginx で proxyPassReverse的なのを実現する。

apache のproxyPassReverseって便利だよね。

とくに静的ページを作るのに便利。記事公開するだけのSPAなんぞいらねーよね。ってとき便利

でもnginx で出来ない。→ 置換すればできるよ。

URIディレクトリを修正する方法の例

プロキシリクエストの結果を書き換えられるので、それを仕込んで見る。

   location ~/blog/(.*) {

      # rewrite ^ $1 break;
     proxy_set_header Host $host;
     proxy_set_header X-Real-IP $remote_addr;
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
     proxy_set_header X-Forwarded-Proto $scheme;

     set $nocache 0;

     if ($arg_filter_distance = "") {
         set $nocache 1;
     }
     proxy_cache_bypass $nocache;
     proxy_no_cache $nocache;


     proxy_pass http://127.0.0.1:1234/$1;


     sub_filter '/static'  "${uri}static";
     sub_filter '/task'  "${uri}task/";
     sub_filter_once off;
   }

ポイント

ここで、proxy 取得したHTMLのコンテンツを「書き換え」ている。

     sub_filter '/static'  "${uri}static";
     sub_filter '/task'  "${uri}task/";

ただし、JSにかかれてJSでLocationを管理されると手出しができないので注意。

どういうときに使えるのか

サブディレクトリに非対応なWebアプリケーションを無理やりサブディレクトリで展開するのに便利。

サブディレクトリにしたほうが便利なアプリケーションがある。たとえば、複数のブログとか。たとえば、JiraとConfulenceとか。