nginx下URL末尾自動加斜杠


服務器上用的nginx,在地址欄輸入網址,例如www.xxx.com.cn/aaa后會跳到localhost/aaa

而如果輸入的是www.xxx.com.cn/aaa/則正常

一直找不到原因,今天抽時間查了查終於找到解決方案了

其實就是加一條配置

server {

    listen 80;

}

改為

server {

    listen 80;

    server_name_in_redirect off;

}

即可,這時再訪問www.xxx.com.cn/aaa即可正常訪問

 

PS. 好像這種問題只會在老版本的nginx里出現,0.8.48版本之后的似乎已經變為默認配置了,未經驗證
————————————————
版權聲明:本文為CSDN博主「geagerg」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/geagerg/article/details/46480779

 

 

 

一、首先對比說明Nginx以下兩個現象:

1. 訪問的uri最后帶斜杠

  http://localhost/product/    >>>>  查找 product下的index頁面,存在就返回;不存在且未開啟自動索引目錄選項(指令是:autoindex on),則報403錯誤

2. 訪問的uri最后不帶斜杠

  http://localhost/product     >>>>  查找product文件,存在就返回;若存在product的文件夾,會產生301跳轉,且自動將uri補全為http://localhost/product/ ,跳轉的域名由server_name_in_redirect 指令控制(下文介紹)

二、server_name_in_redirect 指令配置

1、server_name_in_redirect on ,URL 重定向為: server_name 中的第一個域名 + 目錄名 + /;例如 配置文件中 server_name  www.baidu.com;  則http://localhost/product   時會重定向到http://www.baidu.com/product/(這是個坑)
2、server_name_in_redirect off (默認值)URL 重定向為: 原 URL 中的域名 + 目錄名 + /。

 

轉載於:https://www.cnblogs.com/jedi1995/p/11320357.html

 

今天配置Nginx時,我設置了一個域名: http://www.yuhongchun027.com ,下面有web目錄為www;現在的問題就是如果我訪問 http://www.yuhongchun027.com/www/ 就可以顯示出地址,但如果我訪問 http://www.yuhongchun027.com/www 結果卻提示說找不到所需要的頁面。哈哈,又開始犯老錯了,nginx不會自動在請求的最后加上一個/的,原因是nginx不會自動判斷請求的是一個文件還是一個目錄,解決方法為:
在配置文件中location里加入如下代碼
if (-d $request_filename) {
rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
}
這樣再對 http://www.yuhongchun027.com/www 請求,nginx就會進行判斷了,如果請求的是一個文件夾,會自動在最后加上/符號,如果請求的是一個文件,則不會改變原有url
接下來對這段代碼進行一個解釋
1、if (-d $request_filename),如果請求的是一個文件夾,則為真,進到if語句中執行
2、rewrite是執行url重寫操作
3、^/(.*)([^/])$表示以/符號開始並緊跟着任何字符,同時不是以/為結束的字符串,在我的url中,(.*)表示的ww,([^/])表示的w
4、 http://$host/$1$2/ 表示的重寫后的地址,$host是請求的域名,$1是前面第一個括號里的內容ww,在我的url里就是wordpres $2是前面第二個括號里的內容,在我的url里是w
5、permanent表示,返回永久重定向的HTTP狀態301

※這里值得一說的是,撫琴煮酒瀏覽IE的習慣一般是 http://www.yuhongchun027/www ,相信大家習慣跟我一樣,不會在www后面加上/的;在為客戶設計網站時,首頁不考慮這些問題,但牽涉到二級目錄時,我一般會加上以上正則,這樣會使整個網站設計更人性化點。

 
 
 
 
1、proxy_pass 只是HOST
proxy_pass 只是HOST,不包含任何路徑,比如

* http://host - √
* https://host - √
* http://host:port - √
* https://host:port - √
* http://host/ - x
* http://host:port/ - x
這種情況下,會把匹配到的所有路徑直接穿透轉發。比如以下的配置

location /api/ {
    proxy_pass http://127.0.0.1:3000;
}
訪問 http://127.0.0.1:80/api/cc , 后端請求的地址是/api/cc

2、proxy_pass 包含路徑
這里的路徑哪怕只是一個 / 也是存在的,如:

http://host - x
https//host/ - √
http://host :port- x
https://host :port/ - √
http://host/api - √
http://host/api/ - √
這種情況下,url 里面會 去掉 location 匹配的字符串,拼接到 proxy_pass 再進行轉發。

location /api/ {
    proxy_pass http://127.0.0.1:3000/;
}
訪問 http://127.0.0.1:81/api/cc , 后端請求的地址是/cc

3、重寫代理鏈接 - url rewrite
使用 rewrite 指令並且生效后,proxy_pass url 鏈接中的路徑會被忽略,如:

