请教一个 nginx 小问题

2018-01-25 20:15:53 +08:00
 whx20202

server 块: root /aaa/bbb;

	location = / { 
        include location.conf;

        index html/index.html;
    }   

    location / { 
        include location.conf;

        proxy_pass http://127.0.0.1:8080;
    } 

按理来说, 第一个 location 带等于号=,应该是精确匹配,为啥我访问 www.hostname.com 的时候,却走了第二个 location 呢?

我把第二个 location 注释掉的话,能正常访问 index.html

3639 次点击
所在节点    NGINX
15 条回复
whx20202
2018-01-25 20:22:42 +08:00
而且 8080 端口的日志显示:
"HTTP/1.1 GET /html/index.html" - 404 Not Found

给我感觉像是第一个 location 把请求路径修改,然后丢给第二个 location
qq641016547
2018-01-25 20:29:00 +08:00
覆盖?不晓得如何精确
h4lbhg1G
2018-01-25 20:33:19 +08:00
可以这样用么?我一般都是 root index 一起设置,index 也可以带路径啊。。。
whx20202
2018-01-25 20:35:38 +08:00
@h4lbhg1G 我 index 设置了绝对路径感觉也不行。。。

比如 index /aaa/bbb/html/index.html ,那么那就会给 8080 端口请求这个 /aaa/bbb/html/index.html

总觉得是 location 1 的结果出来之后又跑到 location2 了
h4lbhg1G
2018-01-25 20:42:28 +08:00
我是说 root /aa/bb/html ; index index.html;
交换下两个 location 顺序呢?
whx20202
2018-01-25 20:51:57 +08:00
@h4lbhg1G 还是不行,我想办法把第二个 location 穷举一下,让他匹配不上第一个好了
pubby
2018-01-25 20:58:23 +08:00
index 的问题

http://nginx.org/en/docs/http/ngx_http_index_module.html#index

It should be noted that using an index file causes an internal redirect, and the request can be processed in a different location.
whx20202
2018-01-25 21:03:07 +08:00
@pubby 感谢!我看了半天的 location 文档,没想到在这里
那看来我的需求是达不成了 我看看别的办法
h4lbhg1G
2018-01-25 21:03:32 +08:00
@whx20202 嗯 似乎只有第二个表示默认的能匹配空路径,精确匹配的必须带 /,话说对空路劲加上一个 location 写一个正则^$似乎可行
whx20202
2018-01-25 21:05:48 +08:00
@h4lbhg1G 7L 给了个答案,问题出在 index 上,而不是 location
h4lbhg1G
2018-01-25 21:10:34 +08:00
The ngx_http_index_module module processes requests ending with the slash character (‘/’).

我看到这句随口说的,也不太懂。也许空路劲可以用 try file
whx20202
2018-01-25 21:12:36 +08:00
@pubby 感谢 我又加了一个 location 专门处理 index redirect 过后的请求,这把彻底 OK 了
在 append 里
deletelazy
2018-01-26 00:22:17 +08:00
第一个问题是两个规则都匹配到了,但是以最后一个匹配也就是第二个为准吧,可以限制匹配到了就不往下匹配
iyangyuan
2018-01-26 07:56:03 +08:00
index 是默认路径,但是没有指明 root 或 alias,我觉得这种写法,本身就是给自己挖坑
whx20202
2018-01-26 09:09:07 +08:00
@iyangyuan 没事 现在搞定了
你这头像是吞食天地啊 我特别喜欢

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/425975

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX