简体   繁体   English

使用Nginx代理重定向URL

[英]Redirecting a URL using nginx proxy

I have the following configuration in a linux nginx reverse proxy setup. 我在linux nginx反向代理设置中具有以下配置。 All is working fine for the basic redirection. 对于基本重定向,一切正常。 What I want to achieve is a rewrite from webmail.someserver.net to webmail.someserver.net/gw 我要实现的是从webmail.someserver.net重写为webmail.someserver.net/gw

I can't seem to get any rewrite rule to achieve this successfully and I'm probably missing something very simple. 我似乎无法获得任何重写规则来成功实现这一目标,并且可能缺少一些非常简单的东西。 Can anyone provide any ideas? 谁能提供任何想法? Thanks 谢谢

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;
            }

        }

If you simply want to redirect / to /gw use: 如果您只想将/重定向到/gw使用:

location = / {
    return 301 /gw;
}

See this and this for more details. 这个这个的更多细节。

As an aside, your statement rewrite ^ https://$http_host$request_uri? permanent; rewrite ^ https://$http_host$request_uri? permanent; ,您的语句rewrite ^ https://$http_host$request_uri? permanent; rewrite ^ https://$http_host$request_uri? permanent; can also be written more simply as: return 301 https://$http_host$request_uri 也可以更简单地写为: return 301 https://$http_host$request_uri

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM