Nginx如何配置http强制跳转https

文章2021-03-26
Nginx如何配置http强制跳转https-下一朵云

1.具体方法是采用rewrite方式,在nginx的配置文件前添加80端口的强制跳转:

server{
         listen 80;
         server_name test.orcy.net;   
         #http强制跳转https
         rewrite ^(.*)$  https://$host$1 permanent;
}

2.完整的配置文件如下:

server{
        listen 80;
        server_name test.orcy.net;

        #http强制跳转https
        rewrite ^(.*)$  https://$host$1 permanent;
}
server {
        listen       443 ssl;
        server_name  test.orcy.net;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
        access_log  logs/test.orcy.net-ssl.log  main;
        error_log  logs/test.orcy.net-ssl.error.log;    

        location / {
            root   /srv/www/html;
            index  index.php index.html index.htm;
        }

        ssl_certificate cert/XXXX.pem;   #将domain name.pem替换成您证书的文件名
        ssl_certificate_key cert/XXXX.key;   #将domain name.key替换成您证书的密钥文件名
        ssl_session_timeout 5m;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;  #使用此加密套
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;   #使用该协议进行配置
        ssl_prefer_server_ciphers on;   


        location ~ \.php$ {
            root    /srv/www/html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
        error_page  404              /404.html;
        location = /404.html {
            root   html;
        }

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    } 

3.如果是非默认80/443端口的http强制跳转https该如何设置呢?

传送门>>《Nginx非80/443端口的http强制跳转https》