1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Nginxのリバプロでproxypassreverseの設定

Last updated at Posted at 2023-06-10

目的

Nginxでリバプロを構築して、設定する時に少し苦労した。コンテキストパスの設定を行わないと、CSS等が適用されていない様な文字だけのHTMLが表示されてしまう。
今回は、プロキシ遷移先のアプリケーション側で、特にプロキシの設定をする事無く、リバプロが機能するようする。

原因

アプリケーションの手前にリバプロを設置した場合に、URLで振り分けを行っている事を前提とする。
例えば、/appにアクセスすると、app.comに遷移。/webにアクセスすると、web.comに遷移する場合を考える。
リダイレクト等が走るとアプリケーションサーバーは/redirectへと遷移させようとするが、この構成の場合は、/app/redirectへと遷移させてほしい。

この様な場合は本来アプリ側が相対パスで指定してくれてるか、プロキシの設定を入れる事で解決する。
今回の場合はアプリ側はいじらないとするので、上記の解決法は使えない。

TL;DL

nginx.conf
events {
    worker_connections  16;
}

http {
    server {
        listen 80;
        server_name localhost;

        location /reveal-docker/ {
            proxy_pass http://reveal-docker:8000/;
            proxy_redirect off;

            set $context "/reveal-docker/";

            proxy_set_header Upgrade             $http_upgrade;
            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;
            proxy_set_header X-Forwarded-Host    $http_host;
            proxy_set_header X-Forwarded-Port    $server_port;
            proxy_set_header Accept-Encoding     "";

            sub_filter 'href="/'      'href="$context';
            sub_filter 'src="/'       'src="$context';
            sub_filter 'url("/'       'url("$context';
            sub_filter 'action="/'    'action="$context';
            sub_filter 'href=\'/'     'href=\'$context';
            sub_filter 'src=\'/'      'src=\'$context';
            sub_filter 'url(\'/'      'url(\'$context';
            sub_filter 'action=\'/'   'action=\'$context';
            sub_filter_once off;

            gzip on;
            gzip_vary on;
            gzip_min_length 1000;
            gzip_types text/css text/javascript application/javascript application/xml application/json;
        }
    }
}

入力箇所

location

 location /reveal-docker/ {

サブディレクトリ

URIの指定を以下で行う

set $context "/labweb/";

遷移先の指定

遷移先のIPアドレスやドメインを指定。今回の場合はコンテナ名を入力した

proxy_pass http://reveal-docker:8000/;
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?