nginx配置https正向代理

nginx可以作为http的正向代理服务器,但是不能用做https的正向代理服务器。因为http正向代理使用的是get请求,但是https使用的确实connect请求,而nginx不支持connect请求。所以需要第三方模块ngx_http_proxy_connect_module 来支持https的正向代理。

 

注:NGINX需部署在可以上网的服务器上,请严格按照下图安装和nginx版本匹配的模块,否则会导致https代理不生效。

 

一、安装相关编译环境及相关依赖:

yum -y install gcc gcc-c++

 

上传nginx安装包并解压:

# tar zxvf nginx-1.22.0.tar.gz

# cd nginx-1.22.0

 

二、加载ngx_http_proxy_connect_module模块,安装nginx

下载ngx_http_proxy_connect_module-master.zip文件

github下载地址:https://github.com/chobits/ngx_http_proxy_connect_module.git

 

# unzip ngx_http_proxy_connect_module-master.zip

#cd /usr/local/nginx

#sudo patch -p1 < /root/nginx/ngx_http_proxy_connect_module-master/patch/proxy_connect_rewrite_102101.patch

 

# ./configure –prefix=/usr/local/nginx –with-http_ssl_module –with-http_realip_module –with-stream –with-stream_ssl_module –add-module=/root/ngx_http_proxy_connect_module-master

# make && make install

三、NGINX增加全局代理配置

server {

     listen                         8088;                #对外服务端口

     resolver                       114.114.114.114;     #域名解析服务器

     proxy_connect;

     proxy_connect_allow            all;

     proxy_connect_connect_timeout  10s;

     proxy_connect_read_timeout     10s;

     proxy_connect_send_timeout     10s;

     location / {

         proxy_pass http://$host;

         proxy_set_header Host $host;

     }

}

 

/usr/local/nginx/sbin/nginx        #启动nginx

 

四、在没有外网的机器配置全局代理用户变量

针对各别用户代理可编辑对应的 ~/.bashrc文件,针对全局用户可编辑/etc/profile文件!

http_proxy=192.168.251.5:8088    #NGINX服务器与端口

https_proxy=192.168.251.5:8088

ftp_proxy=192.168.251.5:8088

export http_proxy https_proxy ftp_proxy

 

#source ~/.bashrc   或者  source /etc/profile

 

五、测试正向代理是否生效

在无法上网的机器执行以下命令

curl https://www.baidu.com

查看nginx日志

tail -f /usr/local/nginx/logs/access.log

出现以下内容则表示全局正向代理配置完成

“CONNECT www.baidu.com:443 HTTP/1.1” 200 7198 “-” “curl/7.29.0”

原文地址:http://www.cnblogs.com/LuLu-0904/p/16822009.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性