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

nginx 的 location 优先级怎么没有优先=执行的?

  •  
  •   aries910 · 2021-01-13 12:00:28 +08:00 · 1614 次点击
    这是一个创建于 1192 天前的主题,其中的信息可能已经有所发展或是发生改变。

    看了 nginx 的 location 优先匹配不是先精准再普通最后正则么, 不知道为什么我的配置却总是直接下面的返匹配跳转 301, 换成 [ location ~ /.+ ] 也没用。。。 求大佬解答下,这是为什么~~~

    server { server_name www.a.com a.com; listen 80 ; access_log /var/log/nginx/access.log; location /.well-known/acme-challenge/ { auth_basic off; allow all; root /usr/share/nginx/html; try_files $uri =404; break; }

    rewrite ^(.*) https://$server_name$1 permanent; }

    server { server_name www.a.com a.com; listen 443 ssl http2 ; access_log /var/log/nginx/access.log; ssl_session_timeout 5m; ssl_session_cache shared:SSL:50m; ssl_session_tickets off; ssl_certificate /etc/nginx/certs/www.a.com.crt; ssl_certificate_key /etc/nginx/certs/www.a.com.key; add_header Strict-Transport-Security "max-age=31536000" always;

    root /home;

    location =/ { try_files '' /index.html; }

    location / { return 301 https://2.a.com$request_uri; } }

    2 条回复    2021-01-13 16:16:04 +08:00
    msg7086
        1
    msg7086  
       2021-01-13 15:41:52 +08:00
    try_files 跳转到 /index.html 后会触发 301 。
    你需要加一个 location = /index.html {} 来避免被 location / 带走。
    aries910
        2
    aries910  
    OP
       2021-01-13 16:16:04 +08:00
    @msg7086 感谢老哥,贴子提交才发现格式都没了,感谢老哥在这么乱的排版中给出建议,谢谢!
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   925 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 20:47 · PVG 04:47 · LAX 13:47 · JFK 16:47
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.