server {
    listen  83;
    location / {
        rewrite ^/api/(.*) /fixpath=$1 break;
        proxy_pass http://127.0.0.1:3000/node/;
    }
    
    location ^/api/ {
        rewrite ^/api/(.*) /fixpath=$1 break;
        proxy_pass http://127.0.0.1:3000/node/;
    }
}
訪問 http://127.0.0.1:83/bb/cc 得到的請求地址是/node/bb/cc (匹配上 / 了,沒有匹配 rewrite)

訪問 http://127.0.0.1:83/api/cc 得到的請求地址是/fixpath=cc (我們寫的 proxy_pass http://127.0.0.1:3000/node/ 里面的 node 路徑丟失了 )

其它示例:

location ~ ^/smx/(test|production) {
    rewrite ^/smx/(?:test|production)/(.*)$ /cxf/$1 break;
    proxy_pass http://localhost:8181;
}
 
location ~ ^/es/(test|production) {
    rewrite ^/es/(?:test|production)/(.*)$ /$1 break;
    proxy_pass http://localhost:9200;
}
4、更多
在github上看到的這本小書 :arrow_down:

(參考資料)[ https://xuexb.github.io/learn... ]

我的 nginx 配置

events {
}
http {
    # proxy_pass url 只是 host
    # 這時候 location 匹配的完整路徑將直接透傳給 url ,如:
     server {
            listen       80;
            
            location / {
                proxy_pass http://127.0.0.1:3000;
            }
 
            location /api/ {
                proxy_pass http://127.0.0.1:3000;
            }
           
            
        }
    # url 包含路徑
    # 當 proxy_pass url 的 url 包含路徑時,匹配時會根據 location 的匹配后的鏈接透傳給 url ,注意匹配后就是這樣:
     server {
            listen       81;
            
            location / {
                proxy_pass http://127.0.0.1:3000/;
            }
 
            location /api/ {
                proxy_pass http://127.0.0.1:3000/;
            }
            location /bpi/ {
                proxy_pass http://127.0.0.1:3000/v1;
            }
            location /cpi {
                proxy_pass http://127.0.0.1:3000/v1;
            }
        }
        # 當 location 以正則形式匹配時,proxy_pass 就不能以 / 結束了,也就是不能包含路徑了, 會提示配置錯誤,比如錯誤的:
 
     server {
            listen       82;
            
            location / {
                proxy_pass http://127.0.0.1:3000/;
            }
        # nginx: [emerg] "proxy_pass" cannot have URI part in location given by regular expression, or inside named location, or inside "if" statement, or inside "limit_except" block in /test.conf:47
        #    location ~* ^/api/ {
        #         proxy_pass http://127.0.0.1:3000/;
        #     }
 
        # nginx: [emerg] "proxy_pass" cannot have URI part in location given by regular expression, or inside named location, or inside "if" statement, or inside "limit_except" block in /Users/tangdaoyuan/code/anheng/jz-bingjiang/test.conf:52
        #    location ~* ^/api/ {
        #         proxy_pass http://127.0.0.1:3000/b1;
        #     }
        }
 
    # 使用 rewrite 指令並且生效后,proxy_pass url 鏈接中的路徑會被忽略,如:
     server {
            listen       83;
            location / {
                proxy_pass http://127.0.0.1:3000/node/;
            }
            location ^/api/ {
                rewrite ^/api/(.*) /fixpath=$1 break;
                proxy_pass http://127.0.0.1:3000/node/;
            }
        }
 
}
測試流程 : node 運行 服務, 啟動 Nginx 轉發 , 再用postman 發送請求。

  轉自:https://blog.csdn.net/justlpf/article/details/103390946

https://www.codercto.com/a/68778.html

 

 

url形式為1_11或其他形式需要在后面加上/組成1_11/,在server段中添加如下 :

rewrite ^([^.]*[^/])$ $1/ permanent;
下面這個規則將重定向所有的url包括css等,所以不合適:

rewrite ^(.*[^/])$ $1/ permanent;
也可以在偽靜態規則中排除掉特定 格式,如下是當非.html結尾時才重定向

RewriteRule ^(.*?(?<!(html))$) $1/ permanent;
————————————————
版權聲明:本文為CSDN博主「youcijibi」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/youcijibi/article/details/86977271

 

 

 

運行環境
nginx:1.15.1
windows: 2008

需求描述
使用 nginx 配置tomcat集群后,訪問web時,無法正常打開頁面,需要在test后面添加斜杠/,才能正常訪問:
http://127.0.0.1/test 無法訪問
http://127.0.0.1/test/ 訪問正常

nginx 配置方式如下:

配置服務器集群組
upstream backend{
# server 1
server 127.0.0.1:8080 weight=1 max_fails=3 fail_timeout=30s;
# server 2
server 127.0.0.1:8180 weight=1 max_fails=3 fail_timeout=30s;
}
server {
# 其他配置項
# 映射服務器集群
location /test/{
proxy_pass http://backend;
}
}

解決辦法
修訂server配置,將 /test 替換為 /test/,問題解決。

server {
# 其他配置項
# 映射服務器集群
location /test/{
proxy_pass http://backend;
}
}


————————————————
版權聲明:本文為CSDN博主「huryer」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/huryer/article/details/81091964

 

 

nginx 神奇的斜線,各種斜線的搭配訪問結果

兩台服務器,一台客戶端
服務器B1:192.168.1.1

服務器B2 :192.168.2.2 

客戶端C1 :127.0.0.1

1、代理模式
在客戶端中請求服務器B1,請求路徑為
http://192.168.1.1/test/api
B1 配置

location /test/ {
   proxy_pass http://192.168.2.2/;
}
服務器B2 收到的請求: //api
location /test {
  proxy_pass http://192.168.2.2/;
}
服務器B2收到的請求:  //api
location /test/ {
  proxy_pass http://192.168.2.2;
}
服務器B2收到的請求:  /test/api
location /test {
  proxy_pass http://192.168.2.2
}
服務器B2收到的請求:  /test/api
location /test/ {
  proxy_pass http://192.168.2.2/on/;
}
服務器B2收到的請求: /on/api
location /test {
  proxy_pass http://192.168.2.2/on/;
}
服務器B2收到的請求: /on//api
location /test/ {
  proxy_pass http://192.168.2.2/on;
}
服務器B2收到的請求: /onapi
location /test {
  proxy_pass http://192.168.2.2/on;
}
服務器B2收到的請求為: /on/api

2、本地資源模式
在客戶端中請求服務器B1,請求路徑為
http://192.168.1.1/test/api
http://192.168.1.1/test/api/index.html
B1配置

location /test/ {
  root /usr/local/;
}
服務器B1實際請求資源路徑
/usr/local/test/api
/usr/local/test/api/index.html
location /test/ {
  root /usr/local;
}
服務器B1實際請求資源路徑
/usr/local/test/api
/usr/local/test/api/index.html
location /test {
  root /usr/local/;
}
服務器B1實際請求資源路徑
/usr/local/test/api
/usr/local/test/api/index.html
location /test {
  root /usr/local;
}
服務器B1實際請求資源路徑
/usr/local/test/api
/usr/local/test/api/index.html
location /test/ {
  alias /usr/local/;
}
服務器B1實際請求資源路徑
/usr/local/api
/usr/local/api/index.html
location /test {
  alias /usr/local/;
}
服務器B1實際請求資源路徑
/usr/local//api
/usr/local//api/index.html
————————————————
版權聲明:本文為CSDN博主「好好的浩浩」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/ruihaol/article/details/79526749

  

 

解決Nginx下二級目錄斜杠問題

不少網站頁面習慣於在URL中的目錄名后不加斜杠“/”,這在在apache下會自動添加一個斜杠從而不會造成什么問題,但是如果URL中的二級目錄名后不加斜杠的話在Nginx下就會出現403 Forbidden的問題。在網上找到的比較可靠的原因供以想深究的TX進行參考:

在某些情況下(具體可參考 wiki.nginx.org),Nginx 內部重定向規則會被啟動,例如,當 URL指向一個目錄並且在最后沒有包含“/”時,Nginx 內部會自動的做一個 301 重定向,這時會有兩種情況:

1、 server_name_in_redirect on(默認),URL 重定向為: server_name 中的第一個域名 + 目錄名 + /;

2、 server_name_in_redirect off,URL 重定向為: 原 URL 中的域名 + 目錄名 + /。

當你有多個 域名要指向同一個虛擬主機,並且你自己寫 301 重定向規則把它們合並到某一個域名時,情況就更復雜了:首先,nginx 檢查 URL,如果符合條件,就用該規則(你寫的)做第一遍重定向,接着,檢查新生成的URL,如果符合內部自動重定向之條件,就用前面提到的規則再做一次重定向。

至於 PHP 的 $_SERVER["SERVER_NAME"],在 nginx 中默認是由 nginx 的變量 $server_name提供,這時它和重定向沒有關系,始終是 server_name 設置中的第一個域名,但這是可以被改變的,在你的 nginx 配置中找到fastcgi_param 部分,修改
fastcgi_param SERVER_NAME $server_name; 為fastcgi_param SERVER_NAME $host; 但現在就要注意了,此時$_SERVER["SERVER_NAME"] 會受你寫的和 nginx 自己的重定向規則所影響而變化。

現在就清楚了,如果 MediaWiki 是通過 $_SERVER["SERVER_NAME"] 來自己處理 URL 的話,那么在 nginx + php的默認環境下,它獲得的將始終是 server_name 設置中的第一個域名,所以造成了“不管通過什么域名訪問 MediaWiki首頁,都會被跳轉到其中的一個域名上。”,這不是 nginx 的重定向造成的,雖然默認 server_name_in_redirect 是on,但這個指令的影響范圍僅僅只是 nginx 自己內部的重定向規則。

目前按照網上提供的一些主流方法,比如在 conf/nginx.conf 主配置文件里面加上

    optimize_server_names off; server_name_in_redirect off; 

在FreBSD+Nginx的架構下是無效的,而且optimize_server_names還對Nginx版本有限制。目前比較靠譜的方法還是使用正則來重定向:


    if (-d $request_filename){ rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent; } 

將上面的代碼放在相應虛擬主機配置文件的 location / {}里即可。這樣做的缺點就是必須對每一個需要自動添加斜杠的虛擬主機都要加上相應的內容。

 

解決Nginx中location匹配不到末尾不加斜杠的URL

在Nginx中配置location的匹配規則時,相信大家都遇到過 URL 去掉末尾斜杠(/)匹配失敗的情況。
我們先來舉例描述一下這個問題。
example配置
假設我們配置是這樣:
server {
	listen 80
	server_name xxx.com
	...
	...
	location /yyy {
    	root /home/projects;
		index  index.html index.htm;
   }
	...
	...
}
復制代碼那么,如果我們訪問 xxx.com/yyy/ ,會匹配到服務器上/home/projects/yyy文件夾,並打開index.html或者index.htm文件。
而當我們訪問 xxx.com/yyy ,你會發現,匹配失敗
這種情況下,你當然可以祈禱用戶能夠每次輸入這個 URL 的時候都帶上(/),但是說實在的,我估計你每次打開百度的時候都是直接輸入 www.baidu.com 而不會加斜杠輸入 www.baidu.com/ 。末尾斜杠顯得如此多此一舉。
那如何才能解決這個問題呢?
解決思路
既然加了斜杠可以匹配,那么,我們就可以對不加斜杠的 URL 進行重寫,給它加上末尾斜杠,然后重定向,不就解決問題了。
這里,我們需要用到 Nginx 中 ngx_http_rewrite_module 這個 module。
首先,我們判斷請求資源是否是請求目錄類型
if ( -d $request_filename )
復制代碼然后我們利用正則來匹配、替換,並用rewrite中permanent來重定向(301)
rewrite ^/(.*)([^/])$ $scheme://$host/$1$2/ permanent;
復制代碼正確配置
修改配置如下
server {
	listen 80
	server_name xxx.com
	...
	...
	location /yyy {
    	root /home/projects;
	   	if ( -d $request_filename ){
			rewrite ^/(.*)([^/])$ $scheme://$host/$1$2/ permanent;
		}
		index  index.html index.htm;
   }
	...
	...
}
復制代碼然后驗證配置是否有錯誤
$ nginx -t 
復制代碼如果沒有語法錯誤,會提示

/nginx.conf syntax is ok

最后重啟看效果
$ nginx -s reload

作者:Quenice
鏈接:https://juejin.im/post/5bae3afae51d450ea246a9ed
來源:掘金
著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處。

  轉自:https://juejin.im/post/5bae3afae51d450ea246a9ed

 

nginx 的proxy_pass 基本設置問題

曾在網上看到一些問題,比如 nginx 的proxy_pass后面的地址加“/”與不加“/”有區別。

   參看nginx英文文檔后,發現:

If it is necessary to transmit URI in the unprocessed form then directive proxy_pass should be used without URI part:

location  /some/path/ {
  proxy_pass   http://127.0.0.1;
}

大概意思就是說,如果你不想nginx對你的URI請求有任何形式的修改,那么,proxy_pass的配置中就不應該有任何URI部分。

舉個例子,nginx服務器IP為10.0.0.20,它的配置不含URI:

location /first/second/ {
        proxy_pass http://10.0.0.30:90;
}
那么,

原:     http://10.0.0.20/first/second/test.html
轉:http://10.0.0.30:90/first/second/test.html


 割一下,如果配置成含URI:
location /first/second/ {
proxy_pass http://10.0.0.30:90/myuri;
}
那么,

原: http://10.0.0.20/first/second/test.html
轉:http://10.0.0.30:90/myuri/test.html

簡單地說,配置了URI之后,跳轉行為可能會令你感到莫名其妙,要有充分的思想准備。

 轉自:https://blog.csdn.net/rj042/article/details/7574523?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM