分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > IT知识

站点默认访问https

发布时间:2023-09-06 02:08责任编辑:白小东关键词:http

需求简介

 现在网站都是https访问了,再用http会显得很low,所以我要把网站设置为默认的https访问。  

1nginx的rewrite方法 

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

 
server { listen 192.168.1.111:80; server_name ?chaogg.com; ?rewrite ^(.*)$ https://$host$1 permanent; }

2nginx的497状态码

error code 497

497 - normal request was sent to HTTPS


 当此虚拟站点只允许https访问时,当用http访问时nginx会报出497错误码
 
利用error_page命令将497状态码的链接重定向到https://localhost这个域名上
 
server {   listen 192.168.1.11:443; ???#ssl端口   listen 192.168.1.11:80; ???#用户习惯用http访问,加上80,后面通过497状态码让它自动跳到443端口 ???server_name chaogg.com; ???#为一个server{......}开启ssl支持 ???ssl on; ???#指定PEM格式的证书文件 ???ssl_certificate /etc/nginx/chaogg.pem; ???#指定PEM格式的私钥文件 ???ssl_certificate_key /etc/nginx/chaogg.key; ?????#让http请求重定向到https请求 ????error_page 497 ???https://$host$uri?$args;
}

3index.html刷新网页 

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

index.html

 <html> <meta http-equiv="refresh" content="0;url=https://chaogg.com/"> </html>

nginx虚拟主机配置

 
 server { listen 192.168.1.11:80; server_name ???chaogg.com; ??location / { #index.html放在虚拟主机监听的根目录下 root /srv/www/http.chaogg.com/; } #将404的页面重定向到https的首页 error_page ???404 ???https://chaogg.com/; } 

上述三种方法均可以实现基于nginx强制将http请求跳转到https请求,请放心使用。

站点默认访问https

原文地址:https://www.cnblogs.com/peteremperor/p/9417472.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved