Nginx 實現正向代理

使用正向代理的場景, 以及正向代理和反向代理的區別:

理解:
客戶端 代理 服務器
A B C
正向: * B C
反向: A B *

正向隱藏客戶端
反向隱藏服務端

1 查看文檔

https://github.com/chobits/ngx_http_proxy_connect_module/blob/master/README.md

2 其中: 構建爲動態模塊

安裝 nginx 和 nginx_proxy_connect module

從 nginx 的 1.9.11 開始,你也可以作爲一個動態模塊,通過使用編譯該模塊 --add-dynamic-module=PATH 選項,而不是 --add-module=PATH 在對./configure 命令行。
$ $ wget http://nginx.org/download/ngi...
$ tar -xzvf nginx-1.9.12.tar.gz
$ cd nginx-1.9.12/
$ patch -p1 < /path/to/ngx_http_proxy_connect_module/patch/proxy_connect.patch
$ ./configure --add-dynamic-module=/path/to/ngx_http_proxy_connect_module
$ make && make install

3 make 可能遇到的問題

1 error: this statement may fall through [-Werror=implicit-fallthrough=]
2 “src/os/unix/ngx_user.c:26:7: error: ‘struct crypt_data’ has no member named ‘current_salt’”

問題 1 解決, 註釋掉這一行

vim /opt/nginx/nginx-1.9.2/src/os/unix/ngx_user.c
36 : / cd.current_salt[0] = ~salt[0];/

問題 2 解決, 編譯加上參數 :
make CFLAGS='-Wno-implicit-fallthrough';
make install;

4 完成後 配置 conf :

vim /usr/local/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;
    #access_log  logs/access.log  main;
    sendfile        on;
    #tcp_nopush     on;
    keepalive_timeout  65;
    #gzip  on;
 
server {
        listen 88;                #監聽端口
        resolver 183.60.82.98;   #dns解析地址
        server_name  _;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        location / {
             proxy_pass https://$host$request_uri;     #設定http代理服務器的協議和地址
             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;
            #root   html;
            #index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
 
    }
 
 
 
server {
       resolver 8.8.8.8;   #dns解析地址
       listen 89;          #代理監聽端口
       proxy_connect;
       proxy_connect_allow            443 563;
       location / {
             proxy_pass $scheme://$host$request_uri;     #設定https代理服務器的協議和地址
             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;
 
       }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
 
    }  
 
}

啓動 nginx

curl -i --proxy 192.168.3.17:89 www.baidu.com

結果 :

本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源https://segmentfault.com/a/1190000040271546?utm_source=tag-newest