如何强制 Nginx 将全站转向 WWW 和 HTTPS

简介:

一、简介

如何强制 Nginx 将全站转向 WWW 和 HTTPS?

下面我们以域名 example.com 进行举例,我们的目标是:

  1. http://example.com -> https://www.example.com
  2. https://example.com -> https://www.example.com
  3. http://www.example.com -> https://www.example.com
  4. https://www.example.com

二、如何强制 Nginx 将全站转向 WWW 和 HTTPS

01.在 /etc/nginx/conf.d目录下,新建配置文件example.com.conf

02.将下面的配置内容,拷贝到新建的配置文件中,保存并退出。

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name example.com www.example.com;

    return 301 https://www.example.com$request_uri;
}


server {
    listen *:443 ssl; 
    listen [::]:443 ssl; 
    server_name example.com;

    return 301 https://www.example.com$request_uri;
}

server {
    listen *:443 ssl http2; 
    listen [::]:443  ssl http2; 
    server_name www.example.com;      
              
    add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";
    add_header X-Frame-Options "DENY";

    charset utf8;

    access_log  /var/log/nginx/*.example.com/access.log  main;
    error_log  /var/log/nginx/*.example.com/error.log warn;

    location / {
        root   /root/itcoder/public;
        index  index.html index.htm;
    }

    # ssl on;
    ssl_certificate /etc/nginx/ssl/*.example.com.crt;
    ssl_certificate_key /etc/nginx/ssl/*.example.com.key;
    ssl_session_timeout  5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4:!DH:!DHE;
    ssl_prefer_server_ciphers  on;

    error_page  404              /404.html;
#    location = /404.html {
 #       root   /usr/share/nginx/html;
 #   }

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

03.运行下面的命令,检测配置文件是否合规:

nginx -t

如果配置文件没有语法错误,一般会提示如下:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

04.重新加载 Nginx 配置文件,使修改生效。

nginx -s reload

或者

systemctl reload nginx

三、总结

本站经过改造,已经强制 Nginx 将全站转向 WWW 和 HTTPS,欢迎体验。

  1. http://itcoder.tech -> https://www.itcoder.tech
  2. https://itcoder.tech -> https://www.itcoder.tech
  3. http://www.itcoder.tech -> https://www.itcoder.tech
  4. https://www.itcoder.tech

四、参考文档

  1. Redirect HTTP to HTTPS in Nginx
  2. Redirect all HTTP requests to HTTPS with Nginx
  3. Best way to redirect all HTTP requests to HTTPS with Nginx
相关文章
|
1月前
|
缓存 算法 应用服务中间件
nginx搭建https服务器
nginx搭建https服务器
|
3月前
|
网络安全
Discuz全站切换https,强制Discuz站点所有链接为https
Discuz全站切换https,强制Discuz站点所有链接为https
99 0
|
4月前
|
安全 网络协议 应用服务中间件
一文读懂HTTPS⭐揭秘加密传输背后的原理与Nginx配置攻略
一文读懂HTTPS⭐揭秘加密传输背后的原理与Nginx配置攻略
|
3月前
|
网络协议 安全 应用服务中间件
阿里云 网站https设置 sll申请与nginx跳转配置
阿里云 网站https设置 sll申请与nginx跳转配置
117 0
|
4月前
|
缓存 应用服务中间件 网络安全
nginx 日志,压缩,https功能介绍
nginx 日志,压缩,https功能介绍
|
4月前
|
缓存 安全 应用服务中间件
蓝易云 - Nginx的HTTPS部署与安全性能优化教程
以上就是在Nginx上部署HTTPS并进行安全性能优化的基本步骤。需要注意的是,这些步骤可能会根据您的具体需求和环境有所不同。
47 0
|
4月前
|
Ubuntu 应用服务中间件 Linux
nginx 配置代理ip访问https的域名配置
nginx 配置代理ip访问https的域名配置
618 2
|
4月前
|
应用服务中间件 网络安全 nginx
nginx配置https访问
nginx配置https访问
190 0
|
4月前
|
应用服务中间件 nginx
nginx配置https和直接访问静态文件的方式
nginx配置https和直接访问静态文件的方式
96 3
|
4月前
|
前端开发 应用服务中间件 网络安全
http转为https,ssl证书安装及nginx配置
http转为https,ssl证书安装及nginx配置
158 1