首页域名资讯 正文

nginx强制使用https访问(http跳转到https) – HTTPS SSL 教程

2025-02-07 6 0条评论

需求简介

基于nginx搭建了一个https访问的虚拟主机,监听的域名是trustauth.cn,但是很多用户不清楚https和http的区别,会很容易敲成http://trustauth.cn,这时会报出404错误,所以需要做基于trustauth.cn域名的http向https的强制跳转

nginx的rewrite方法

思路

这应该是大家最容易想到的方法,将所有的http请求通过rewrite重写到https上即可

配置

[html] view plain copy print?

  1. server {
  2.     listen  192.168.1.111:80;
  3.     server_name trustauth.cn;
  4.     rewrite ^(.*)$  https://$host$1 permanent;
  5. }

搭建此虚拟主机完成后,就可以将http://trustauth.cn的请求全部重写到https://trustauth.cn上了

nginx的497状态码

error code 497

[html] view plain copy print?

  1. 497 – normal request was sent to HTTPS

解释:当此虚拟站点只允许https访问时,当用http访问时nginx会报出497错误码

思路

利用error_page命令将497状态码的链接重定向到https://trustauth.cn这个域名上

配置

[html] view plain copy print?

  1. server {
  2.     listen       192.168.1.11:443;  # ssl 端口
  3.     listen       192.168.1.11:80;   #用户习惯用http访问,加上80,后面通过497状态码让它自动跳到443端口
  4.     server_name  trustauth.cn;
  5.     #为一个server{……}开启ssl支持
  6.     ssl                  on;
  7.     #指定PEM格式的证书文件
  8.     ssl_certificate      /etc/nginx/test.pem;
  9.     #指定PEM格式的私钥文件
  10.     ssl_certificate_key  /etc/nginx/test.key;
  11.     #让http请求重定向到https请求
  12.     error_page 497  https://$host$uri?$args;
  13. }

index.html刷新网页

思路

上述两种方法均会耗费服务器的资源,我们用curl访问baidu.com试一下,看百度的公司是如何实现baidu.com向www.baidu.com的跳转 
可以看到百度很巧妙的利用meta的刷新作用,将baidu.com跳转到www.baidu.com.因此我们可以基于http://trustauth.cn的虚拟主机路径下也写一个index.html,内容就是http向https的跳转

index.html

[html] view plain copy print?

  1. <html>
  2. <meta http-equiv=“refresh” content=“0;url=https://trustauth.cn/”>
  3. </html>

nginx虚拟主机配置

[html] view plain copy print?

  1. server {
  2.     listen 192.168.1.11:80;
  3.     server_name trustauth.cn;
  4.     location / {
  5.                 #index.html放在虚拟主机监听的根目录下
  6.         root /srv/www/http.trustauth.cn/;
  7.     }
  8.         #将404的页面重定向到https的首页
  9.     error_page  404 https://trustauth.cn/;
  10. }

文章版权及转载声明

本文作者:亿网 网址:https://www.edns.com/ask/post/149997.html 发布于 2025-02-07
文章转载或复制请以超链接形式并注明出处。