Nginx配置正向代理支援HTTP和HTTPS轉發

abin1703發表於2020-07-09





1、nginx不支援https,如果想要使用需要安裝以下的補丁

參考文件:

https nginx補丁



安裝編譯環境和工具

yum install gcc gcc-c++ autoconf automake -y

yum install pcre pcre-devel -y

yum install openssl openssl-devel -y

yum install patch -y

yum install git -y

yum install net-tools -y


安裝Nginx和ngx_http_proxy_connect_module模組

mkdir -p /downloads

cd /downloads


git clone


wget

tar -xzvf nginx-1.15.12.tar.gz

cd nginx-1.15.12/


patch -p1 < /downloads/ngx_http_proxy_connect_module/patch/proxy_connect_rewrite_101504.patch


./configure --add-module=/downloads/ngx_http_proxy_connect_module


make && make install


修改Nginx配置檔案

Nginx目錄:/usr/local/nginx


修改Nginx目錄下conf/nginx.conf配置檔案,在http中新增以下內容:


server {  

    resolver 114.114.114.114; 

    listen 8080;  

    location / {  

        proxy_pass

        proxy_set_header HOST $http_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;

    }  

}



server {

     listen                         8443;


     # dns resolver used by forward proxying

     resolver                       114.114.114.114;


     # forward proxy for CONNECT request

     proxy_connect;

     proxy_connect_allow            443 563;

     proxy_connect_connect_timeout  10s;

     proxy_connect_read_timeout     10s;

     proxy_connect_send_timeout     10s;


     # forward proxy for non-CONNECT request

     location / {

         proxy_pass

         proxy_set_header Host $host;

     }

 }


啟動Nginx

執行./sbin/nginx啟動Nginx。


Nginx命令參考:


# Start Nginx

./sbin/nginx


# Reload Nginx configuration

./sbin/nginx -s reload


# Stop Nginx

./sbin/nginx -s stop


檢視埠

netstat -tnlp | grep 8080

netstat -tnlp | grep 8443


開啟防火牆

firewall-cmd --zone=public --add-port=8080/tcp

firewall-cmd --zone=public --add-port=8080/tcp --permanent


firewall-cmd --zone=public --add-port=8443/tcp

firewall-cmd --zone=public --add-port=8443/tcp --permanent



firewall-cmd --reload

1

測試代理

# Test HTTP proxy

curl --proxy 192.168.87.123:8080


# Test HTTPS proxy

curl --proxy 192.168.87.123:8443



客戶端需要設定代理

export http_proxy="http://192.168.87.123:8080"

export https_proxy="http://192.168.87.123:8443"

如果永久生效需要寫在/etc/profile檔案中

source /etc/pofile 進行生效

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/30345407/viewspace-2703594/,如需轉載,請註明出處,否則將追究法律責任。

相關文章