nginx正向代理的配置和使用教程
- 正向代理,指的是通過(guò)代理服務(wù)器 代理瀏覽器/客戶端去重定向請(qǐng)求訪問(wèn)到目標(biāo)服務(wù)器 的一種代理服務(wù)。正向代理服務(wù)的特點(diǎn)是代理服務(wù)器 代理的對(duì)象是瀏覽器/客戶端,也就是對(duì)于目標(biāo)服務(wù)器 來(lái)說(shuō)瀏覽器/客戶端是隱藏的。
- nginx默認(rèn)支持正向代理http,不支持https
- nginx官方并不支持直接轉(zhuǎn)發(fā)https請(qǐng)求,nginx支持https需要ngx_http_proxy_connect_module模塊。github上開(kāi)源了模塊 https://github.com/chobits/ngx_http_proxy_connect_module。不過(guò)維護(hù)的ngx_http_proxy_connect_module模塊的補(bǔ)丁也是有nginx版本限制的,需根據(jù)自身使用的nginx版本選擇相應(yīng)的正向代理模塊??梢栽赗EDEME.md的Select patch中查看nginx版本和模塊的對(duì)應(yīng)關(guān)系
安裝包準(zhǔn)備
下載nginx安裝包
下載正向代理模塊的包
版本與模塊對(duì)照表
部署nginx服務(wù)
此處使用的是nginx-1.20.2,對(duì)應(yīng)proxy_connect_rewrite_1018.patch
上傳nginx包和正向模塊包
mkdir /nginx cd /nginx [root@mysql nginx]# ll -rw-r--r-- 1 root root 1062124 Feb 12 15:23 nginx-1.20.2.tar.gz -rw-r--r-- 1 root root 57926 Feb 12 15:23 ngx_http_proxy_connect_module-master.zip
解壓,改名
tar -xf nginx.tar.gz unzip ngx_http_proxy_connect_module-master.zip ll drwxr-xr-x 9 1001 1001 4096 Feb 12 15:27 nginx-1.20.2 -rw-r--r-- 1 root root 1062124 Feb 12 15:23 nginx-1.20.2.tar.gz drwxr-xr-x 5 root root 4096 Feb 9 16:54 ngx_http_proxy_connect_module-master -rw-r--r-- 1 root root 57926 Feb 12 15:23 ngx_http_proxy_connect_module-master.zip mv ngx_http_proxy_connect_module-master ngx_http_proxy_connect_module ll drwxr-xr-x 9 1001 1001 4096 Feb 12 15:27 nginx-1.20.2 -rw-r--r-- 1 root root 1062124 Feb 12 15:23 nginx-1.20.2.tar.gz drwxr-xr-x 5 root root 4096 Feb 9 16:54 ngx_http_proxy_connect_module -rw-r--r-- 1 root root 57926 Feb 12 15:23 ngx_http_proxy_connect_module-master.zip
安裝nginx
yum -y install make gcc openssl openssl-devel pcre-devel zlib zlib-devel cd nginx-1.20.2 ll drwxr-xr-x 6 1001 1001 4096 Feb 12 15:20 auto -rw-r--r-- 1 1001 1001 312251 Nov 16 2021 CHANGES -rw-r--r-- 1 1001 1001 476577 Nov 16 2021 CHANGES.ru drwxr-xr-x 2 1001 1001 4096 Feb 12 15:20 conf -rwxr-xr-x 1 1001 1001 2590 Nov 16 2021 configure drwxr-xr-x 4 1001 1001 4096 Feb 12 15:20 contrib drwxr-xr-x 2 1001 1001 4096 Feb 12 15:20 html -rw-r--r-- 1 1001 1001 1397 Nov 16 2021 LICENSE drwxr-xr-x 2 1001 1001 4096 Feb 12 15:20 man -rw-r--r-- 1 1001 1001 49 Nov 16 2021 README drwxr-xr-x 9 1001 1001 4096 Feb 12 15:20 src # 查看正向代理模塊proxy_connect_rewrite_1018.patch的位置 ll ../ngx_http_proxy_connect_module/patch/ -rw-r--r-- 1 root root 9849 Feb 9 16:54 proxy_connect_1014.patch -rw-r--r-- 1 root root 9697 Feb 9 16:54 proxy_connect.patch -rw-r--r-- 1 root root 9408 Feb 9 16:54 proxy_connect_rewrite_1014.patch -rw-r--r-- 1 root root 9505 Feb 9 16:54 proxy_connect_rewrite_101504.patch -rw-r--r-- 1 root root 9496 Feb 9 16:54 proxy_connect_rewrite_1015.patch -rw-r--r-- 1 root root 9553 Feb 9 16:54 proxy_connect_rewrite_1018.patch -rw-r--r-- 1 root root 9306 Feb 9 16:54 proxy_connect_rewrite_102101.patch -rw-r--r-- 1 root root 9337 Feb 9 16:54 proxy_connect_rewrite.patch # 導(dǎo)入模塊 后面為模塊路徑 patch -p1 < /nginx/ngx_http_proxy_connect_module/patch/proxy_connect_rewrite_1018.patch # 編譯 ./configure --add-module=/nginx/ngx_http_proxy_connect_module # 安裝,默認(rèn)安裝在/usr/local/nginx/ make && make install # 查看nginx ll /usr/local/nginx/ drwx------ 2 nobody root 4096 Feb 12 15:47 client_body_temp drwxr-xr-x 2 root root 4096 Feb 12 15:46 conf drwx------ 2 nobody root 4096 Feb 12 15:47 fastcgi_temp drwxr-xr-x 2 root root 4096 Feb 12 15:28 html drwxr-xr-x 2 root root 4096 Feb 12 15:47 logs drwx------ 2 nobody root 4096 Feb 12 15:47 proxy_temp drwxr-xr-x 2 root root 4096 Feb 12 15:33 sbin drwx------ 2 nobody root 4096 Feb 12 15:47 scgi_temp drwx------ 2 nobody root 4096 Feb 12 15:47 uwsgi_temp
配置正向代理
cd /usr/local/nginx/ #gzip on;下添加 vim conf/nginx.conf #gzip on; #正向代理轉(zhuǎn)發(fā)http請(qǐng)求 server { #指定DNS服務(wù)器IP地址 resolver 114.114.114.114; #監(jiān)聽(tīng)80端口,http默認(rèn)端口80 listen 80; #服務(wù)器IP或域名 server_name localhost; #正向代理轉(zhuǎn)發(fā)http請(qǐng)求 location / { proxy_pass http://$host$request_uri; proxy_set_header HOST $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; } } #正向代理轉(zhuǎn)發(fā)https請(qǐng)求 server { #指定DNS服務(wù)器IP地址 resolver 114.114.114.114; #監(jiān)聽(tīng)443端口,https默認(rèn)端口443 listen 443; #正向代理轉(zhuǎn)發(fā)https請(qǐng)求 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; } }
創(chuàng)建nginx用戶
nginx服務(wù)以用戶nginx身份啟動(dòng)
useradd nginx
檢查nginx配置并啟動(dòng)
sbin/nginx -t sbin/nginx
ss -utnlp | grep nginx tcp LISTEN 0 511 *:443 *:* users:(("nginx",pid=6645,fd=7),("nginx",pid=6644,fd=7)) tcp LISTEN 0 511 *:80 *:* users:(("nginx",pid=6645,fd=6),("nginx",pid=6644,fd=6))
nginx服務(wù)所在服務(wù)器驗(yàn)證正向代理功能
curl -I http://www.baidu.com/ -v -x 127.0.0.1:80 curl -I https://www.baidu.com/ -v -x 127.0.0.1:443
curl -I http://www.baidu.com/ -v -x 127.0.0.1:80 * About to connect() to proxy 127.0.0.1 port 80 (#0) * Trying 127.0.0.1... * Connected to 127.0.0.1 (127.0.0.1) port 80 (#0) > HEAD http://www.baidu.com/ HTTP/1.1 > User-Agent: curl/7.29.0 > Host: www.baidu.com > Accept: */* > Proxy-Connection: Keep-Alive > < HTTP/1.1 200 OK HTTP/1.1 200 OK < Server: nginx/1.20.2 Server: nginx/1.20.2 < Date: Sun, 12 Feb 2023 09:03:40 GMT Date: Sun, 12 Feb 2023 09:03:40 GMT < Content-Type: text/html Content-Type: text/html < Content-Length: 277 Content-Length: 277 < Connection: keep-alive Connection: keep-alive < Accept-Ranges: bytes Accept-Ranges: bytes < Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform < Etag: "575e1f60-115" Etag: "575e1f60-115" < Last-Modified: Mon, 13 Jun 2016 02:50:08 GMT Last-Modified: Mon, 13 Jun 2016 02:50:08 GMT < Pragma: no-cache Pragma: no-cache < * Connection #0 to host 127.0.0.1 left intact
curl -I https://www.baidu.com/ -v -x 127.0.0.1:443 * About to connect() to proxy 127.0.0.1 port 443 (#0) * Trying 127.0.0.1... * Connected to 127.0.0.1 (127.0.0.1) port 443 (#0) * Establish HTTP proxy tunnel to www.baidu.com:443 > CONNECT www.baidu.com:443 HTTP/1.1 > Host: www.baidu.com:443 > User-Agent: curl/7.29.0 > Proxy-Connection: Keep-Alive > < HTTP/1.1 200 Connection Established HTTP/1.1 200 Connection Established < Proxy-agent: nginx Proxy-agent: nginx < * Proxy replied OK to CONNECT request * Initializing NSS with certpath: sql:/etc/pki/nssdb * CAfile: /etc/pki/tls/certs/ca-bundle.crt CApath: none * SSL connection using TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 * Server certificate: * subject: CN=baidu.com,O="Beijing Baidu Netcom Science Technology Co., Ltd",OU=service operation department,L=beijing,ST=beijing,C=CN * start date: Jul 05 05:16:02 2022 GMT * expire date: Aug 06 05:16:01 2023 GMT * common name: baidu.com * issuer: CN=GlobalSign RSA OV SSL CA 2018,O=GlobalSign nv-sa,C=BE > HEAD / HTTP/1.1 > User-Agent: curl/7.29.0 > Host: www.baidu.com > Accept: */* > < HTTP/1.1 200 OK HTTP/1.1 200 OK < Accept-Ranges: bytes Accept-Ranges: bytes < Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform < Connection: keep-alive Connection: keep-alive < Content-Length: 277 Content-Length: 277 < Content-Type: text/html Content-Type: text/html < Date: Sun, 12 Feb 2023 09:03:40 GMT Date: Sun, 12 Feb 2023 09:03:40 GMT < Etag: "575e1f60-115" Etag: "575e1f60-115" < Last-Modified: Mon, 13 Jun 2016 02:50:08 GMT Last-Modified: Mon, 13 Jun 2016 02:50:08 GMT < Pragma: no-cache Pragma: no-cache < Server: bfe/1.0.8.18 Server: bfe/1.0.8.18 < * Connection #0 to host 127.0.0.1 left intact
配置不能訪問(wèn)外網(wǎng)的服務(wù)器(即內(nèi)網(wǎng)服務(wù)器),使其可以訪問(wèn)外網(wǎng)
liunx服務(wù)器配置
1.只配置使用yum時(shí),能夠使用正向代理訪問(wèn)外網(wǎng)
#追加配置 vim /etc/yum.conf proxy=http://192.168.0.20:80 #nginx正向代理服務(wù)器的地址 proxy=ftp://192.168.0.20:80 #nginx正向代理服務(wù)器的地址
2.只配置使用wget時(shí),能夠使用正向代理訪問(wèn)外網(wǎng)
#追加配置 vim /etc/wgetrc http_proxy=192.168.0.20:80 #nginx正向代理服務(wù)器的地址 http_proxy=192.168.0.20:443 #nginx正向代理服務(wù)器的地址
3.全局配置,所以訪問(wèn)請(qǐng)求都能夠使用正向代理訪問(wèn)外網(wǎng)
#追加配置 vim /etc/profile http_proxy=192.168.0.20:80 https_proxy=192.168.0.20:443 ftp_proxy=192.168.0.20:443 export http_proxy export https_proxy export ftp_proxy # 加載配置 source /etc/profile
windows服務(wù)器配置
驗(yàn)證不能訪問(wèn)外網(wǎng)的服務(wù)器(即內(nèi)網(wǎng)服務(wù)器),使用代理上網(wǎng)
curl -I http://www.baidu.com curl -I https://www.baidu.com
curl -I http://www.baidu.com HTTP/1.1 200 OK Server: nginx/1.20.2 Date: Sun, 12 Feb 2023 09:31:03 GMT Content-Type: text/html Content-Length: 277 Connection: keep-alive Accept-Ranges: bytes Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform Etag: "575e1f60-115" Last-Modified: Mon, 13 Jun 2016 02:50:08 GMT Pragma: no-cache
curl -I https://www.baidu.com 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: 277 Content-Type: text/html Date: Sun, 12 Feb 2023 09:31:07 GMT Etag: "575e1f60-115" Last-Modified: Mon, 13 Jun 2016 02:50:08 GMT Pragma: no-cache Server: bfe/1.0.8.18
到此這篇關(guān)于nginx正向代理的配置和使用教程的文章就介紹到這了,更多相關(guān)nginx正向代理內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Nginx簡(jiǎn)介_(kāi)動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
Nginx是一個(gè)跨平臺(tái)的Web服務(wù)器,可運(yùn)行在Linux、FreeBSD、Solaris、AIX、Mac OS、Windows等操作系統(tǒng)上,并且它還可以使用當(dāng)前操作系統(tǒng)特有的一些高效API來(lái)提高自己的性能2017-08-08nginx安裝圖解_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了nginx安裝的圖文教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08Nginx配置網(wǎng)頁(yè)轉(zhuǎn)發(fā)的實(shí)現(xiàn)步驟
本文主要介紹了Nginx配置網(wǎng)頁(yè)轉(zhuǎn)發(fā)的實(shí)現(xiàn)步驟,實(shí)現(xiàn)將云服務(wù)器的80端口轉(zhuǎn)發(fā)到另一臺(tái)服務(wù)器部署的網(wǎng)頁(yè),具有一定的參考價(jià)值,感興趣的可以了解一下2024-05-05nginx打印請(qǐng)求頭日志方法(親測(cè)可用)
之前想用nginx打印收到的請(qǐng)求的請(qǐng)求頭,但是只找到打印請(qǐng)求體的,沒(méi)有打印請(qǐng)求頭的,本文就來(lái)介紹一下nginx打印請(qǐng)求頭日志方法,感興趣的可以了解一下2023-11-11nginx 多個(gè)location轉(zhuǎn)發(fā)任意請(qǐng)求或訪問(wèn)靜態(tài)資源文件的實(shí)現(xiàn)
這篇文章主要介紹了nginx 多個(gè)location轉(zhuǎn)發(fā)任意請(qǐng)求或訪問(wèn)靜態(tài)資源文件的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11Nginx配置缺少導(dǎo)致CSS不起作用的問(wèn)題及解決方法
在Web開(kāi)發(fā)中,確保樣式表正確加載是前端顯示正常工作的關(guān)鍵,然而,有時(shí)候即使CSS文件的路徑和代碼本身沒(méi)有問(wèn)題,CSS樣式也可能無(wú)法正確應(yīng)用,本文將分享一個(gè)常見(jiàn)的問(wèn)題——Nginx配置缺少導(dǎo)致的CSS不起作用,以及如何解決這個(gè)問(wèn)題,感興趣的朋友一起看看吧2024-07-07nginx啟動(dòng)服務(wù)提示98: Address already in use錯(cuò)誤的解決
這篇文章主要給大家介紹了nginx啟動(dòng)服務(wù)提示98: Address already in use錯(cuò)誤的解決方法,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-05-05教你快速構(gòu)建一個(gè)基于nginx的web集群項(xiàng)目
本文教你快速構(gòu)建一個(gè)基于nginx的4/7層負(fù)載均衡的web集群項(xiàng)目,項(xiàng)目步驟本文給大家介紹介紹,對(duì)nginx web集群項(xiàng)目感興趣的朋友一起看看吧2021-11-11