詳解如何在Nginx中配置正向代理以及整合Proxy插件
在企業(yè)開發(fā)環(huán)境中,局域網(wǎng)內(nèi)的設(shè)備通常需要通過正向代理服務(wù)器訪問互聯(lián)網(wǎng)。正向代理服務(wù)器充當中介,幫助客戶端請求外部資源并返回結(jié)果。局域網(wǎng)內(nèi)也就是我們俗稱的內(nèi)網(wǎng),局域網(wǎng)外的互聯(lián)網(wǎng)就是外網(wǎng),在一些特殊場景內(nèi),例如:醫(yī)院。而局域網(wǎng)中的客戶端要訪問這些資源時,就需要通過代理服務(wù)器。這種通過代理服務(wù)器訪問外部網(wǎng)絡(luò)資源的方式,就是正向代理。
正向代理不僅用于提升訪問速度,還能提高網(wǎng)絡(luò)安全性、管理訪問權(quán)限和優(yōu)化網(wǎng)絡(luò)流量。在本文中,我們將詳細介紹如何在Nginx中配置正向代理,以及整合ngx_http_proxy_connect_module 插件。
本次涉及到的所有文件都已經(jīng)打包好(包括已經(jīng)整合ngx_http_proxy_connect_module的Nginx),文末自取,如有需要更高版本,去官網(wǎng)下載編譯即可。
一、概述

