nginx Http跳转到Https

nginx 80端口通过端口转发、重定向访问443端口,即http跳转到https:

一、终端cd到nginx配置文件夹:

$ cd /etc/nginx/conf.d/

二、修改80端口站点配置文件:

$ vim npen-net.conf

方法一:端口转发

server {
    listen 80;
    server_name npen.net;

    location / {
        proxy_pass http://localhost:443;
        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

方法二:重定向

server {
    listen       80;
    server_name  npen.net;
    rewrite ^(.*)$ https://$host$1 permanent; 
}

三、重启nginx服务:

$ systemctl restart nginx

附80/443站点配置:

server {
    listen       80;
    server_name  npen.net;
    rewrite ^(.*)$ https://$host$1 permanent; 
}
server
{
    listen       443;
    server_name  npen.net;
    location / {
		index  index.html index.htm index.php;
        #autoindex  on;
        if (!-e $request_filename) {
            rewrite ^/(.*) /index.php last;
        }
    }
    root   "/www/blog/public";
    ssl                  on;
    ssl_certificate      /ssl/npen-net/npen.net.cer;
    ssl_certificate_key  /ssl/npen-net/npen.net.key;
    ssl_session_cache   shared:SSL:10m;
    ssl_session_timeout  5m;
    ssl_protocols  SSLv2 SSLv3 TLSv1.2;
    ssl_ciphers  HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers   on;
    #ssl_session_tickets on;

    location ~ \.php(.*)$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_param  PATH_INFO  $fastcgi_path_info;
        fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
        include        fastcgi_params;
    }
    location /logs/ {
        root  /runtime/;
        break;
    }
    location /bug/ {
	if (!-e $request_filename) {
            rewrite ^([_0-9a-zA-Z-]+)?(/wp-.*) $2 last;
            rewrite ^([_0-9a-zA-Z-]+)?(/.*\.php)$ $2 last;
            rewrite ^ /bug/index.php last;
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq292913477/article/details/81364818