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

简介:

本文最先发布在:

https://www.itcoder.tech/posts/how-to-force-nginx-redirect-to-www-and-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
相关文章
|
3月前
IIS上实现网站朝https://www的自动跳转
我们在做网站时时常有网站朝https://www的自动跳转的需求,以便在不输入www.子域名时也可以自动跳转到我们的当前站点,本文将介绍实现网站朝https://www的自动跳转的操作。
62 0
IIS上实现网站朝https://www的自动跳转
|
11天前
|
应用服务中间件 网络安全 nginx
nginx配置https访问
nginx配置https访问
24 0
|
21天前
|
应用服务中间件 nginx
nginx配置https和直接访问静态文件的方式
nginx配置https和直接访问静态文件的方式
27 3
|
5月前
|
负载均衡 Unix 应用服务中间件
深入解析HTTP反向代理:探索NGINX的神奇之处
深入解析HTTP反向代理:探索NGINX的神奇之处
|
5月前
|
负载均衡 Dubbo 应用服务中间件
Nginx系列教程(11) - HTTP动态负载均衡(一)
Nginx系列教程(11) - HTTP动态负载均衡(一)
61 0
|
2月前
|
网络协议 Unix 应用服务中间件
如何进行 Nginx HTTPS服务器搭建
【2月更文挑战第6天】
63 0
|
4月前
|
应用服务中间件 Linux 网络安全
Linux【脚本 06】HTTPS转发HTTP安装OpenSSL、Nginx(with-http_ssl_module)及自签名的X.509数字证书生成(一键部署生成脚本分享)
Linux【脚本 06】HTTPS转发HTTP安装OpenSSL、Nginx(with-http_ssl_module)及自签名的X.509数字证书生成(一键部署生成脚本分享)
64 1
|
4月前
|
应用服务中间件 网络安全 nginx
百度搜索:蓝易云【Nginx【https配置教程】】
现在,你的 Nginx 已经配置为使用 HTTPS。访问 `https://your_domain.com` 应该能够通过 SSL 连接访问你的网站。
45 2
|
4月前
|
负载均衡 安全 应用服务中间件
Nginx + Tomcat+HTTP/HTTPS实现负载均衡实例
Nginx + Tomcat+HTTP/HTTPS实现负载均衡实例
178 0
|
5月前
|
负载均衡 应用服务中间件 Linux
Nginx系列教程(12) - HTTP动态负载均衡(二)
Nginx系列教程(12) - HTTP动态负载均衡(二)
102 1