1.下載nginx安裝包 此處下載nginx-1.20
https://nginx.org/en/download.html 官網(wǎng)下載地址
2.下載正向代理模塊包
https://github.com/chobits/ngx_http_proxy_connect_module 下載地址
nginx版本與代理模塊對(duì)照表
3.部署nginx服務(wù)
此處使用的是nginx-1.20.2,對(duì)應(yīng)proxy_connect_rewrite_1018.patch
3.1. 上傳nginx包和正向模塊包至互聯(lián)網(wǎng)服務(wù)器
3.2 解壓 改名
tar -xf nginx.tar.gz
unzip ngx_http_proxy_connect_module-master.zip
mv ngx_http_proxy_connect_module-master ngx_http_proxy_connect_module
3.3 安裝nginx
基礎(chǔ)運(yùn)行環(huán)境安裝
yum -y install make gcc openssl openssl-devel pcre-devel zlib zlib-devel
查看正向代理模塊proxy_connect_rewrite_1018.patch的位置
ll ../ngx_http_proxy_connect_module/patch/
導(dǎo)入模塊 后面為模塊路徑
patch -p1 < /nginx/ngx_http_proxy_connect_module/patch/proxy_connect_rewrite_1018.patch
編譯
#配置configure --prefix 代表安裝的路徑,--with-http_ssl_module 安裝ssl,--with-http_stub_status_module查看nginx的客戶端狀態(tài)
./configure --add-module=/nginx/ngx_http_proxy_connect_module --prefix=/usr/local/nginx-1.20.2 --with-http_ssl_module --with-http_stub_status_module
安裝nginx 安裝位置根據(jù) 編譯時(shí)配置的–prefix=
make && make install
3.4 配置正向代理(舉例,將正向代理端口配置在8030上)
在nginx.conf文件里的http節(jié)點(diǎn)下增加文章來源:http://www.zghlxwxcb.cn/news/detail-719631.html
server {
listen 8030;
server_name localhost;
resolver 114.114.114.114 ipv6=off;
proxy_connect;
proxy_connect_allow all;
proxy_connect_connect_timeout 10s;
proxy_connect_read_timeout 10s;
proxy_connect_send_timeout 10s;
location / {
proxy_pass https://$host$request_uri;
proxy_set_header HOST $host;
proxy_http_version 1.1;
proxy_ssl_server_name on;
}
}
3.5 測(cè)試nginx所在的互聯(lián)網(wǎng)機(jī)器是否正常
curl -I http://www.baidu.com/ -v -x 127.0.0.1:8030
curl -I https://www.baidu.com/ -v -x 127.0.0.1:8030
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
3.6 內(nèi)網(wǎng)機(jī)器全局配置,所有請(qǐng)求都能夠正常使用代理訪問外網(wǎng)
# 追加配置
vim /etc/profile
# 這里的地址要寫代理的服務(wù)器地址
http_proxy=192.168.0.20:80
# 這里的地址要寫代理的服務(wù)器地址
https_proxy=192.168.0.20:443
# 這里的地址要寫代理的服務(wù)器地址
ftp_proxy=192.168.0.20:443
export http_proxy
export https_proxy
export ftp_proxy
# 加載配置
source /etc/profile
3.7測(cè)試內(nèi)網(wǎng)服務(wù)器(即不能訪問外網(wǎng)的服務(wù)器),使用代理上網(wǎng)
curl -I https://www.baidu.com -v -x http://ip:8030
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
此時(shí)內(nèi)網(wǎng)機(jī)器已經(jīng)可以通過互聯(lián)網(wǎng)機(jī)器對(duì)應(yīng)的正向代理端口訪問互聯(lián)網(wǎng)了文章來源地址http://www.zghlxwxcb.cn/news/detail-719631.html
到了這里,關(guān)于用nginx做正向代理,即(使內(nèi)網(wǎng)機(jī)器可以通過互聯(lián)網(wǎng)服務(wù)器上互聯(lián)網(wǎng)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!