【问题标题】:Location ignored in nginxnginx 中忽略的位置
【发布时间】:2020-02-04 23:59:03
【问题描述】:

我的 nginx 配置中有一个简单的位置,但从未被捕获

server {
    listen 80 default_server;
    server_name _;

    location /healthcheck {
        access_log off;
        add_header Content-Type "text/plain";
        return 200 "OK\n";
    }

    return 301 http://www.google.fr;
}

每次我转到http://xxx/healthcheck 时,都不会检测到该位置,我会被重定向到 Google。

我在文件中做错了什么? 谢谢

【问题讨论】:

    标签: nginx nginx-location nginx-config


    【解决方案1】:

    请切换 301 到 302 重定向代码并在浏览器中使用匿名模式,或者最好使用 curl 检查健康检查 url。 例如:

    curl -IL http://xxx/healthcheck

    301 总是在您测试某些东西时出现问题。

    变化不会被模仿。请耐心等待。让我们知道

    【讨论】:

    • 为了扩展答案,301 是一个 永久 重定向,浏览器将永远缓存它,而 302 重定向仅针对一个请求。
    【解决方案2】:

    = 前缀表示指定的位置应与请求的 URL 完全匹配。

    这是工作设置

    server {
        listen 80 default_server;
        server_name _;
        location = /healthcheck {
            access_log off;
            add_header Content-Type "text/plain";
            return 200 "OK\n";
        }
    
        return 301 http://www.google.fr;
    }
    

    server {
        listen 80 default_server;
        server_name _;
        location /healthcheck {
            access_log off;
            add_header Content-Type "text/plain";
            return 200 "OK\n";
        }
    }
    

    注意:不要忘记在所有更改后重新加载 nginx sudo nginx -s reload

    【讨论】:

    • 您能否尝试我在答案中发布的其他方法。 @Sylvain
    猜你喜欢
    • 2012-03-27
    • 1970-01-01
    • 2012-07-14
    • 2013-07-19
    • 2018-07-04
    • 1970-01-01
    • 2016-11-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多