问题原文
原帖链接 · Geo C. · CC BY-SA 3.0
Every time I try foobar.com/test in browser, nginx seems to redirect (301) the client to foobar.com/test/. This behaviour is unacceptable. The proxied server is a remote Apache web server. I have tried direct calls to the Apache server (without a proxy), and it does not redirect the client.
Considering the nginx server configuration below, any idea how should I solve this?
server {
listen 80;
server_name fooBar.com;
location /test {
proxy_redirect off;
proxy_pass http://1.1.1.1:80/;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Host $http_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;
}
location /test/ {
proxy_redirect off;
proxy_pass http://1.1.1.1:80/;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Host $http_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;
}
}
已采纳解答
原帖链接 · famousgarkin · CC BY-SA 3.0
There is a special kind of processing for this scenario, as per the docs:
If a location is defined by a prefix string that ends with the slash character, and requests are processed by one of proxy_pass, fastcgi_pass, uwsgi_pass, scgi_pass, or memcached_pass, then the special processing is performed. In response to a request with URI equal to this string, but without the trailing slash, a permanent redirect with the code 301 will be returned to the requested URI with the slash appended. If this is not desired, an exact match of the URI and location could be defined like this:
location /user/ { proxy_pass http://user.example.com; } location = /user { proxy_pass http://login.example.com; }(http://nginx.org/en/docs/http/ngx_http_core_module.html#location)
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
