使用nginx代理重定向URL

时间:2017-01-22 02:17:25

标签: nginx proxy

我在linux nginx反向代理设置中有以下配置。一切都适用于基本重定向。我想要实现的是从webmail.someserver.net重写到webmail.someserver.net/gw

我似乎无法获得任何重写规则来成功实现这一点,我可能会错过一些非常简单的事情。谁能提供任何想法? 感谢

server {
        listen       80;
    server_name  webmail.someserver.net;
    access_log  /var/log/www/webmail.someserver.net.access.log;  
    error_log  /var/log/www/webmail.someserver.net.error.log error; 
        rewrite ^ https://$http_host$request_uri? permanent;    # force redirect http to https
    index  index.html index.htm;  
    }
    server {
        listen 443;
        ssl on;
        ssl_certificate /etc/ssl/servercerts/someserver.net.chained.crt;        # path to your cacert.pem
        ssl_certificate_key /etc/ssl/servercerts/certified_wildcard_certificate_Private.key;    # path to your privkey.pem
        server_name webmail.someserver.net;                    #Domain Name
        proxy_set_header X-Forwarded-For $remote_addr;
#   rewrite ^ $scheme://$host/gw redirect;

        add_header Strict-Transport-Security "max-age=31536000; includeSubdomains";
        server_tokens off;

        location / {
            proxy_pass https://ebvl-009.cdn.someserver.net;
            proxy_redirect off;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            }

        }

1 个答案:

答案 0 :(得分:0)

如果您只是想将/重定向到/gw,请使用:

location = / {
    return 301 /gw;
}

有关详细信息,请参阅thisthis

另外,您的陈述rewrite ^ https://$http_host$request_uri? permanent;也可以更简单地写为:return 301 https://$http_host$request_uri