当前位置: 首页 > news >正文

Nginx配置HTTP强制跳转到HTTPS

https 访问我们的测试域名 https://www.xxx.com 站点,但是当我们直接在浏览器地址栏中直接输入 www.xxx.com 的时候却发现进入的是 http 协议的网站,这与我们的初衷不一致。

由于浏览器默认访问域名使用的是80端口,而当我们使用SSL证书后,网站的端口就变成了443,所以当我们直接在浏览器中输入网址 www.xxx.com 的时候进入的是 80 端口的 HTTP 站点而不是 443 端口的 HTTPS 站点。

解决方法

这里提供两种 http 跳转到 https 的方法:

1. 使用nginx的 rewrite 将请求过来的 http URL直接重写成 https

server {

listen 80;

#填写绑定证书的域名

server_name www.xxx.com;

#强制将http的URL重写成https

rewrite ^(.*) https://$server_name$1 permanent;

}

2. 使用301重定向的方式将 http 的请求重定向到 https 上

server {

listen 80;

#填写绑定证书的域名

server_name www.xxx.com;

#把http的域名请求转成https

return 301 https://$host$request_uri;

}

3.完整的代码

# https配置

server {

listen 443 ssl;

server_name www.xxx.com xxx.com;

#ssl on; #开启ssl支持

ssl_certificate /data/ssl/www.xxx.com.pem; #指定服务器证书路径

ssl_certificate_key /data/ssl/www.xingguangshe.com.key; #指定私钥证书路径

ssl_session_timeout 5m;

#ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

#ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;

#ssl_prefer_server_ciphers on;

root /myweb/new/xxx.com;

location / {

index index.php index.html index.htm;

if (!-e $request_filename) {

rewrite ^/(.*)$ /index.php/$1 last;

break;

}

}

location ~ \.php/?.*$ {

fastcgi_pass 127.0.0.1:9000;

fastcgi_split_path_info ^(.+\.(?:php|phar))(/.*)$;

fastcgi_intercept_errors on;

fastcgi_index index.php;

include fastcgi_params;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

fastcgi_param PATH_INFO $fastcgi_path_info;

#fastcgi_pass php-fpm; #uquq use this

}

location ~ /\.ht {

deny all;

}

}

#http配置

server {

listen 80;

#listen somename:8080;

server_name www.xxx.com xxx.com;

client_max_body_size 80m;

#error_page 404 /data/ymg280/404.html;

#error_page 500 502 503 504 /errors/default/50x.html;

rewrite ^(.*) https://$server_name$1 permanent;

if ($host != 'ww.xxx.com'){

#rewrite ^/(.*)$ http://www.xxx.com/$1 permanent;

}

root /myweb/new/xxx.com;

location / {

index index.php index.html index.htm;

if (!-e $request_filename) {

rewrite ^/(.*)$ /index.php/$1 last;

break;

}

}

#pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

location ~ \.php/?.*$ {

fastcgi_pass 127.0.0.1:9000;

fastcgi_split_path_info ^(.+\.(?:php|phar))(/.*)$;

fastcgi_intercept_errors on;

fastcgi_index index.php;

include fastcgi_params;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

fastcgi_param PATH_INFO $fastcgi_path_info;

#fastcgi_pass php-fpm;

}

#deny access to .htaccess files, if Apache's document root

location ~ /\.ht {

deny all;

}

}

http://www.lryc.cn/news/8743.html

相关文章:

  • 从实现到原理,聊聊Java中的SPI动态扩展
  • 3、MySQL字符集
  • 大漠插件最新中文易语言模块7.2302
  • 极客大挑战 2021
  • C#开发的OpenRA加载文件的管理
  • SSM实现文件上传
  • OPENCV计算机视觉开发实践-图像的基本概念
  • Android 9.0 ResolverActivity.java多个app选择界面去掉始终保留仅有一次
  • 【算法 | 例题简答】相关例题讲解
  • 浅谈AQS
  • 关于服务连接器(Servlet)你了解多少?
  • 面对学员的投诉,中创教育是如何处理的?
  • 算法问题——排序算法问题
  • ArcGIS网络分析之构建网络分析数据集(一)
  • 微电影的行业痛点有哪些?
  • spark3.0源码分析-driver-executor心跳机制
  • 数据分析就要选择这款免费报表工具
  • node学习-3:服务器渲染和客户端渲染
  • LeetCode刷题笔记和周赛题解总目录
  • 用类比方式学习编程中函数递归(个人理解仅供参考)(内含汉诺塔问题的求解)
  • 【云原生之Docker实战】使用Docker部署Taskover开源个人任务管理工具
  • 5、SQL编程开发与注意事项
  • Allegro如何通过视图显示区分动态和静态铜皮操作指导
  • 测试开发之Django实战示例 第十一章 渲染和缓存课程内容
  • 90%企业在探索的敏捷开发怎么做?极狐GitLab总结了这些逻辑与流程
  • LeetCode-257. 二叉树的所有路径
  • 测试用例该怎么设计?—— 日常加更篇(下)
  • Java基础:接口
  • vuex基础入门:uniapp实现用户登录授权实战
  • Windows系统从权限维持角度进行应急响应