nginx try_files 导致路径组件加倍,导致请求失败

nginx try_files 导致路径组件加倍,导致请求失败

我正在尝试创建两个相邻的文件夹,/var/www/site/var/www/overlay@site共存于一个站点中(出于文件权限和部署工具目的)。

我尝试调整基本配置(删除 HTTPS 配置):

server {
    server_name example.com;
    root /var/www/site;

    location / { try_files $uri $uri/ =404; }
}

进入此,将根向上移动一级:

server {
    server_name example.com;
    root /var/www;

    location / { try_files /site$uri /site$uri/ /overlay@site$uri /overlay@site$uri/ =404; }
}

但这会导致所有页面都返回 404,原因我无法理解。如果我启用调试日志记录,它似乎正在尝试加载例如/var/www/site/site/index.html,我不确定双倍site是从哪里来的。

以下是请求的 debug.log:

2021/12/18 14:56:22 [notice] 451123#451123: signal process started
2021/12/18 14:56:37 [debug] 451127#451127: *1 SSL_do_handshake: -1
2021/12/18 14:56:37 [debug] 451127#451127: *1 SSL_get_error: 2
2021/12/18 14:56:37 [debug] 451127#451127: *1 SSL handshake handler: 0
2021/12/18 14:56:37 [debug] 451127#451127: *1 ssl new session: 775CFEDA:32:169
2021/12/18 14:56:37 [debug] 451127#451127: *1 ssl new session: D9E5A8B9:32:169
2021/12/18 14:56:37 [debug] 451127#451127: *1 SSL_do_handshake: 1
2021/12/18 14:56:37 [debug] 451127#451127: *1 SSL: TLSv1.3, cipher: "TLS_AES_256_GCM_SHA384 TLSv1.3 Kx=any Au=any Enc=AESGCM(256) Mac=AEAD"
2021/12/18 14:56:37 [debug] 451127#451127: *1 reusable connection: 1
2021/12/18 14:56:37 [debug] 451127#451127: *1 http wait request handler
2021/12/18 14:56:37 [debug] 451127#451127: *1 malloc: 00005590274BA790:1024
2021/12/18 14:56:37 [debug] 451127#451127: *1 SSL_read: 399
2021/12/18 14:56:37 [debug] 451127#451127: *1 SSL_read: -1
2021/12/18 14:56:37 [debug] 451127#451127: *1 SSL_get_error: 2
2021/12/18 14:56:37 [debug] 451127#451127: *1 reusable connection: 0
2021/12/18 14:56:37 [debug] 451127#451127: *1 posix_memalign: 00005590274DBCB0:4096 @16
2021/12/18 14:56:37 [debug] 451127#451127: *1 http process request line
2021/12/18 14:56:37 [debug] 451127#451127: *1 http request line: "GET / HTTP/1.1"
2021/12/18 14:56:37 [debug] 451127#451127: *1 http uri: "/"
2021/12/18 14:56:37 [debug] 451127#451127: *1 http args: ""
2021/12/18 14:56:37 [debug] 451127#451127: *1 http exten: ""
2021/12/18 14:56:37 [debug] 451127#451127: *1 posix_memalign: 00005590274DCCC0:4096 @16
2021/12/18 14:56:37 [debug] 451127#451127: *1 http process request header line
2021/12/18 14:56:37 [debug] 451127#451127: *1 http header: "User-Agent: Mozilla/5.0+(compatible; UptimeRobot/2.0; http://www.uptimerobot.com/)"
2021/12/18 14:56:37 [debug] 451127#451127: *1 http header: "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
2021/12/18 14:56:37 [debug] 451127#451127: *1 http header: "Accept-Language: en-US,en;q=0.8"
2021/12/18 14:56:37 [debug] 451127#451127: *1 http header: "Accept-Charset: ISO-8859-1,UTF-8;q=0.7,*;q=0.7"
2021/12/18 14:56:37 [debug] 451127#451127: *1 http header: "Connection: close"
2021/12/18 14:56:37 [debug] 451127#451127: *1 http header: "cache-control: no-cache"
2021/12/18 14:56:37 [debug] 451127#451127: *1 http header: "Referer: https://www.witchoflight.com"
2021/12/18 14:56:37 [debug] 451127#451127: *1 http header: "host: www.witchoflight.com"
2021/12/18 14:56:37 [debug] 451127#451127: *1 http header: "accept-encoding: gzip, deflate"
2021/12/18 14:56:37 [debug] 451127#451127: *1 http header done
2021/12/18 14:56:37 [debug] 451127#451127: *1 event timer del: 3: 3230777386
2021/12/18 14:56:37 [debug] 451127#451127: *1 generic phase: 0
2021/12/18 14:56:37 [debug] 451127#451127: *1 rewrite phase: 1
2021/12/18 14:56:37 [debug] 451127#451127: *1 test location: "/"
2021/12/18 14:56:37 [debug] 451127#451127: *1 using configuration "/"
2021/12/18 14:56:37 [debug] 451127#451127: *1 http cl:-1 max:1048576
2021/12/18 14:56:37 [debug] 451127#451127: *1 rewrite phase: 3
2021/12/18 14:56:37 [debug] 451127#451127: *1 rewrite phase: 4
2021/12/18 14:56:37 [debug] 451127#451127: *1 post rewrite phase: 5
2021/12/18 14:56:37 [debug] 451127#451127: *1 generic phase: 6
2021/12/18 14:56:37 [debug] 451127#451127: *1 generic phase: 7
2021/12/18 14:56:37 [debug] 451127#451127: *1 generic phase: 8
2021/12/18 14:56:37 [debug] 451127#451127: *1 access phase: 9
2021/12/18 14:56:37 [debug] 451127#451127: *1 access phase: 10
2021/12/18 14:56:37 [debug] 451127#451127: *1 access phase: 11
2021/12/18 14:56:37 [debug] 451127#451127: *1 access phase: 12
2021/12/18 14:56:37 [debug] 451127#451127: *1 post access phase: 13
2021/12/18 14:56:37 [debug] 451127#451127: *1 generic phase: 14
2021/12/18 14:56:37 [debug] 451127#451127: *1 try files handler
2021/12/18 14:56:37 [debug] 451127#451127: *1 http script copy: "/www.witchoflight.com"
2021/12/18 14:56:37 [debug] 451127#451127: *1 http script var: "/"
2021/12/18 14:56:37 [debug] 451127#451127: *1 trying to use file: "/www.witchoflight.com/" "/var/www/www.witchoflight.com/"
2021/12/18 14:56:37 [debug] 451127#451127: *1 http script copy: "/www.witchoflight.com"
2021/12/18 14:56:37 [debug] 451127#451127: *1 http script var: "/"
2021/12/18 14:56:37 [debug] 451127#451127: *1 trying to use dir: "/www.witchoflight.com/" "/var/www/www.witchoflight.com/"
2021/12/18 14:56:37 [debug] 451127#451127: *1 try file uri: "/www.witchoflight.com/"
2021/12/18 14:56:37 [debug] 451127#451127: *1 generic phase: 15
2021/12/18 14:56:37 [debug] 451127#451127: *1 content phase: 16
2021/12/18 14:56:37 [debug] 451127#451127: *1 content phase: 17
2021/12/18 14:56:37 [debug] 451127#451127: *1 content phase: 18
2021/12/18 14:56:37 [debug] 451127#451127: *1 open index "/var/www/www.witchoflight.com/index.html"
2021/12/18 14:56:37 [debug] 451127#451127: *1 internal redirect: "/www.witchoflight.com/index.html?"
2021/12/18 14:56:37 [debug] 451127#451127: *1 rewrite phase: 1
2021/12/18 14:56:37 [debug] 451127#451127: *1 test location: "/"
2021/12/18 14:56:37 [debug] 451127#451127: *1 test location: ".gitignore"
2021/12/18 14:56:37 [debug] 451127#451127: *1 using configuration "/"
2021/12/18 14:56:37 [debug] 451127#451127: *1 http cl:-1 max:1048576
2021/12/18 14:56:37 [debug] 451127#451127: *1 rewrite phase: 3
2021/12/18 14:56:37 [debug] 451127#451127: *1 rewrite phase: 4
2021/12/18 14:56:37 [debug] 451127#451127: *1 post rewrite phase: 5
2021/12/18 14:56:37 [debug] 451127#451127: *1 generic phase: 6
2021/12/18 14:56:37 [debug] 451127#451127: *1 generic phase: 7
2021/12/18 14:56:37 [debug] 451127#451127: *1 generic phase: 8
2021/12/18 14:56:37 [debug] 451127#451127: *1 access phase: 9
2021/12/18 14:56:37 [debug] 451127#451127: *1 access phase: 10
2021/12/18 14:56:37 [debug] 451127#451127: *1 access phase: 11
2021/12/18 14:56:37 [debug] 451127#451127: *1 access phase: 12
2021/12/18 14:56:37 [debug] 451127#451127: *1 post access phase: 13
2021/12/18 14:56:37 [debug] 451127#451127: *1 generic phase: 14
2021/12/18 14:56:37 [debug] 451127#451127: *1 try files handler
2021/12/18 14:56:37 [debug] 451127#451127: *1 http script copy: "/www.witchoflight.com"
2021/12/18 14:56:37 [debug] 451127#451127: *1 http script var: "/www.witchoflight.com/index.html"
2021/12/18 14:56:37 [debug] 451127#451127: *1 trying to use file: "/www.witchoflight.com/www.witchoflight.com/index.html" "/var/www/www.witchoflight.com/www.witchoflight.com/index.html"
2021/12/18 14:56:37 [debug] 451127#451127: *1 http script copy: "/www.witchoflight.com"
2021/12/18 14:56:37 [debug] 451127#451127: *1 http script var: "/www.witchoflight.com/index.html"
2021/12/18 14:56:37 [debug] 451127#451127: *1 trying to use dir: "/www.witchoflight.com/www.witchoflight.com/index.html" "/var/www/www.witchoflight.com/www.witchoflight.com/index.html"
2021/12/18 14:56:37 [debug] 451127#451127: *1 trying to use file: "=404" "/var/www=404"
2021/12/18 14:56:37 [debug] 451127#451127: *1 http finalize request: 404, "/www.witchoflight.com/index.html?" a:1, c:2
2021/12/18 14:56:37 [debug] 451127#451127: *1 http special response: 404, "/www.witchoflight.com/index.html?"
2021/12/18 14:56:37 [debug] 451127#451127: *1 http set discard body
2021/12/18 14:56:37 [debug] 451127#451127: *1 uploadprogress error-tracker error: 404
2021/12/18 14:56:37 [debug] 451127#451127: *1 uploadprogress error-tracker not tracking in this location
2021/12/18 14:56:37 [debug] 451127#451127: *1 xslt filter header
2021/12/18 14:56:37 [debug] 451127#451127: *1 HTTP/1.1 404 Not Found
Server: nginx/1.18.0 (Ubuntu)
Date: Sat, 18 Dec 2021 19:56:37 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: close
Content-Encoding: gzip

