欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解如何在Nginx中配置正向代理以及整合Proxy插件

 更新時(shí)間:2024年05月21日 10:44:25   作者:微楓Micromaple  
正向代理不僅用于提升訪(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 插件,需要的朋友可以參考下

在企業(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.xNOproxy_connect.patch
1.4.x ~ 1.12.xYESproxy_connect_rewrite.patch
1.13.x ~ 1.14.xNOproxy_connect_1014.patch
1.13.x ~ 1.14.xYESproxy_connect_rewrite_1014.patch
1.15.2YESproxy_connect_rewrite_1015.patch
1.15.4 ~ 1.16.xYESproxy_connect_rewrite_101504.patch
1.17.x ~ 1.18.xYESproxy_connect_rewrite_1018.patch
1.19.x ~ 1.21.0YESproxy_connect_rewrite_1018.patch
1.21.1 ~ 1.22.xYESproxy_connect_rewrite_102101.patch
1.23.x ~ 1.24.0YESproxy_connect_rewrite_102101.patch
1.25.0 ~ 1.25.xYESproxy_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)文章

最新評(píng)論