[[ Закрывающий слеш в Nginx ]]

Nginx

Закрывающий слеш в Nginx

nginx/1.14.0

Закрывающий слеш и proxy_pass.
Читаем документацию и проводим эксперименты.

Пример 1

Если директива proxy_pass указана без URI, то при обработке первоначального запроса на сервер передаётся URI запроса в том же виде, в каком его прислал клиент, а при обработке изменённого URI - нормализованный URI запроса целиком:

location /test {              
    proxy_pass http://127.0.0.1:8098;
}
 
curl "http://127.0.0.1/test"  # запрос к nginx
"GET /test HTTP/1.0" 200 -    # запрос к backend
 
curl "http://127.0.0.1/test/"
"GET /test/ HTTP/1.0" 200 -
 
curl "http://127.0.0.1/test//"
"GET /test// HTTP/1.0" 200 -
 
curl "http://127.0.0.1/test///"
"GET /test/// HTTP/1.0" 200 -
 
#--------------------------------------------------
location /test/ {
    proxy_pass http://127.0.0.1:8098;
}
 
curl -i "http://127.0.0.1/test"
HTTP/1.1 301 Moved Permanently
Location: http://127.0.0.1/test/
 
 
curl "http://127.0.0.1/test/"
"GET /test/ HTTP/1.0" 200 -
 
curl "http://127.0.0.1/test//"
"GET /test// HTTP/1.0" 200 -
 
curl "http://127.0.0.1/test///"
"GET /test/// HTTP/1.0" 200 -
 
#--------------------------------------------------
location /test/foo {
    proxy_pass http://127.0.0.1:8098;
}
 
curl "http://127.0.0.1/test/foo"
"GET /test/foo HTTP/1.0" 200 -
 
curl "http://127.0.0.1/test/foo/"
"GET /test/foo/ HTTP/1.0" 200 -
 
curl "http://127.0.0.1/test/foo//"
"GET /test/foo// HTTP/1.0" 200 -
 
curl "http://127.0.0.1/test/foo///"
"GET /test/foo/// HTTP/1.0" 200 -
 
#--------------------------------------------------
location /test/foo/ {
    proxy_pass http://127.0.0.1:8098;
}
 
curl -i "http://127.0.0.1/test/foo"
HTTP/1.1 301 Moved Permanently
Location: http://127.0.0.1/test/foo/
 
curl "http://127.0.0.1/test/foo/"
"GET /test/foo/ HTTP/1.0" 200 -
 
curl "http://127.0.0.1/test/foo//"
"GET /test/foo// HTTP/1.0" 200 -
 
curl "http://127.0.0.1/test/foo///"
"GET /test/foo/// HTTP/1.0" 200 -

Пример 2

Если директива proxy_pass указана с URI, то при передаче запроса серверу часть нормализованного URI запроса, соответствующая location, заменяется на URI, указанный в директиве:

location /test {              
    proxy_pass http://127.0.0.1:8098/;
}
 
curl "http://127.0.0.1/test"
"GET / HTTP/1.0" 200 -
 
curl "http://127.0.0.1/test/"
"GET // HTTP/1.0" 200 -
 
curl "http://127.0.0.1/test//"
"GET // HTTP/1.0" 200 -
 
curl "http://127.0.0.1/test///"
"GET // HTTP/1.0" 200 -
 
#--------------------------------------------------
location /test/ {
    proxy_pass http://127.0.0.1:8098/;
}
 
curl -i "http://127.0.0.1/test"
HTTP/1.1 301 Moved Permanently
Location: http://127.0.0.1/test/
 
curl "http://127.0.0.1/test/"
"GET / HTTP/1.0" 200 -
 
curl "http://127.0.0.1/test//"
"GET / HTTP/1.0" 200 -
 
curl "http://127.0.0.1/test///"
"GET / HTTP/1.0" 200 -
 
#--------------------------------------------------
location /test/foo {
    proxy_pass http://127.0.0.1:8098/;
}
 
curl "http://127.0.0.1/test"
"GET / HTTP/1.0" 200 -
 
curl "http://127.0.0.1/test/"
"GET // HTTP/1.0" 200 -
 
curl "http://127.0.0.1/test//"
"GET // HTTP/1.0" 200 -
 
curl "http://127.0.0.1/test///"
"GET // HTTP/1.0" 200 -
 
#--------------------------------------------------
location /test/foo/ {
    proxy_pass http://127.0.0.1:8098/;
}
 
curl -i "http://127.0.0.1/test/foo"
HTTP/1.1 301 Moved Permanently
Location: http://127.0.0.1/test/foo/
 
curl "http://127.0.0.1/test/foo/"
"GET / HTTP/1.0" 200 -
 
curl "http://127.0.0.1/test/foo/"
"GET / HTTP/1.0" 200 -
 
curl "http://127.0.0.1/test/foo/"
"GET / HTTP/1.0" 200 -
 
curl "http://127.0.0.1/test/foo///"
"GET / HTTP/1.0" 200 -

Пример 3

Если location задан регулярным выражением, а также в именованных location'ах. В этих случаях proxy_pass следует указывать без URI.

location ~ /test[AB] {
    proxy_pass http://127.0.0.1:8098;
}
 
curl "http://127.0.0.1/testA"
"GET /testA HTTP/1.0" 200 -
 
curl "http://127.0.0.1/testB"
"GET /testB HTTP/1.0" 200 -
 
curl "http://127.0.0.1/testA/"
"GET /testA/ HTTP/1.0"
 
curl "http://127.0.0.1/testA//"
"GET /testA// HTTP/1.0"
 
curl "http://127.0.0.1/testA///"
"GET /testA/// HTTP/1.0"
 
#--------------------------------------------------
location ~ /test[AB] {
    proxy_pass http://127.0.0.1:8098/;
}
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 /etc/nginx/sites-enabled/default:30






Обсуждение

Ваш комментарий. Вики-синтаксис разрешён:
248 +2 = 
 
practice/nginx/nginx_proxy_trailing_slash.txt · Последнее изменение: 2022/08/31 14:53 — 127.0.0.1
Gentoo Linux Gentoo Linux Driven by DokuWiki