Nginx 强制 http 转发到 https

想从A -> B ,那就使用重定向

return 301 https://www.ff11sf.com;

案例演示


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


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

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            return 301 https://www.zlk1999.xyz;

        }

        #error_page  404              /404.html;

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

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }



	
    # HTTPS server
    
    server {
        listen       443 ssl;
        server_name  www.zlk1999.xyz;

        ssl_certificate      1_zlk1999.xyz_bundle.crt;
        ssl_certificate_key  2_zlk1999.xyz.key;

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;

        location / {
            root   html;
            index  index.html index.htm;
        }
    }
还是那句话,你只看到了大佬轻松解决问题的光鲜,却不记得当初人家死磕Bug的痛苦。

万物皆入轮回,谁也躲不掉!

以上文章,均是我实际体验,写出来的笔记资料,不会出现全文盗用别人文章!烦请各位,支持下原创,请勿直接盗用,有条件的可以开通个会员支持下,谢谢!!!

Java Code Study » Nginx 强制 http 转发到 https

招聘、单纯问问题,都可以联系我

加我QQ 公司缺人,拉我入伙