nginx配置https正向代理

  • nginx配置https正向代理已关闭评论
  • 113 次浏览
  • A+
所属分类:linux技术
摘要

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


nginx配置https正向代理

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

 

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

 nginx配置https正向代理

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

yum -y install gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel

上传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"