Nginx重定向https到非www无法正常工作

时间:2016-05-25 21:44:05

标签: redirect nginx

我查看了Stack Overflow和其他网站,但似乎无法使此重定向工作。一切都按预期工作,除了:

以下工作:

http://example.com -> https://example.com
http://www.example.com -> https://example.com

这不会重定向到非www(它保持www到位):

https://www.example.com -> https://example.com

www和非www https版本似乎都按原样解析。有什么想法吗?

这是当前的配置:

# Redirect WWW to NON-WWW
server {
    # Server host
    server_name www.example.com;

    # Server ports
    listen 80;
    listen 443;
    listen [::]:80;
    listen [::]:443;

    # SSL
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

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

# Redirect HTTP to HTTPS
server {
    # Server host
    server_name example.com;

    # Server port
    listen 80;
    listen [::]:80;

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

# Main Block
server {
    # Server host
    server_name example.com;

    # Server port
    listen 443 ssl;
    listen [::]:443;

    # SSL
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_dhparam /etc/ssl/certs/dhparam.pem;
    ssl_session_timeout 1d;
    ssl_session_cache shared:SSL:50m;
    ssl_stapling on;
    ssl_stapling_verify on;
    add_header Strict-Transport-Security max-age=15768000;

    root /var/www/example.com;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }

    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
     }

    location ~ /.well-known {
        allow all;
    }
}

0 个答案:

没有答案
相关问题