Nginx正向代理

2023-03-13 15:57:57 浏览数 (1)

  • 记一次使用Nginx做正向代理,实现http与https的代理访问

1.环境介绍

  • 操作系统:CentOS Linux release 7.6.1810 (Core)
  • Nginx编译安装,版本-nginx/1.18.0
  • 服务器A部署在DMZ区域且可上网,服务器B不可上网,但是和服务器A能够通信
  • 在服务器A上部署Nginx正向代理,使服务器B通过服务器A正向代理功能可访问http和https
  • Nginx默认不支持https的正向代理,这里需要安装第三方模块“ngx_http_proxy_connect_module”
    • 模块参考https://github.com/chobits/ngx_http_proxy_connect_module
  • 服务器B --> 服务器A --> 外网

server

ip

用途

DMZ区可上网服务器-A

192.168.252.247

Nginx代理服务器-nginx/1.18.0

内部网络不可上网服务器-B

192.168.10.247

内网服务器

2.安装依赖环境(192.168.252.247)

代码语言:javascript复制
yum install gcc gcc-c   autoconf automake -y
yum install zlib zlib-devel -y
yum install pcre pcre-devel -y
yum install openssl openssl-devel -y
yum install patch -y
yum install git -y

3.安装Nginx及第三方模块(192.168.252.247)

代码语言:javascript复制
#进入src目录
cd /usr/local/src
#下载nginx源码包
wget http://nginx.org/download/nginx-1.18.0.tar.gz
#克隆第三方模块项目到本地
git clone https://github.com/chobits/ngx_http_proxy_connect_module.git
#解压nginx源码包
tar zxvf nginx-1.18.0.tar.gz
#进入解压后的nginx目录
cd nginx-1.18.0
#根据官方文档对第三方模块进行处理(patch一定要在 nginx 解压目录里面执行)
patch -p1 < /usr/local/src/ngx_http_proxy_connect_module/patch/proxy_connect_rewrite_101504.patch
#配置nginx参数
./configure --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --with-http_stub_status_module --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-stream_ssl_preread_module --with-http_addition_module --with-http_gunzip_module --with-http_gzip_static_module   --with-stream_ssl_module  --add-module=/usr/local/src/ngx_http_proxy_connect_module
#编译
make
#编译后的安装
make  install

4.修改nginx配置文件参数(192.168.252.247)

代码语言:javascript复制
#新建正向代理配置文件
touch /etc/nginx/conf.d/proxy_agent.conf
cat /etc/nginx/conf.d/proxy_agent.conf
server {
    resolver 114.114.114.114;
    listen 3128;
    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 30;
        proxy_send_timeout 60;
        proxy_read_timeout 60;
        proxy_next_upstream error timeout invalid_header http_502;
    }
}
server {
     listen                         8443;
     resolver                       114.114.114.114;
     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;
     }
}

5.启动Nginx(192.168.252.247)

代码语言:javascript复制
#启动
nginx
#停止
nginx -s stop
#重载配置文件
nginx -s reload

6.查看listen端口是否启动

代码语言:javascript复制
netstat -nutpl
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:3128            0.0.0.0:*               LISTEN      7443/nginx: master  
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      7443/nginx: master  
tcp        0      0 0.0.0.0:8443            0.0.0.0:*               LISTEN      7443/nginx: master

7.配置防火墙规则

代码语言:javascript复制
firewall-cmd --zone=public --add-port=3128/tcp --permanent
firewall-cmd --zone=public --add-port=8443/tcp --permanent
firewall-cmd --reload
#查看防火墙规则是否生效
firewall-cmd --list-ports
3128/tcp 8443/tcp 80/tcp

8.配置代理(192.168.10.247)

代码语言:javascript复制
#在环境变量里面配置代理服务器地址和ip
#在profile文件末尾追加如下内容
vim /etc/profile
#正向代理服务器http的IP与端口
export http_proxy=192.168.252.247:3128
#正向代理服务器https的IP与端口
export https_proxy=192.168.252.247:8443
#刷新环境变量
source /etc/profile

9.测试正向代理功能是否可用(192.168.10.247)

代码语言:javascript复制
#测试 http 代理
curl -i http://www.baidu.com/
HTTP/1.1 200 OK  #返回状态码正常,成功
Server: nginx/1.18.0
Date: Wed, 23 Sep 2020 05:41:19 GMT
Content-Type: text/html
Content-Length: 2381
Connection: keep-alive
Accept-Ranges: bytes
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Etag: "588604c8-94d"
Last-Modified: Mon, 23 Jan 2017 13:27:36 GMT
Pragma: no-cache
Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/

#测试 https 代理
HTTP/1.1 200 Connection Established
Proxy-agent: nginx
HTTP/1.1 200 OK  #返回状态码正常,成功
Accept-Ranges: bytes
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Connection: keep-alive
Content-Length: 2443
Content-Type: text/html
Date: Wed, 23 Sep 2020 05:42:46 GMT
Etag: "588603eb-98b"
Last-Modified: Mon, 23 Jan 2017 13:23:55 GMT
Pragma: no-cache
Server: bfe/1.0.8.18
Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/

0 人点赞