V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
NGINX
NGINX Trac
3rd Party Modules
Security Advisories
CHANGES
OpenResty
ngx_lua
Tengine
在线学习资源
NGINX 开发从入门到精通
NGINX Modules
ngx_echo
whx20202
V2EX  ›  NGINX

请教一个 nginx 小问题

  •  
  •   whx20202 · 2018-01-25 20:15:53 +08:00 · 3628 次点击
    这是一个创建于 2275 天前的主题,其中的信息可能已经有所发展或是发生改变。

    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

    第 1 条附言  ·  2018-01-25 21:11:59 +08:00
    # 这个 location 只是捕捉 /的请求,实际转发到下面那个
    location = / {
    include location.conf;

    # 瞎 JB 填一个,反正也用不上
    root /aaa/bbb/;
    # 使用 index 会造成一次内部重定向,详情 https://nginx.org/en/docs/http/ngx_http_index_module.html#index
    index index.html;
    }

    location = /index.html {
    include location.conf;
    root /真 /实 /路 /径 /;
    }

    location / {
    include location.conf;

    proxy_pass http://127.0.0.1:8080;
    }
    15 条回复    2018-01-26 09:09:07 +08:00
    whx20202
        1
    whx20202  
    OP
       2018-01-25 20:22:42 +08:00
    而且 8080 端口的日志显示:
    "HTTP/1.1 GET /html/index.html" - 404 Not Found

    给我感觉像是第一个 location 把请求路径修改,然后丢给第二个 location
    qq641016547
        2
    qq641016547  
       2018-01-25 20:29:00 +08:00 via iPhone
    覆盖?不晓得如何精确
    h4lbhg1G
        3
    h4lbhg1G  
       2018-01-25 20:33:19 +08:00
    可以这样用么?我一般都是 root index 一起设置,index 也可以带路径啊。。。
    whx20202
        4
    whx20202  
    OP
       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
        5
    h4lbhg1G  
       2018-01-25 20:42:28 +08:00
    我是说 root /aa/bb/html ; index index.html;
    交换下两个 location 顺序呢?
    whx20202
        6
    whx20202  
    OP
       2018-01-25 20:51:57 +08:00
    @h4lbhg1G 还是不行,我想办法把第二个 location 穷举一下,让他匹配不上第一个好了
    pubby
        7
    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
        8
    whx20202  
    OP
       2018-01-25 21:03:07 +08:00
    @pubby 感谢!我看了半天的 location 文档,没想到在这里
    那看来我的需求是达不成了 我看看别的办法
    h4lbhg1G
        9
    h4lbhg1G  
       2018-01-25 21:03:32 +08:00
    @whx20202 嗯 似乎只有第二个表示默认的能匹配空路径,精确匹配的必须带 /,话说对空路劲加上一个 location 写一个正则^$似乎可行
    whx20202
        10
    whx20202  
    OP
       2018-01-25 21:05:48 +08:00
    @h4lbhg1G 7L 给了个答案,问题出在 index 上,而不是 location
    h4lbhg1G
        11
    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
        12
    whx20202  
    OP
       2018-01-25 21:12:36 +08:00
    @pubby 感谢 我又加了一个 location 专门处理 index redirect 过后的请求,这把彻底 OK 了
    在 append 里
    deletelazy
        13
    deletelazy  
       2018-01-26 00:22:17 +08:00 via iPhone
    第一个问题是两个规则都匹配到了,但是以最后一个匹配也就是第二个为准吧,可以限制匹配到了就不往下匹配
    iyangyuan
        14
    iyangyuan  
       2018-01-26 07:56:03 +08:00 via iPhone
    index 是默认路径,但是没有指明 root 或 alias,我觉得这种写法,本身就是给自己挖坑
    whx20202
        15
    whx20202  
    OP
       2018-01-26 09:09:07 +08:00
    @iyangyuan 没事 现在搞定了
    你这头像是吞食天地啊 我特别喜欢
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4764 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 39ms · UTC 09:51 · PVG 17:51 · LAX 02:51 · JFK 05:51
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.