在Nginx中,我想将带有request-uri的子域请求重定向到子域,但是如果其中没有request-uri,则应重定向到主域

时间:2018-11-09 07:51:49

标签: nginx subdomain

我的问题陈述:
我的网域:example.com
子域:main.example.com

何时访问:
1. http://main.example.com/xyzhttps://main.example.com/xyz
必须将其重定向到https://main.example.com/xyz

  1. http://main.example.comhttps://main.example.com
    必须将其重定向到https://www.example.com

我正在使用nginx。 Nginx服务器的配置文件是什么?

我当前的设置是:

server{
        listen 443;
        ssl on;
        ssl_certificate /var/www/html/demo.crt;
        ssl_certificate_key /var/www/html/demo.key;
        server_name main.example.com$request_uri;

       location / {
                root /var/www/html;
                index  index.html index.htm;
                try_files $uri $uri/ /index.html;
        }
}

server {
    listen 80;
    server_name main.example.com$request_uri;
    return 301 https://main.example.com$request_uri;
}

server {
    listen 80;
    server_name main.example.com;
    return 301 https://www.example.com;
}

server {
    listen 443;
    server_name main.example.com;
    return 301 https://www.example.com;
}

1 个答案:

答案 0 :(得分:0)

尝试

server{
    listen 80;
    listen 443 ssl;
    server_name          main.example.com;
    ssl_certificate      /var/www/html/demo.crt;
    ssl_certificate_key  /var/www/html/demo.key;

    location / {
        proxy_pass https://www.example.com;
    }

    location ~ ^(/.+) {
         return 301  https://main.example.com$1;
    }
}

http://nginx.org/en/docs/http/configuring_https_servers.html#single_http_https_server