Nginx配置HTTP强制跳转到HTTPS

news/2024/7/8 1:18:33

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://lihuaxi.xjx100.cn/news/896352.html

相关文章

redis主从哨兵模式

一.为什么用redis主从模式 1.数据备份:主从复制实现数据的热备份。 2.故障恢复:当主节点出现问题时,由从节点提供服务,实现快速恢复。 3.负载均衡:读写分离,主节点提供写服务,从节点提供读服务。在写少读多时提高Redis的并发。 二.为什么使用哨兵模式 主要用于主节…

如何将vue2项目重构成vue3《后台管理系统》

文章目录前言一、基础配置总结前言 首先我们要知道vue3是基于vue2基础上更改了一些改变但改变不大,基本上把vue2的后台管理系统复制过来对项目进行改写就行 一、基础配置 说起登录页面。所谓登录就是要有一个参照物 你输入账号或者密码达到参照物的标准 就会执行下…

P5461 赦免战俘 (递归/分治)

链接 思路&#xff1a; 对于每一个小方阵&#xff0c;分别操作(取该方阵的左上角&#xff0c;把这部分设置成为已经赦免). z 1时&#xff0c;递归结束. 参考代码&#xff1a; #include <bits/stdc.h>int main() {std::ios::sync_with_stdio(false);std::cin.tie(nullp…

AcWing语法基础课笔记 第六章 C++中的函数

第六章 C中的函数 函数让代码变得更加简洁。 ——闫学灿 目录 1.函数基础 1.1编写函数 1.2调用函数 1.3形参和实参 1.4函数的形参列表 1.5函数返回类型 1.6局部变量、全局变量与静态变量 2.参数传递 传值参数​编辑 2.2传引用参数 2.3数组形参…

k8s之容器存储接口(CSI)ceph-csi-rbd部署

TOC CSI简介 容器存储接口&#xff08;Container Storage Interface&#xff09;&#xff0c;简称 CSI&#xff0c;CSI 试图建立一个行业标准接口的规范&#xff0c;借助 CSI 容器编排系统&#xff08;CO&#xff09;可以将任意存储系统暴露给自己的容器工作负载。 csi 卷类…

【Vue】基本交互指令

Vue el挂载点 <div id"app">{{message}} </div> <script>var app new Vue({el:"#app",data:{message:"hello"}}) </script>Vue实例的作用范围&#xff1a;管理el选项命中的元素及其内部的后代元素使用其他的选择器&a…

分布式新闻项目实战 - 11.定时计算热点文章(xxl-Job)

男人过了四十&#xff0c;千万要少说话&#xff0c;拉长脸&#xff0c;闭紧嘴&#xff0c;买件立领风衣&#xff0c;浓个眉大个眼&#xff0c;一直走&#xff0c;不要往两边看&#xff0c;还能再混几十年。 —— 冯唐 系列文章目录 项目搭建App登录及网关App文章自媒体平台&am…

C语言(强制类型转换)

一.类型转换原则 1.升级&#xff1a;当类型转换出现在表达式时&#xff0c;无论时unsigned还是signed的char和short都会被自动转换成int&#xff0c;如有必要会被转换成unsigned int(如果short与int的大小相同&#xff0c;unsigned short就比int大。这种情况下&#xff0c;uns…