正向代理是一種網(wǎng)絡(luò)服務(wù),它充當客戶端與目標服務(wù)器之間的中介。其主要功能是代表客戶端向目標服務(wù)器發(fā)出請求并將響應(yīng)結(jié)果返回給客戶端。正向代理服務(wù)器位于客戶端和目標服務(wù)器之間,用于處理客戶端的請求并代為訪問目標服務(wù)器。
在正向代理中,客戶端必須明確指定要使用的代理服務(wù)器,并且代理服務(wù)器能夠訪問目標服務(wù)器。這樣,所有請求都會先發(fā)送到代理服務(wù)器,再由代理服務(wù)器進行處理和轉(zhuǎn)發(fā)。
二、正向代理配置
2.1、配置
在nginx.conf配置文件中,使用 proxy_pass 指令將請求轉(zhuǎn)發(fā)到代理服務(wù)器
server {
listen 90;
resolver 114.114.114.114;
location / {
proxy_pass $scheme://$host$request_uri;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
listen: 指定了監(jiān)聽的端口,可以根據(jù)你的實際情況進行修改。resolver: 指定DNS服務(wù)器IP地址。location / { ... }:定義了請求的匹配規(guī)則,這里表示匹配所有請求。proxy_pass: 將請求轉(zhuǎn)發(fā)到具體的服務(wù)。proxy_set_header:用于設(shè)置轉(zhuǎn)發(fā)請求時的頭部信息,以便將客戶端的真實 IP 和協(xié)議信息傳遞給代理服務(wù)器。
2.2、驗證
curl http://www.baidu.com -v -x 127.0.0.1:90
測試結(jié)果
$ curl http://www.baidu.com -v -x 127.0.0.1:90 * Rebuilt URL to: http://www.baidu.com/ * Trying 127.0.0.1... * TCP_NODELAY set * Connected to 127.0.0.1 (127.0.0.1) port 90 (#0) > GET http://www.baidu.com/ HTTP/1.1 > Host: www.baidu.com > User-Agent: curl/7.55.1 > Accept: */* > Proxy-Connection: Keep-Alive > < HTTP/1.1 200 OK < Server: nginx/1.20.1 < Date: Sat, 27 Jan 2024 01:47:22 GMT < Content-TypeControl: private, no-cache, no-store, proxy-revalidate, no-transform < Etag: "588604dc-94d" < Last-Modified: Mon, 23 Jan 2017 13:27:56 GMT < Pragma: no-cache < Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/
注意:這種代理方式只能使用http去訪問目標網(wǎng)站
三、使用插件實現(xiàn)HTTP/HTTPS正向代理
使用到的插件是ngx_http_proxy_connect_module ,它允許 Nginx 作為代理服務(wù)器處理 CONNECT 方法。CONNECT 方法通常用于建立到遠程服務(wù)器的端到端的加密連接,通常在代理服務(wù)器后面的客戶端需要通過代理服務(wù)器與目標服務(wù)器建立安全連接,比如 HTTPS 連接。
GitHub地址:
https://github.com/chobits/ngx_http_proxy_connect_module.git
使用這個插件,意味著需要重新編譯nginx,在編譯的過程中,將插件添加進去。
本次編譯以目前最新穩(wěn)定版1.24.0為例。
四、在CentOS 7上整合Proxy插件
4.1、環(huán)境準備
1)、創(chuàng)建編譯目錄
mkdir -p /usr/local/apps/nginx
2)、準備所需的構(gòu)建工具和依賴項
- openssl-1.1.1m
- pcre2-10.39
- zlib-1.2.11
將文件上傳至/usr/local/apps/nginx/目錄下并解壓
cd /usr/local/apps/nginx
tar -zxvf openssl-1.1.1m.tar.gz tar -zxvf pcre2-10.39.tar.gz tar -zxvf zlib-1.2.11.tar.gz
3)、上傳 Nginx 源代碼
下載地址:
https://nginx.org/download/nginx-1.24.0.tar.gz
上傳至/usr/local/apps/nginx目錄下,解壓
cd /usr/local/apps/nginx tar -zxvf nginx-1.24.0.tar.gz
4)、下載ngx_http_proxy_connect_module源碼
上傳至/usr/local/apps/nginx目錄下,解壓
unzip ngx_http_proxy_connect_module-master.zip mv ngx_http_proxy_connect_module-master ngx_http_proxy_connect_module
4.2、配置Nginx
1)、設(shè)置補丁
選擇正確的補丁進行構(gòu)建
| nginx 版本 | 啟用重寫階段 | patch |
|---|---|---|
| 1.4.x ~ 1.12.x | NO | proxy_connect.patch |
| 1.4.x ~ 1.12.x | YES | proxy_connect_rewrite.patch |
| 1.13.x ~ 1.14.x | NO | proxy_connect_1014.patch |
| 1.13.x ~ 1.14.x | YES | proxy_connect_rewrite_1014.patch |
| 1.15.2 | YES | proxy_connect_rewrite_1015.patch |
| 1.15.4 ~ 1.16.x | YES | proxy_connect_rewrite_101504.patch |
| 1.17.x ~ 1.18.x | YES | proxy_connect_rewrite_1018.patch |
| 1.19.x ~ 1.21.0 | YES | proxy_connect_rewrite_1018.patch |
| 1.21.1 ~ 1.22.x | YES | proxy_connect_rewrite_102101.patch |
| 1.23.x ~ 1.24.0 | YES | proxy_connect_rewrite_102101.patch |
| 1.25.0 ~ 1.25.x | YES | proxy_connect_rewrite_102101.patch |
本次版本為1.24.0,則使用如下命令:
cd /usr/local/apps/nginx/nginx-1.24.0
patch -p1 < /usr/local/apps/nginx/ngx_http_proxy_connect_module/patch/proxy_connect_rewrite_102101.patch
2)、配置編譯參數(shù)
cd /usr/local/apps/nginx/nginx-1.24.0
./configure \ --prefix=/usr/local/nginx \ --with-openssl=../openssl-1.1.1m \ --with-pcre=../pcre2-10.39 \ --with-pcre-jit \ --with-zlib=../zlib-1.2.11 \ --with-http_ssl_module \ --with-http_v2_module \ --with-http_gzip_static_module \ --with-http_flv_module \ --with-http_stub_status_module \ --with-stream \ --add-module=/usr/local/apps/nginx/ngx_http_proxy_connect_module
--prefix:指定需要安裝的目錄。
4.3、編譯Nginx
編譯并安裝 Nginx
make && make install
4.4、測試
1)、配置
編輯配置文件:
vi /usr/local/nginx/conf/nginx.conf
增加如下配置:
server {
listen 91;
resolver 114.114.114.114 ipv6=off;
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 $scheme://$host$request_uri;
proxy_set_header Host $host:$server_port;
}
}
2)、啟動
cd /usr/local/nginx/sbin ./nginx
測試:
curl https://www.baidu.com -v -x 127.0.0.1:91
五、在Windows上編譯Nginx并整合Proxy插件
在Windows中編譯nginx是比較麻煩的,需要安裝一系列的環(huán)境。
nginx官網(wǎng)也有具體的文檔說明:
http://nginx.org/en/docs/howto_build_on_win32.html
5.1、環(huán)境準備
1)、所需安裝包及環(huán)境
Microsoft Visual Studio 2017MSYS2PerlPCREzlibOpenSSL

