要实现80端口自动跳转到443端口,可以在server块中添加一个新的server块,监听80端口,并进行重定向到443端口。修改后的配置如下:

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;
    
    server {
        listen       443 ssl;
        server_name  www.ruichuangqitian.com;
        
        ssl_certificate      /usr/local/nginx/ssl/test.pem;
        ssl_certificate_key  /usr/local/nginx/ssl/test.key;

        ssl_session_cache    shared:SSL:10m;
        ssl_session_timeout  10m;

        ssl_protocols TLSv1.2 TLSv1.3;
        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;  

        location / {
            root   html;
            index  index.html index.htm;
        }
    }
    
    server {
        listen 80;
        server_name www.ruichuangqitian.com;
        return 301 https://$host$request_uri;
    }
}

在上面的配置中,添加了一个新的server块,监听80端口,并设置server_name为需要重定向的域名。然后使用return 301指令将请求重定向到https://$host$request_uri,即将http协议转换为https协议。保存并重新加载Nginx配置后,访问80端口将自动跳转到443端口

http include mimetypes; default_type applicationoctet-stream; sendfile on; keepalive_timeout 65; server listen 443 ssl; server_name wwwruichuan

原文地址: http://www.cveoy.top/t/topic/i0qK 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录