Nginx location 斜線問題

        今天遇到一個Nginx location 301問題,比如我配置如下:

#前端通過反向代理,把80端口映射到後端8000端口
server{
    listen 8000;
    server_name domain;
    
    location /test {
        rewrite ^/test/(.*)$ /test/index.php/$1;
    }
    
    ……
}

當訪問http://domain/test/的時候一切正常,但是在訪問http://domain/test的時候卻返回301跳轉,指向的地址http://domain:8000/test/。


這裏有兩個問題,第一,爲什麼跳轉;第二,爲什麼給我加8000,第一個問題,看官網文檔就可以知道

If a location is defined by a prefix string that ends with the slash character, and requests are processed by one of proxy_passfastcgi_passuwsgi_passscgi_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;
}

原因就是Nginx判斷所訪問的是一個目錄,就會進行301跳轉加上斜線,但他的方案針對*_pass等,但對於第二個問題還是沒解決,我們默認的端口就是80端口,那返回的時候不加端口號不久行了嗎?是的,沒錯

port_in_redirect on | off;

Enables or disables specifying the port in redirects issued by nginx.

The use of the primary server name in redirects is controlled by the server_name_in_redirectdirective.

默認是開啓的,也就是默認會帶port返回,如果置爲off就不會返回來,這樣問題就因人而結了,反問沒有斜線的地址會自動加上斜線通過301跳轉。


       對於地址不是80端口的大家可以自行測試,歡迎反饋!



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章