server | ip | 用途 |
---|---|---|
DMZ区可上网服务器-A | 192.168.252.247 | Nginx代理服务器-nginx/1.18.0 |
内部网络不可上网服务器-B | 192.168.10.247 | 内网服务器 |
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
#进入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
#新建正向代理配置文件
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;
}
}
#启动
nginx
#停止
nginx -s stop
#重载配置文件
nginx -s reload
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
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
#在环境变量里面配置代理服务器地址和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
#测试 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=/