NGINX环境下强制HTTP跳转HTTPS

不管是门户网站还是个人网站,添加HTTPS是一个趋势,而在NGINX下添加SSL证书后直接输入域名访问到的网站依然是HTTP的,我们要用301让HTTP强制跳转到HTTPS才可以,设置方法如下:

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

这一条强制http://bijiaofan.com和http://www.bijiaofan.com跳转https://www.bijiaofan.com

server {
    listen 443 ssl;
    ssl_certificate /www.bijiaofan.com/cert.pem;
    ssl_certificate_key /www.bijiaofan.com/privkey.pem;
    server_name www.bijiaofan.com;
    return 301 https://bijiaofan.com$request_uri;
}

这一条强制https://www.bijiaofan.com跳转https://bijiaofan.com,证书要改为自己的

server {
    listen 443 ssl;
    ssl_certificate /bijiaofan.com/cert.pem;
    ssl_certificate_key /bijiaofan.com/privkey.pem;
    server_name  bijiaofan.com;
}

这一条指定SSL域名,证书要改成自己的

发表评论

邮箱地址不会被公开。 必填项已用*标注

+ 58 = 68