【问题标题】:Nginx - Redirecting a server_name to a different domain using location blockNginx - 使用位置块将 server_name 重定向到不同的域
【发布时间】:2019-01-19 19:32:40
【问题描述】:

我想要的是输入 test.com/path 并将其转到 anothersite.com,但它却转到了 differentsite.com。我相信我的格式正确,但似乎完全跳过了位置块。

server {
        listen       80;
        listen       [::]:80;
        server_name  test.com;
        return 301 https://$server_name$request_uri;
}


server {

        listen       443;
        listen       [::]:443;
        server_name  test.com;


        location /path {

        return 301 https://anothersite.com;
}

        return 301 https://differentsite.com;

}

【问题讨论】:

    标签: nginx networking uri url-redirection nginx-location


    【解决方案1】:

    事实证明,服务器块不应该有返回语句。每个路径应该有两个带有返回语句的位置块

        server {
                listen       80;
                listen       [::]:80;
                server_name  test.com;
                return 301 https://$server_name$request_uri;
        }
    
    
        server {
    
                listen       443;
                listen       [::]:443;
                server_name  test.com;
    
    
                location / {
    
                 return 301 https://differentsite.com;
    }
    
                location /path {
    
                return 301 https://anothersite.com;
        }
    
    
    
        }
    

    【讨论】:

      猜你喜欢
      • 2023-02-25
      • 2011-08-28
      • 1970-01-01
      • 2017-02-24
      • 2011-05-18
      • 1970-01-01
      • 2017-12-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多