2)、Nginx源碼下載
下載地址:
http://hg.nginx.org/nginx
在右側(cè)tags中找到對應(yīng)的版本,例如:1.24.0
點開后,點擊右側(cè)zip即可下載。附上完整下載地址:
http://hg.nginx.org/nginx/archive/release-1.24.0.zip
3)、Microsoft Visual Studio 2017 安裝
提供安裝包visual_studio_community_2017_version_15.3.exe,自行安裝
4)、MSYS2 安裝
提供安裝包msys2-x86_64-20230127.exe,自行安裝
官網(wǎng):
https://www.msys2.org/
5)、Perl 安裝
提供安裝包strawberry-perl-5.32.1.1-64bit.msi,自行安裝
5.2、配置Nginx
需使用MSYS2進行配置
將文件解壓至E:\nginx\nginx-release-1.24.0
將下列四個文件拷貝至E:\nginx\nginx-release-1.24.0目錄下
openssl-1.1.1m.tar.gzpcre2-10.39.tar.gzzlib-1.2.11.tar.gzngx_http_proxy_connect_module-master.zip

使用MSYS2進入此文件夾
cd /e/nginx/nginx-release-1.24.0/

在MSYS2窗口執(zhí)行如下命令
mkdir -p objs/lib
cd objs/lib tar -zxf ../../pcre2-10.39.tar.gz tar -zxf ../../zlib-1.2.11.tar.gz tar -zxf ../../openssl-1.1.1m.tar.gz

解壓ngx_http_proxy_connect_module-master.zip
unzip ../../ngx_http_proxy_connect_module-master.zip mv ngx_http_proxy_connect_module-master ngx_http_proxy_connect_module
如果提示-bash: unzip: command not found,則使用pacman -S unzip安裝unzip命令
執(zhí)行如下配置命令,開始配置
回到nginx-release-1.24.0目錄
cd /e/nginx/nginx-release-1.24.0/
設(shè)置補丁
patch -p1 < objs/lib/ngx_http_proxy_connect_module/patch/proxy_connect_rewrite_102101.patch
如果提示
-bash: patch: command not found,則使用pacman -S patch安裝patch命令
開始配置
auto/configure \
--with-cc=cl \
--with-debug \
--prefix= \
--conf-path=conf/nginx.conf \
--pid-path=logs/nginx.pid \
--http-log-path=logs/access.log \
--error-log-path=logs/error.log \
--sbin-path=nginx.exe \
--http-client-body-temp-path=temp/client_body_temp \
--http-proxy-temp-path=temp/proxy_temp \
--http-fastcgi-temp-path=temp/fastcgi_temp \
--http-scgi-temp-path=temp/scgi_temp \
--http-uwsgi-temp-path=temp/uwsgi_temp \
--with-cc-opt=-DFD_SETSIZE=1024 \
--with-pcre=objs/lib/pcre2-10.39 \
--with-zlib=objs/lib/zlib-1.2.11 \
--with-openssl=objs/lib/openssl-1.1.1m \
--with-openssl-opt=no-asm \
--with-http_ssl_module \
--with-stream \
--add-module=objs/lib/ngx_http_proxy_connect_module
配置過程中,可能會提示錯誤auto/cc/msvs: line 117: [: : integer expression expected]
這個是MSVC版本識別錯誤導致,直接打開auto/cc/msvc文件
注釋說明和本地實際安裝的MSVC編譯器情況,設(shè)置版本號,如下(添加NGX_MSVC_VER=19.00)

5.3、編譯Nginx
使用Microsoft Visual Studio的編譯工具中的適用于 VS 2017 的 x86_x64 兼容工具命令提示工具進行編譯。編譯成功后,會在objs文件夾生成nginx.exe文件。

如果沒有,則安裝下列組件即可。

進入nginx-release-1.24.0目錄
cd E:\nginx\nginx-release-1.24.0
執(zhí)行命令
nmake

提示sed錯誤NMAKE : fatal error U1077: “sed”: 返回代碼“0x1”,這個錯誤可以忽略。
可以添加Git下的 \usr\bin 路徑添加到系統(tǒng)環(huán)境變量的Path中解決。

可以看到在objs文件夾下生成了nginx.exe文件

