Nginx重定向URL到安全域

时间:2018-06-21 04:29:26

标签: http nginx web https

我的Webstie托管在AWS EC2实例上,并且具有nginx 1.12.2和Ec2操作系统是centos,如何将http://example.comhttps://example.com重定向到https://www.example.com

谢谢

1 个答案:

答案 0 :(得分:0)

您将需要3个服务器部分(我几乎可以确定您忘记处理http://www.example.com情况)

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

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

server {
    listen 443 ssl; 
    server_name www.example.com;
    ssl_certificate ...
    location / {
             #your configuration here
           }
...