Nginx 防止重定向单个位置

dge*_*gel 9 nginx

我有一个重定向来强制 https:

server {
    listen 80;

    server_name example.com;
    return 301 https://example.com$request_uri;
}
Run Code Online (Sandbox Code Playgroud)

效果很好,但我希望能够使用echo 模块通过 http 访问单个文本文件:

server {
    listen 80;

    location /ping {
        echo "http_pong";
    }

    server_name example.com;
    return 301 https://example.com$request_uri;
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,我永远无法到达,/ping因为我得到了 301 重定向。如何防止全局重定向应用于该单个位置?

pho*_*ops 8

尝试将重定向放在 下location /,如下所示:

server {
    listen 80;
    server_name example.com;

    location /ping {
        echo "http_pong";
    }

    location / {
        return 301 https://example.com$request_uri; 
    }
}
Run Code Online (Sandbox Code Playgroud)