Nginx 在位置下返回

jcr*_*sel 5 redirect nginx http-redirect url-redirection nginx-location

我目前正面临一个使用 nginx 重定向到另一台主机的小问题。例如,我想将https://service.company.com/new/test.html重定向到https://new-service.company.com/test.html。

现在我有以下配置,它将我重定向到https://new-service.company.com/new/test.html。

server {
        # SSL
        ssl_certificate /etc/nginx/cert/chained_star_company.com.crt;
        ssl_certificate_key /etc/nginx/cert/star_company.com.key;

        listen 443;
        server_name service.company.com;

        location /new/$1 {
        return 301 $scheme://service-new.company.com/$1;
    }

}
Run Code Online (Sandbox Code Playgroud)

我也尝试以下相同的结果:

return 301 $scheme://service-new.company.com/$request_uri
Run Code Online (Sandbox Code Playgroud)

Ric*_*ith 5

您想重写 URI 并重定向。您可以使用location和return指令来实现它,但rewrite指令将是最简单的方法:

rewrite ^/new(.*)$ https://new-service.company.com$1 permanent;
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请参阅此文档。

顺便说一句,您的location块解决方案的问题是正则表达式捕获,不是。用:

location ~ ^/new(.*)$ {
    return 301 https://new-service.company.com$1$is_args$args;
}
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请参阅此文档。