nginx从http跳转到https
背景介绍
项目前期使用http,后期为了安全方面的考虑,启用了https。
项目架构:前端使用nginx作为多个tomcat实例的反向代理和负载均衡。
实际上只需要在nginx上启用https即可,使客户端与nginx之后使用https方式通信,而nginx与tomcat之间依然以http方式通信。
现在需要将之前客户端所有的http请求全部都自动重定向为https,只需要在nginx上添加相应配置即可。
如下配置实现来源于Nginx HTTP 跳转至 HTTPS,但是我都实践验证过。
另外,也加入了一些自己的理解整理而成。
使用rewrite指令
server {
listen 80;
server_name domain.com;
rewrite ^(.*) https://$server_name$1 permanent;
}
server {
listen 443 ssl;
server_name domain.com;
ssl on;
ssl_certificate /etc/nginx/ssl/domain.com.crt;
ssl_certificate_key /etc/nginx/ssl/domain.com.crt;
# other
}
如果此时nginx作为Tomcat的前端反向代理的话,需要将相应配置放在配置ssl的server块中。
使用return指令
server {
listen 80;
server_name domain.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name domain.com;
ssl on;
ssl_certificate /etc/nginx/ssl/domain.com.crt;
ssl_certificate_key /etc/nginx/ssl/domain.com.crt;
# other
}
如果此时nginx作为Tomcat的前端反向代理的话,需要将相应配置放在配置ssl的server块中。
使用error_page指令
只允许HTTP来访问时,用HTTP访问会让Nginx报497错误,然后利用error_page将链接重定向至HTTPS上。
server {
listen 80;
listen 443 ssl;
server_name domain.com;
ssl on;
ssl_certificate /etc/nginx/ssl/domain.com.crt;
ssl_certificate_key /etc/nginx/ssl/domain.com.crt;
# other
error_page 497 https://$server_name$request_uri;
}
使用error_page指令时,将http和https的监听配置写在同一个server块中,对应的其他配置也需要在该server配置块中完成。
需要注意的是,此时需要将error_page
指令语句写在最后,否则不能生效。
【参考】
http://www.netingcn.com/nginx-rewrite-flag.html 关于nginx rewrtie的四种flag
https://nginx.org/en/docs/http/ngx_http_rewrite_module.html#rewrite rewrite指令
https://nginx.org/en/docs/http/ngx_http_rewrite_module.html#return return指令
https://nginx.org/en/docs/http/ngx_http_core_module.html#error_page error_page指定
https://en.wikipedia.org/wiki/List_of_HTTP_status_codes 关于497状态码在nginx中的扩展应用
nginx从http跳转到https的更多相关文章
- 使用 Nginx 实现 301 跳转至 https 的根域名
基于 SEO 和安全性的考量,需要进行 301 跳转,以下使用 Nginx 作通用处理 实现结果 需要将以下地址都统一跳转到 https 的根域名 https://chanvinxiao.com ht ...
- Nginx环境中如何将HTTP跳转至HTTPS设置
如果我们VPS服务器的WEB环境采用的是NGINX架构,那如果我们将安装SSL证书的网站希望强制跳转至HTTPS网站URL的时候那需要如何设置呢?这里个人建议是这样的,我们必须要强制一个地址,这样网站 ...
- nginx http跳转到https
server { listen 80; server_name www.888.com; location / { #index.html放在虚拟主机监听的根目录下 root /usr/local/n ...
- Nginx配置http强制跳转到https
目的:访问http://sdk.open.test.com/时强制自动跳转到https://sdk.open.test.com/ 修改nginx站点配置文件sdk.open.test.com.conf ...
- Nginx配置http跳转https访问
Nginx强制http跳转https访问有以下几个方法 nginx的rewrite方法 可以把所有的HTTP请求通过rewrite重写到HTTPS上 配置 方法一 server{ listen ; s ...
- nginx强制使用https访问(http跳转到https)
Nginx 的 Location 从零开始配置 - 市民 - SegmentFault 思否https://segmentfault.com/a/1190000009651161 nginx配置loc ...
- Nginx的https配置记录以及http强制跳转到https的方法梳理
一.Nginx安装(略)安装的时候需要注意加上 --with-http_ssl_module,因为http_ssl_module不属于Nginx的基本模块.Nginx安装方法: 1 2 # ./con ...
- nginx配置http访问自动跳转到https
1.按照如下格式修改nginx.conf 配置文件,80端口会自动转给443端口,这样就强制使用SSL证书加密了.访问http的时候会自动跳转到https上面 server { listen ; se ...
- (转)Nginx的https配置记录以及http强制跳转到https的方法梳理
Nginx的https配置记录以及http强制跳转到https的方法梳理 原文:http://www.cnblogs.com/kevingrace/p/6187072.html 一.Nginx安装(略 ...
随机推荐
- 【刷题】LOJ 556 「Antileaf's Round」咱们去烧菜吧
题目描述 你有 \(m\) 种物品,第 \(i\) 种物品的大小为 \(a_i\) ,数量为 \(b_i\)( \(b_i=0\) 表示有无限个). 你还有 \(n\) 个背包,体积分别为 \(1 ...
- CRT and exlucas
CRT 解同余方程,形如\(x \equiv c_i \ mod \ m_i\),我们对每个方程构造一个解满足: 对于第\(i\)个方程:\(x \equiv 1 \ mod \ m_i\),\(x ...
- 牛客练习赛40 A 小D的剧场 (思维dp)
链接:https://ac.nowcoder.com/acm/contest/369/A 题目描述 若你摘得小的星星 你将得到小的幸福 若你摘得大的星星 你将得到大的财富 若两者都能摘得 你将得到 ...
- maven将项目及第三方jar打成一个jar包
pom.xml中添加如下配置 把依赖包和自己项目的文件打包如同一个jar包(这种方式对spring的项目不支持) <build> <plugins> <plugin> ...
- 如何查看本地电脑ip
1.快捷键 win+R打开命令窗口 输入 ipconfig查看你电脑的ip 2.输入netstat -an ,查看当前所有连接端口,显示所有的有效连接信息列表,包括已建立的连接(ESTABLISHED ...
- 纪中2018暑假培训day1提高b组改题记录
收到意见,认为每天的程序和随笔放在一起写的博客太长了,于是分开整理 day1 模拟赛,看了看提高a组t1的样例就不太想写,于是转而写b组 t1: Description 给定一个n个点m条边的有向图, ...
- A1106. Lowest Price in Supply Chain
A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone invo ...
- Django(十四)分页器(paginator)及自定义分页D
http://www.mamicode.com/info-detail-1724597.html http://www.cnblogs.com/wupeiqi/articles/5246483.htm ...
- 自定义数据类型 typedef
其实就是为数据类型起一个别名. typedef unsigned char AGE; //字符类型AGE x; //等价于 unsigned char x; typedef int * IPointe ...
- 爬虫之requests请求库高级应用
1.SSL Cert Verification #证书验证(大部分网站都是https) import requests respone=requests.get('https://www.12306. ...