Nginx 防止單一位置的重新導向

Nginx 防止單一位置的重新導向

我有一個強制 https 的重定向:

server {
    listen 80;

    server_name example.com;
    return 301 https://example.com$request_uri;
}

效果很好,但我希望能夠使用 http 透過 http 存取單一文字文件迴聲模組

server {
    listen 80;

    location /ping {
        echo "http_pong";
    }

    server_name example.com;
    return 301 https://example.com$request_uri;
}

不幸的是我永遠無法到達,/ping因為我得到的是 301 重定向。如何防止全域重定向應用於該單一位置?

答案1

嘗試將重定向放在 下location /,如下所示:

server {
    listen 80;
    server_name example.com;

    location /ping {
        echo "http_pong";
    }

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

答案2

意識到 -「echo」已被棄用。使用“返回”

location / {
  return 200 'http_pong';

}

相關內容