詳解如何在Nginx中配置正向代理以及整合Proxy插件
在企業(yè)開(kāi)發(fā)環(huán)境中,局域網(wǎng)內(nèi)的設(shè)備通常需要通過(guò)正向代理服務(wù)器訪(fǎng)問(wèn)互聯(lián)網(wǎng)。正向代理服務(wù)器充當(dāng)中介,幫助客戶(hù)端請(qǐng)求外部資源并返回結(jié)果。局域網(wǎng)內(nèi)也就是我們俗稱(chēng)的內(nèi)網(wǎng),局域網(wǎng)外的互聯(lián)網(wǎng)就是外網(wǎng),在一些特殊場(chǎng)景內(nèi),例如:醫(yī)院。而局域網(wǎng)中的客戶(hù)端要訪(fǎng)問(wèn)這些資源時(shí),就需要通過(guò)代理服務(wù)器。這種通過(guò)代理服務(wù)器訪(fǎng)問(wèn)外部網(wǎng)絡(luò)資源的方式,就是正向代理。
正向代理不僅用于提升訪(fǎng)問(wèn)速度,還能提高網(wǎng)絡(luò)安全性、管理訪(fǎng)問(wèn)權(quán)限和優(yōu)化網(wǎng)絡(luò)流量。在本文中,我們將詳細(xì)介紹如何在Nginx中配置正向代理,以及整合ngx_http_proxy_connect_module 插件。
本次涉及到的所有文件都已經(jīng)打包好(包括已經(jīng)整合ngx_http_proxy_connect_module的Nginx),文末自取,如有需要更高版本,去官網(wǎng)下載編譯即可。
一、概述
正向代理是一種網(wǎng)絡(luò)服務(wù),它充當(dāng)客戶(hù)端與目標(biāo)服務(wù)器之間的中介。其主要功能是代表客戶(hù)端向目標(biāo)服務(wù)器發(fā)出請(qǐng)求并將響應(yīng)結(jié)果返回給客戶(hù)端。正向代理服務(wù)器位于客戶(hù)端和目標(biāo)服務(wù)器之間,用于處理客戶(hù)端的請(qǐng)求并代為訪(fǎng)問(wèn)目標(biāo)服務(wù)器。
在正向代理中,客戶(hù)端必須明確指定要使用的代理服務(wù)器,并且代理服務(wù)器能夠訪(fǎng)問(wèn)目標(biāo)服務(wù)器。這樣,所有請(qǐng)求都會(huì)先發(fā)送到代理服務(wù)器,再由代理服務(wù)器進(jìn)行處理和轉(zhuǎn)發(fā)。
二、正向代理配置
2.1、配置
在nginx.conf
配置文件中,使用 proxy_pass
指令將請(qǐng)求轉(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)聽(tīng)的端口,可以根據(jù)你的實(shí)際情況進(jìn)行修改。resolver
: 指定DNS服務(wù)器IP地址。location / { ... }
:定義了請(qǐng)求的匹配規(guī)則,這里表示匹配所有請(qǐng)求。proxy_pass
: 將請(qǐng)求轉(zhuǎn)發(fā)到具體的服務(wù)。proxy_set_header
:用于設(shè)置轉(zhuǎn)發(fā)請(qǐng)求時(shí)的頭部信息,以便將客戶(hù)端的真實(shí) IP 和協(xié)議信息傳遞給代理服務(wù)器。
2.2、驗(yàn)證
curl http://www.baidu.com -v -x 127.0.0.1:90
測(cè)試結(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去訪(fǎng)問(wèn)目標(biāo)網(wǎng)站
三、使用插件實(shí)現(xiàn)HTTP/HTTPS正向代理
使用到的插件是ngx_http_proxy_connect_module ,它允許 Nginx 作為代理服務(wù)器處理 CONNECT 方法。CONNECT 方法通常用于建立到遠(yuǎn)程服務(wù)器的端到端的加密連接,通常在代理服務(wù)器后面的客戶(hù)端需要通過(guò)代理服務(wù)器與目標(biāo)服務(wù)器建立安全連接,比如 HTTPS 連接。
GitHub地址:
https://github.com/chobits/ngx_http_proxy_connect_module.git
使用這個(gè)插件,意味著需要重新編譯nginx,在編譯的過(guò)程中,將插件添加進(jìn)去。
本次編譯以目前最新穩(wěn)定版1.24.0為例。
四、在CentOS 7上整合Proxy插件
4.1、環(huán)境準(zhǔn)備
1)、創(chuàng)建編譯目錄
mkdir -p /usr/local/apps/nginx
2)、準(zhǔn)備所需的構(gòu)建工具和依賴(lài)項(xiàng)
- 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è)置補(bǔ)丁
選擇正確的補(bǔ)丁進(jìn)行構(gòu)建
nginx 版本 | 啟用重寫(xiě)階段 | 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、測(cè)試
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)、啟動(dòng)
cd /usr/local/nginx/sbin ./nginx
測(cè)試:
curl https://www.baidu.com -v -x 127.0.0.1:91
五、在Windows上編譯Nginx并整合Proxy插件
在Windows中編譯nginx是比較麻煩的,需要安裝一系列的環(huán)境。
nginx官網(wǎng)也有具體的文檔說(shuō)明:
http://nginx.org/en/docs/howto_build_on_win32.html
5.1、環(huán)境準(zhǔn)備
1)、所需安裝包及環(huán)境
Microsoft Visual Studio 2017
MSYS2
Perl
PCRE
zlib
OpenSSL
2)、Nginx源碼下載
下載地址:
http://hg.nginx.org/nginx
在右側(cè)tags
中找到對(duì)應(yīng)的版本,例如:1.24.0
點(diǎn)開(kāi)后,點(diǎn)擊右側(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進(jìn)行配置
將文件解壓至E:\nginx\nginx-release-1.24.0
將下列四個(gè)文件拷貝至E:\nginx\nginx-release-1.24.0
目錄下
openssl-1.1.1m.tar.gz
pcre2-10.39.tar.gz
zlib-1.2.11.tar.gz
ngx_http_proxy_connect_module-master.zip
使用MSYS2
進(jìn)入此文件夾
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í)行如下配置命令,開(kāi)始配置
回到nginx-release-1.24.0
目錄
cd /e/nginx/nginx-release-1.24.0/
設(shè)置補(bǔ)丁
patch -p1 < objs/lib/ngx_http_proxy_connect_module/patch/proxy_connect_rewrite_102101.patch
如果提示
-bash: patch: command not found
,則使用pacman -S patch
安裝patch
命令
開(kāi)始配置
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
配置過(guò)程中,可能會(huì)提示錯(cuò)誤auto/cc/msvs: line 117: [: : integer expression expected]
這個(gè)是MSVC版本識(shí)別錯(cuò)誤導(dǎo)致,直接打開(kāi)auto/cc/msvc文件
注釋說(shuō)明和本地實(shí)際安裝的MSVC編譯器情況,設(shè)置版本號(hào),如下(添加NGX_MSVC_VER=19.00)
5.3、編譯Nginx
使用Microsoft Visual Studio的編譯工具中的適用于 VS 2017 的 x86_x64 兼容工具命令提示
工具進(jìn)行編譯。編譯成功后,會(huì)在objs文件夾
生成nginx.exe
文件。
如果沒(méi)有,則安裝下列組件即可。
進(jìn)入nginx-release-1.24.0
目錄
cd E:\nginx\nginx-release-1.24.0
執(zhí)行命令
nmake
提示sed
錯(cuò)誤NMAKE : fatal error U1077: “sed”: 返回代碼“0x1”
,這個(gè)錯(cuò)誤可以忽略。
可以添加Git下的 \usr\bin 路徑添加到系統(tǒng)環(huán)境變量的Path中解決。
可以看到在objs
文件夾下生成了nginx.exe
文件
5.4、創(chuàng)建完整Nginx
創(chuàng)建完整Nginx
,在MSYS2窗口中執(zhí)行如下命令
# 進(jìn)入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、測(cè)試
配置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; } }
啟動(dòng)nginx
start nginx
測(cè)試:
curl https://www.baidu.com -v -x 127.0.0.1:91
六、整合Proxy插件并創(chuàng)建Nginx Docker鏡像
6.1、編寫(xiě)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、測(cè)試
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; } } }
啟動(dòng)
docker-compose up -d
測(cè)試:
curl https://www.baidu.com -v -x 127.0.0.1:91
6.5、鏡像導(dǎo)入導(dǎo)出
導(dǎo)出
docker save -o nginx-1.24.0-proxy.tar nginx:1.24.0-proxy
導(dǎo)入
docker load < nginx-1.24.0-proxy.tar
以上就是詳解如何在Nginx中配置正向代理以及整合Proxy插件的詳細(xì)內(nèi)容,更多關(guān)于Nginx正向代理與Proxy整合的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Nginx的location的常見(jiàn)規(guī)則優(yōu)先級(jí)問(wèn)題
Nginx是反向代理和負(fù)載均衡的首選工具,nginx的location配置有許多細(xì)節(jié)內(nèi)容在網(wǎng)上不容易找到資料,或者解釋不清。本文對(duì)Nginx location規(guī)則優(yōu)先級(jí)介紹,需要的朋友參考下吧2021-08-08Nginx設(shè)置Access-Control-Allow-Origin多域名跨域?qū)崿F(xiàn)
本文主要介紹了Nginx設(shè)置Access-Control-Allow-Origin多域名跨域?qū)崿F(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-11-11nginx中proxy_set_header參數(shù)的實(shí)現(xiàn)
本文詳細(xì)介紹了Nginx中proxy_set_header指令的用法,通過(guò)設(shè)置不同的請(qǐng)求頭信息,可以實(shí)現(xiàn)更靈活的反向代理功能,具有一定的參考價(jià)值,感興趣的可以了解一下2024-12-12nginx訪(fǎng)問(wèn)動(dòng)態(tài)接口報(bào)錯(cuò)404Not Found問(wèn)題解決
本文主要介紹了nginx訪(fǎng)問(wèn)動(dòng)態(tài)接口報(bào)錯(cuò)404Not Found問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03瀏覽器控制臺(tái)報(bào)錯(cuò)Failed to load module script:解決方
這篇文章主要為大家介紹了瀏覽器控制臺(tái)報(bào)錯(cuò)Failed to load module script:解決方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11三步配置輕量級(jí)服務(wù)器nginx小結(jié)
Nginx是一個(gè)安裝非常的簡(jiǎn)單 , 配置文件非常簡(jiǎn)潔,本文就來(lái)介紹一下三步配置輕量級(jí)服務(wù)器nginx,具有一定的參考價(jià)值,感興趣的可以了解一下2023-08-08nginx代理多次302的解決方法(nginx Follow 302)
這篇文章主要介紹了nginx代理多次302的解決方法(nginx Follow 302),詳細(xì)的介紹了解決方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-12-12Nginx配置出現(xiàn)訪(fǎng)問(wèn)白屏問(wèn)題的原因與解決
這篇文章主要為大家詳細(xì)介紹了Nginx配置出現(xiàn)訪(fǎng)問(wèn)白屏問(wèn)題的原因以及該如何解決,文中的示例代碼簡(jiǎn)潔易懂,有需要的小伙伴可以參考一下2025-02-02