在進(jìn)行nginx反向代理配置的時(shí)候,location和proxy_pass中的斜線會(huì)造成各種困擾,有時(shí)候多一個(gè)或少一個(gè)斜線,就會(huì)造成完全不同的結(jié)果,所以特地將location和proxy_pass后有無(wú)斜線的情況進(jìn)行了排列組合,進(jìn)行了一次完整的測(cè)試,找出原理,以提高姿勢(shì)水平~
〇. 環(huán)境信息
兩臺(tái)nginx服務(wù)器
nginx A: 192.168.1.48
nginx B: 192.168.1.56
一. 測(cè)試方法
在nginx A中配置不同的規(guī)則,然后請(qǐng)求nginx A: http://192.168.1.48/foo/api
觀察nginx B收到的請(qǐng)求,具體操作是查看日志中的$request字段
二. 測(cè)試過(guò)程及結(jié)果
案例1
nginx A配置:
location /foo/ { proxy_pass http://192.168.1.56/;}
nginx B收到的請(qǐng)求:/api
案例2
nginx A配置:
location /foo/ { proxy_pass http://192.168.1.56/;}
nginx B收到的請(qǐng)求://api
案例3
nginx A配置:
location /foo/ { proxy_pass http://192.168.1.56/;}
nginx B收到的請(qǐng)求:/foo/api
案例4
nginx A配置:
location /foo/ { proxy_pass http://192.168.1.56/;}
nginx B收到的請(qǐng)求:/foo/api
案例5
nginx A配置:
location /foo/ { proxy_pass http://192.168.1.56/bar/;}
nginx B收到的請(qǐng)求:/bar/api
案例6
nginx A配置:
location /foo { proxy_pass http://192.168.1.56/bar/;}
nginx B收到的請(qǐng)求:/bar//api
案例7
nginx A配置:
location /foo/ { proxy_pass http://192.168.1.56/bar;}
nginx B收到的請(qǐng)求:/barapi
案例8
nginx A配置:
location /foo { proxy_pass http://192.168.1.56/bar;}
nginx B收到的請(qǐng)求:/bar/api
看到這里是不是都暈了呢,其實(shí)是有規(guī)律的
現(xiàn)在把這些案例按表格排列起來(lái),結(jié)果表示nginx B收到的請(qǐng)求
表一