Nginx无法从无www重定向到www

时间:2019-04-07 17:28:16

标签: nginx redirect

我已经基于suggestions实现了我的nginx的配置,当向http://www.example.com发出请求时,该配置可以成功重定向到https://www.example.com

但是它不适用于非www,因此当我向https://example.com发出请求时,nginx无法重定向到https://www.example.com

请告知,我的配置如下所示:

upstream oms {
   server localhost:8081;
}

upstream backend {
       server localhost:8082;
}

#
# Redirect all non-www to www
#
server {
    server_name          example.com;
    ssl_certificate      /etc/ssl/certs/secure.example.com.9.2019.chain.crt;
    ssl_certificate_key  /etc/ssl/private/example.com.key;

    listen               *:80;
    listen               *:443 ssl spdy;
    listen               [::]:80 ipv6only=on;
    listen               [::]:443 ssl spdy ipv6only=on;

    return 301 https://www.example.com$request_uri;
}

#
# Redirect all non-encrypted to encrypted
#
server {
    server_name          www.example.com;
    listen               *:80;
    listen               [::]:80;

    return 301 https://www.example.com$request_uri;
}


server {
        #listen  80;
        #listen 443 ssl;
        listen               *:443 ssl spdy;
        listen               [::]:443 ssl spdy;
        ssl on;
        ssl_certificate      /etc/ssl/certs/secure.example.com.9.2019.chain.crt;
        ssl_certificate_key  /etc/ssl/private/example.com.key;

        server_name www.abc.com;
        access_log /var/log/nginx/nginx.vhost.access.log;
        error_log /var/log/nginx/nginx.vhost.error.log;

        location / {
                proxy_pass "http://oms";
        }

        location /backend/ {
                proxy_pass "http://backend";
        }
}

0 个答案:

没有答案