【问题标题】:Nginx return under locationNginx返回位置
【发布时间】:2017-06-09 07:49:58
【问题描述】:

我目前在使用 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;
    }

}

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

return 301 $scheme://service-new.company.com/$request_uri

【问题讨论】:

    标签: redirect nginx url-redirection http-redirect nginx-location


    【解决方案1】:

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

    rewrite ^/new(.*)$ https://new-service.company.com$1 permanent;
    

    请参阅this document 了解更多信息。

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

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

    请参阅this document 了解更多信息。

    【讨论】:

      猜你喜欢
      • 2018-04-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-09
      • 1970-01-01
      • 2017-06-06
      • 2017-09-07
      • 1970-01-01
      • 2021-10-05
      相关资源
      最近更新 更多