Nginx:将非www重定向到www https

前端之家收集整理的这篇文章主要介绍了Nginx:将非www重定向到www https前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我有我的下面的Nginx配置,我试图将所有内容重定向https://www,无论例如http://example.com,http://www.example.comhttps://example.com中的内容.

我已经看了很多关于SO的主题,并尝试了几件但仍然难倒,我不能让https://example.com重定向https://www模式!?

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

server {
    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_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';
    ssl_dhparam /etc/Nginx/ssl/dhparams.pem;
    ssl_session_timeout 30m;
    ssl_session_cache shared:SSL:10m;
    ssl_buffer_size 8k;
    add_header Strict-Transport-Security max-age=31536000;

    root         /usr/share/Nginx/html;

    # Load configuration files for the default server block.
    include /etc/Nginx/default.d/*.conf;

    location / {
    }

    error_page 404 /404.html;
        location = /40x.html {
    }

    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }
}
使一个服务器阻止默认服务器,并为另一个服务器阻止一个真正的server_name.

server {
    listen  80 default_server;
    listen  443 ssl default_server;

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

server {
    listen  443 ssl;
    server_name www.example.com;

    ssl_certificate ...;
    ssl_certificate_key ...;
    ...
}

https的默认服务器需要有效的证书.假设您有一个通配符证书 – 大多数ssl_语句可以移动到外部块中并由两个服务器块继承.例如:

ssl_certificate ...;
ssl_certificate_key ...;
ssl_...;

server {
    listen  80 default_server;
    listen  443 ssl default_server;
    return  301 https://www.example.com$request_uri;
}

server {
    listen  443 ssl;
    server_name www.example.com;
    ...
}

有关更多信息,请参见this document

猜你在找的Nginx相关文章