网站首页 > 技术教程 正文
通常情况下,Nginx常用于代理(正向、反向)HTTP请求,最近碰到有项目需求Nginx反向代理HTTPS后端,因为想验证一下Nginx是否正反向均支持HTTPS代理。
测试环境:
- CentOS7.3_x64
- nginx-1.14.0
§Nginx HTTPS正向代理测试
§1. 正向代理配置
forward_proxy.conf
server {
# 配置DNS解析IP地址,以及超时时间(5秒),正向代理配置不能有server_name参数
resolver 114.114.114.114; # 必需
resolver_timeout 5s;
# 监听端口,必需
listen 8080;
access_log /var/log/nginx/forward_proxy.access.log;
error_log /var/log/nginx/forward_proxy.error.log;
location / {
# 配置正向代理参数,必需
proxy_pass $scheme://$host$request_uri;
# 解决如果URL中带"."后Nginx 503错误
proxy_set_header Host $http_host;
# 配置缓存大小
proxy_buffers 256 4k;
# 关闭磁盘缓存读写减少I/O
proxy_max_temp_file_size 0;
# 代理连接超时时间
proxy_connect_timeout 30;
# 配置代理服务器HTTP状态缓存时间
proxy_cache_valid 200 302 10m;
proxy_cache_valid 301 1h;
proxy_cache_valid any 1m;
}
重载nginx
/usr/sbin/nginx -t systemctl reload nginx
§2. 测试验证
设置linux客户端http/https代理(此次测试在同一台机器)或在使用curl时带–proxy参数指定代理地址,永久生效可写入配置文件(/etc/profile或.bashrc等地方)
export http_proxy=http://127.0.0.1:8080 export https_proxy=http://127.0.0.1:8080
- 测试http
root@centos-vm1 ~]# curl http://www.baidu.com <!DOCTYPE html> <!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body ............后略
同时实时查看nginx访问日志
127.0.0.1 - - [16/Nov/2018:10:36:23 +0800] "GET http://www.baidu.com/ HTTP/1.1" 200 2381 "-" "curl/7.29.0"
- 测试https
[root@centos-vm1 ~]# curl https://www.baidu.com curl: (56) Received HTTP code 400 from proxy after CONNECT
查看nginx访问日志
127.0.0.1 - - [16/Nov/2018:10:38:42 +0800] "CONNECT www.baidu.com:443 HTTP/1.1" 400 166 "-" "-"
由测试结果可知:nginx正向代理不支持https
§Nginx HTTPS反向代理测试
§1. 反向代理配置
reverse_proxy.conf
server {
listen 8081;
access_log /var/log/nginx/reverse_proxy.access.log;
error_log /var/log/nginx/reverse_proxy.error.log;
location / {
proxy_pass https://www.baidu.com; # test.local为本地正常的服务地址,域名已加入到/etc/hosts解析。
# 保留原始客户端地址
proxy_set_header X-Real-IP $remote_addr;
# 把请求头中的X-Forwarded-For与$remote_addr用逗号合起来,如果请求头中没有X-Forwarded-For则$proxy_add_x_forwarded_for为$remote_addr
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# 请求头设置传输协议
proxy_set_header X-Forwarded-Proto $scheme;
}
}
§2. 测试验证
- 测试https(client为本机)
想要通过nginx反向代理访问https://www.baidu.com:
[root@centos-vm1 conf.d]# curl http://127.0.0.1:8081 <!DOCTYPE html> <!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body ............后略
查看nginx日志
127.0.0.1 - - [16/Nov/2018:12:27:44 +0800] "GET / HTTP/1.1" 302 239 "-" "curl/7.29.0"
即和正常访问相同
由此可知,nginx反向代理支持后端https
猜你喜欢
- 2024-10-11 部署支持 https 的 Nginx 服务(nginx配置https证书)
- 2024-10-11 nginx优化https(ocsp)(nginx配置生成https)
- 2024-10-11 Nginx配置HTTPS(nginx配置带重定向的反向代理)
- 2024-10-11 Linux下配置Nginx并使用https协议
- 2024-10-11 超详解:HTTPS及配置Django+HTTPS开发环境
- 2024-10-11 如何使网站支持https访问?nginx配置https证书
- 2024-09-21 搭建Nginx(Https)+Tomcat实现负载均衡且Https自发证书配置
- 2024-09-21 「python」「nginx」「https」 Nginx 服务器 SSL 证书安装部署
- 2024-09-21 Nginx配置https实现加密认证(https加密证书)
- 2024-09-21 Nginx部署ssl证书开启https简要教程
欢迎 你 发表评论:
- 最近发表
- 标签列表
-
- 下划线是什么 (87)
- 精美网站 (58)
- qq登录界面 (90)
- nginx 命令 (82)
- nginx .http (73)
- nginx lua (70)
- nginx 重定向 (68)
- Nginx超时 (65)
- nginx 监控 (57)
- odbc (59)
- rar密码破解工具 (62)
- annotation (71)
- 红黑树 (57)
- 智力题 (62)
- php空间申请 (61)
- 按键精灵 注册码 (69)
- 软件测试报告 (59)
- ntcreatefile (64)
- 闪动文字 (56)
- guid (66)
- abap (63)
- mpeg 2 (65)
- column (63)
- dreamweaver教程 (57)
- excel行列转换 (56)

本文暂时没有评论,来添加一个吧(●'◡'●)