Nginx配置正向代理 支持HTTP、HTTPS

1. 环境准备
本次试验使用2台linux虚拟机,一台配置成代理服务器,一台作为客户端测试使用,两台主机配置如下:

主机名 外网IP(NAT模式) 内网IP(Hostonly模式) 操作系统版本
nginx-proxy 10.0.0.250/24 172.16.100.250/24 CentOS7.6
client 172.16.100.100/24 CentOS7.6

2. 编译安装Nginx
Nginx本身不支持HTTPS正向代理,需要安装ngx_http_proxy_connect_module模块后才可以支持https正向代理

安装编译环境和工具

# yum -y install pcre pcre-devel 
# yum -y install openssl-devel
# yum -y install gcc make gcc-c++
# yum -y install git
# yum -y install net-tools

编译安装Nginx和ngx_http_proxy_connect_module模块

# mkdir -p /server/tools
# cd /server/tools
# git clone https://github.com/chobits/ngx_http_proxy_connect_module.git
# wget http://nginx.org/download/nginx-1.18.0.tar.gz
# tar -xf nginx-1.18.0.tar.gz
# mkdir /app
# cd nginx-1.18.0
# patch -p1 < /server/tools/ngx_http_proxy_connect_module/patch/proxy_connect_rewrite_101514.patch
# ./configure --prefix=/app/nginx-1.18.0 --with-http_stub_status_module --with-http_ssl_module --add-module=/server/tools/ngx_http_proxy_connect_module
# make 
# make install
# cd /app
# ln -sv /app/nginx-1.18.0 /app/nginx
# echo > /etc/profile.d/nginx.sh
# sed -i 'i export PATH=$PATH:/app/nginx/sbin'  nginx.sh
# source /etc/profile.d/nginx.sh

3修改Nginx配置文件,配置nginx支持http,https代理

# cp /app/nginx/conf/nginx.conf{,.bak}
# 
server {
        listen 8099;
        resolver 223.5.5.5;
        location / {
                proxy_pass http://$http_host$request_uri;
                proxy_set_header HOST $http_host;
                proxy_buffers 256 4k;
                proxy_max_temp_file_size 0k;
                proxy_connect_timeout 60s;
                proxy_send_timeout 60s;
                proxy_read_timeout 60s;
                proxy_next_upstream error timeout invalid_header http_502;

        }

}
# 支持https代理
server {
      listen 8443;
        resolver 223.5.5.5;
        proxy_connect;
        proxy_connect_allow 443 563;
        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;
        }
}

4.启动 nginx

# 检查配置文件是否正确
# nginx -t  
#启动nginx 
# nginx 

# 检查服务端口
# netstat -tunlp | grep 8099
# netstat -tunlp | grep 8443

5.在客户端配置使用代理上网

#临时测试
# curl --proxy  172.16.100.250:8099 http://www.baidu.com
# curl --proxy 172.16.100.250:8443 https://www.baidu.com

永久生效
# echo > /etc/profile.d/http_proxy.sh
# sed -i 'i export http_proxy=http://172.16.100.250:8099\nexport https_proxy=http://172.16.100.250:8443' /etc/profile.d/http_proxy.sh
# source /etc/profile.d/http_proxy.sh
# 测试是否生效
# curl -v https://www.baidu.com
# curl -v http://www.baidu.com
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章