
リクエストの URL を変更したいのですが、ブラウザのアドレスは変更してはなりません。これを実現するために、以下の構成を試しました。
location /my-website {
proxy_pass http://tomcat/my-website;
}
location =/my-website {
rewrite /my-website$(.*) $1/my-website/mypage/index.html last;
}
これを実行すると、リクエストは正しいアドレスを取得しますが、ブラウザのアドレス バーも変更されます。
また試しました;
location /my-website {
proxy_pass http://tomcat;
rewrite /my-website$(.*) $1/my-website/page/index.html break;
}
この構成を改善するための提案はありますか?
期待される出力
アドレスバー:プロトコル://localhost/my-website
実際の:プロトコル://localhost/my-website/page
電流出力
アドレスバー:プロトコル://localhost/my-website/page
実際の:プロトコル://localhost/my-website/page
試したこと:
- https://www.claudiokuenzler.com/blog/436/nginx-rewrite-url-examples-with-without-redirect-address#.W3_a6M4zaUk
- https://stackoverflow.com/questions/15322826/nginx-rewrite-without-change-url
編集
上記の問題は、302 リダイレクトで発生します。その他のケースでは、ブラウザ アドレスを変更せずに URL が変更されます。私は、次の設定を使用して後者のケースを処理しています。
location /my-website {
proxy_pass http://tomcat;
rewrite ^(.*)my-website/src(.*)$ $1my-website/page/src$2 break;
}
つまり、その場所の後に /src が続くと機能します。
302 の場合、場所は my-website/ だけなので、上記の試行は失敗します。
私のファイル構成:
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'
'"$upstream_http_location"';
rewrite_log on;
#log_format graylog2_format '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for" <msec=$msec|connection=$connection|connection_requests=$connection_requests|millis=$request_time>';
error_log logs/error.log warn;
sendfile on;
keepalive_timeout 65;
map $http_user_agent $ua_redirect {
default 'abc';
}
upstream docker-mysite {
server localhost:9012;
}
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-Host $server_name;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_read_timeout 900;
client_max_body_size 0;
server {
listen 80;
access_log logs/host.access.log main;
#below config works
location /mysite {
proxy_pass http://docker-mysite;
rewrite ^(.*)mysite/src(.*)$ $1mysite/$ua_redirect/src$2 break;
}
#below config works but modifies the browser url hence the issue
location = /mysite {
proxy_pass http://docker-mysite;
rewrite /mysite(.*)$ /mysite/$ua_redirect$1 break;
}
}
}
ログ
127.0.0.1 - - [03/Sep/2018:11:46:07 +0500] "GET /mysite/login?code=token HTTP/1.1" 302 0 "http://localhost/loginapp/web/index.html" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36" "-"http://localhost/mysite/abc
127.0.0.1 - - [03/Sep/2018:11:46:07 +0500] "GET /mysite/abc HTTP/1.1" 404 0 "http://localhost/loginapp/web/index.html" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36" "-"-
答え1
通常のシナリオでは正しく動作しているので、書き換えロジックは問題ありません。rewrite
ディレクティブはリクエストの応答ではなくリクエスト URI のみを変更するため、302 リダイレクトではブラウザ URL が変更されます。
典型的な 302 応答は次のようになります。
HTTP/1.1 302 Found
Location: http://overrideurlrewriting.com
Location
302 応答のヘッダーにより、ブラウザは指定された URL に従うようになります。
Location
可能な解決策は、プロキシされたレスポンス(302リダイレクトの場合)のヘッダーをproxy_redirect
ディレクティブを使用して変更することです。
便利なリンク:
https://stackoverflow.com/a/26025618/2073920
http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_redirect
答え2
これを試して:
location /my-website {
proxy_pass http://tomcat;
rewrite /my-website(.*)$ $1/my-website/page/index.html break;
}
正規表現に が不適切な位置にあると思います$
。 正規表現の最後にあるべきです。 あなたの意図が正確にはわかりませんが、 以外のページがindex.html
機能するように、書き換えターゲットを変更することもできます。
location /my-website {
proxy_pass http://tomcat:8000;
rewrite /my-website(.*)$ /my-website/page$1 break;
}