
我想更改請求的 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/我的網站
實際的:協議://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
您的重寫邏輯很好,因為它在正常情況下可以正常工作。 302 重定向的瀏覽器 URL 正在更改,因為rewrite
指令僅更改請求 URI,而不更改請求的回應。
典型的 302 響應如下所示:
HTTP/1.1 302 Found
Location: http://overrideurlrewriting.com
Location
302 回應中的 header 強制瀏覽器遵循提到的 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;
}