2021/12/18 14:56:37 [debug] 451127#451127: *1 write new buf t:1 f:0 00005590274DD280, pos 00005590274DD280, size: 190 file: 0, size: 0
2021/12/18 14:56:37 [debug] 451127#451127: *1 http write filter: l:0 f:0 s:190
2021/12/18 14:56:37 [debug] 451127#451127: *1 http output filter "/www.witchoflight.com/index.html?"
2021/12/18 14:56:37 [debug] 451127#451127: *1 http copy filter: "/www.witchoflight.com/index.html?"
2021/12/18 14:56:37 [debug] 451127#451127: *1 image filter
2021/12/18 14:56:37 [debug] 451127#451127: *1 xslt filter body
2021/12/18 14:56:37 [debug] 451127#451127: *1 http postpone filter "/www.witchoflight.com/index.html?" 00005590274DD478
2021/12/18 14:56:37 [debug] 451127#451127: *1 http gzip filter
2021/12/18 14:56:37 [debug] 451127#451127: *1 malloc: 0000559027619160:12288
2021/12/18 14:56:37 [debug] 451127#451127: *1 gzip alloc: n:1 s:5952 a:8192 p:0000559027619160
2021/12/18 14:56:37 [debug] 451127#451127: *1 gzip alloc: n:512 s:2 a:1024 p:000055902761B160
2021/12/18 14:56:37 [debug] 451127#451127: *1 gzip alloc: n:512 s:2 a:1024 p:000055902761B560
2021/12/18 14:56:37 [debug] 451127#451127: *1 gzip alloc: n:512 s:2 a:1024 p:000055902761B960
2021/12/18 14:56:37 [debug] 451127#451127: *1 gzip alloc: n:256 s:4 a:1024 p:000055902761BD60
2021/12/18 14:56:37 [debug] 451127#451127: *1 gzip in: 00005590274DD4A8
2021/12/18 14:56:37 [debug] 451127#451127: *1 gzip in_buf:00005590274DD350 ni:00005590262646C0 ai:100
2021/12/18 14:56:37 [debug] 451127#451127: *1 malloc: 000055902761C170:4096
2021/12/18 14:56:37 [debug] 451127#451127: *1 deflate in: ni:00005590262646C0 no:000055902761C170 ai:100 ao:4096 fl:0 redo:0
2021/12/18 14:56:37 [debug] 451127#451127: *1 deflate out: ni:0000559026264724 no:000055902761C17A ai:0 ao:4086 rc:0
2021/12/18 14:56:37 [debug] 451127#451127: *1 gzip in_buf:00005590274DD350 pos:00005590262646C0
2021/12/18 14:56:37 [debug] 451127#451127: *1 gzip in: 00005590274DD4B8
2021/12/18 14:56:37 [debug] 451127#451127: *1 gzip in_buf:00005590274DD3A0 ni:0000559026264E20 ai:62
2021/12/18 14:56:37 [debug] 451127#451127: *1 deflate in: ni:0000559026264E20 no:000055902761C17A ai:62 ao:4086 fl:4 redo:0
2021/12/18 14:56:37 [debug] 451127#451127: *1 deflate out: ni:0000559026264E5E no:000055902761C1EB ai:0 ao:3973 rc:1
2021/12/18 14:56:37 [debug] 451127#451127: *1 gzip in_buf:00005590274DD3A0 pos:0000559026264E20
2021/12/18 14:56:37 [debug] 451127#451127: *1 free: 0000559027619160
2021/12/18 14:56:37 [debug] 451127#451127: *1 http chunk: 123
2021/12/18 14:56:37 [debug] 451127#451127: *1 write old buf t:1 f:0 00005590274DD280, pos 00005590274DD280, size: 190 file: 0, size: 0
2021/12/18 14:56:37 [debug] 451127#451127: *1 write new buf t:1 f:0 00005590274DD588, pos 00005590274DD588, size: 4 file: 0, size: 0
2021/12/18 14:56:37 [debug] 451127#451127: *1 write new buf t:1 f:0 000055902761C170, pos 000055902761C170, size: 123 file: 0, size: 0
2021/12/18 14:56:37 [debug] 451127#451127: *1 write new buf t:0 f:0 0000000000000000, pos 0000559026237155, size: 7 file: 0, size: 0
2021/12/18 14:56:37 [debug] 451127#451127: *1 http write filter: l:1 f:1 s:324
2021/12/18 14:56:37 [debug] 451127#451127: *1 http write filter limit 0
2021/12/18 14:56:37 [debug] 451127#451127: *1 posix_memalign: 0000559027618E60:512 @16
2021/12/18 14:56:37 [debug] 451127#451127: *1 malloc: 0000559027611C10:16384
2021/12/18 14:56:37 [debug] 451127#451127: *1 SSL buf copy: 190
2021/12/18 14:56:37 [debug] 451127#451127: *1 SSL buf copy: 4
2021/12/18 14:56:37 [debug] 451127#451127: *1 SSL buf copy: 123
2021/12/18 14:56:37 [debug] 451127#451127: *1 SSL buf copy: 7
2021/12/18 14:56:37 [debug] 451127#451127: *1 SSL to write: 324
2021/12/18 14:56:37 [debug] 451127#451127: *1 SSL_write: 324
2021/12/18 14:56:37 [debug] 451127#451127: *1 http write filter 0000000000000000
2021/12/18 14:56:37 [debug] 451127#451127: *1 http copy filter: 0 "/www.witchoflight.com/index.html?"
2021/12/18 14:56:37 [debug] 451127#451127: *1 http finalize request: 0, "/www.witchoflight.com/index.html?" a:1, c:2
2021/12/18 14:56:37 [debug] 451127#451127: *1 http request count:2 blk:0
2021/12/18 14:56:37 [debug] 451127#451127: *1 http finalize request: -4, "/www.witchoflight.com/index.html?" a:1, c:1
2021/12/18 14:56:37 [debug] 451127#451127: *1 http request count:1 blk:0
2021/12/18 14:56:37 [debug] 451127#451127: *1 http close request
2021/12/18 14:56:37 [debug] 451127#451127: *1 http log handler
2021/12/18 14:56:37 [debug] 451127#451127: *1 geoip2 http log handler
2021/12/18 14:56:37 [debug] 451127#451127: *1 free: 000055902761C170
2021/12/18 14:56:37 [debug] 451127#451127: *1 free: 0000000000000000
2021/12/18 14:56:37 [debug] 451127#451127: *1 free: 00005590274DBCB0, unused: 1
2021/12/18 14:56:37 [debug] 451127#451127: *1 free: 00005590274DCCC0, unused: 1469
2021/12/18 14:56:37 [debug] 451127#451127: *1 close http connection: 3
2021/12/18 14:56:37 [debug] 451127#451127: *1 SSL_shutdown: 1
2021/12/18 14:56:37 [debug] 451127#451127: *1 reusable connection: 0
2021/12/18 14:56:37 [debug] 451127#451127: *1 free: 0000559027611C10
2021/12/18 14:56:37 [debug] 451127#451127: *1 free: 00005590274BA790
2021/12/18 14:56:37 [debug] 451127#451127: *1 free: 000055902750CEC0, unused: 8
2021/12/18 14:56:37 [debug] 451127#451127: *1 free: 0000559027618E60, unused: 384

答案1

问题出在条目上/site$uri/。它们从规则中收到重定向index,因此对 的请求(例如)/foo在 处找到/site/foo/index.html,然后内部重定向到/site/foo/index.html,然后失败,因为它找不到/site/site/foo/index.html

我可以通过添加来解决这个问题

location /site/ { try_files $uri =404; }
location /overlay@site/ { try_files $uri =404; }

但这似乎仍然有一些与重定向相关的怪癖。我得到了一个更强大的修复,将我的覆盖更改为如下方式:

server {
    server_name example.com;
    root /var/www/site;

    location / { try_files $uri $uri/ @overlay; }
    location @overlay {
        root /var/www/overlay@site;
        try_files $uri $uri/ =404;
    }
}

(谢谢Paul Khuong 帮助我诊断问题

相关内容