5.4、創(chuàng)建完整Nginx
創(chuàng)建完整Nginx,在MSYS2窗口中執(zhí)行如下命令
# 進入nginx目錄 cd /e/nginx # 創(chuàng)建nginx-1.24.0-proxy目錄 mkdir nginx-1.24.0-proxy cd nginx-1.24.0-proxy # 拷貝 cp -r ../nginx-release-1.24.0/conf . cp -r ../nginx-release-1.24.0/contrib . cp -r ../nginx-release-1.24.0/docs . cp ../nginx-release-1.24.0/objs/nginx.exe . # 創(chuàng)建logs、temp目錄 mkdir logs mkdir temp
5.5、測試
配置nginx.conf,增加如下配置:
server {
listen 91;
resolver 114.114.114.114 ipv6=off;
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 $scheme://$host$request_uri;
proxy_set_header Host $host:$server_port;
}
}
啟動nginx
start nginx
測試:
curl https://www.baidu.com -v -x 127.0.0.1:91
六、整合Proxy插件并創(chuàng)建Nginx Docker鏡像
6.1、編寫Dockerfile
FROM alpine:3.17.4 RUN mkdir /data && cd /data WORKDIR /data RUN wget http://nginx.org/download/nginx-1.24.0.tar.gz RUN apk add git && git clone https://github.com/chobits/ngx_http_proxy_connect_module.git nginx_proxy RUN tar -zxvf nginx-1.24.0.tar.gz && rm -f nginx-1.24.0.tar.gz WORKDIR /data/nginx-1.24.0 RUN apk add patch && patch -p1 < /data/nginx_proxy/patch/proxy_connect_rewrite_102101.patch \ && apk add gcc g++ linux-headers pcre-dev openssl-dev zlib-dev make \ && mkdir -p /var/cache/nginx && ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules \ --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid \ --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp \ --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \ --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp \ --with-compat --with-file-aio --with-threads --with-http_addition_module \ --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module \ --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module \ --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module \ --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream \ --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module \ --with-cc-opt='-g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC' \ --with-ld-opt='-Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pie' --add-module=/data/nginx_proxy \ && make -j2 && make install && rm -rf /data/nginx_proxy CMD ["nginx", "-g", "daemon off;"]
6.2、構(gòu)建鏡像
執(zhí)行命令
docker build -t nginx:1.24.0-proxy .
6.3、docker-compose.yml配置
version: '3.1'
services:
# nginx
nginx:
image: nginx:1.24.0-proxy
container_name: nginx
restart: always
volumes:
- ./conf/nginx.conf:/etc/nginx/nginx.conf
- ./html:/usr/share/nginx/html
- ./logs:/var/log/nginx
environment:
TZ: Asia/Shanghai
network_mode: host
6.4、測試
vi conf/nginx.conf
nginx.conf配置如下:
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 91;
resolver 114.114.114.114 ipv6=off;
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 $scheme://$host$request_uri;
proxy_set_header Host $host:$server_port;
}
}
}
啟動
docker-compose up -d
測試:
curl https://www.baidu.com -v -x 127.0.0.1:91
6.5、鏡像導入導出
導出
docker save -o nginx-1.24.0-proxy.tar nginx:1.24.0-proxy
導入
docker load < nginx-1.24.0-proxy.tar
以上就是詳解如何在Nginx中配置正向代理以及整合Proxy插件的詳細內(nèi)容,更多關(guān)于Nginx正向代理與Proxy整合的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Nginx的location的常見規(guī)則優(yōu)先級問題
Nginx是反向代理和負載均衡的首選工具,nginx的location配置有許多細節(jié)內(nèi)容在網(wǎng)上不容易找到資料,或者解釋不清。本文對Nginx location規(guī)則優(yōu)先級介紹,需要的朋友參考下吧2021-08-08
Nginx設(shè)置Access-Control-Allow-Origin多域名跨域?qū)崿F(xiàn)
本文主要介紹了Nginx設(shè)置Access-Control-Allow-Origin多域名跨域?qū)崿F(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2024-11-11
nginx中proxy_set_header參數(shù)的實現(xiàn)
本文詳細介紹了Nginx中proxy_set_header指令的用法,通過設(shè)置不同的請求頭信息,可以實現(xiàn)更靈活的反向代理功能,具有一定的參考價值,感興趣的可以了解一下2024-12-12
nginx訪問動態(tài)接口報錯404Not Found問題解決
本文主要介紹了nginx訪問動態(tài)接口報錯404Not Found問題解決,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-03-03
瀏覽器控制臺報錯Failed to load module script:解決方
這篇文章主要為大家介紹了瀏覽器控制臺報錯Failed to load module script:解決方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11
nginx代理多次302的解決方法(nginx Follow 302)
這篇文章主要介紹了nginx代理多次302的解決方法(nginx Follow 302),詳細的介紹了解決方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12

