Post

Nginx 如何进行正向代理

配置脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
wget https://github.com/chobits/ngx_http_proxy_connect_module/archive/refs/tags/v0.0.2.zip
unzip v0.0.2.zip

wget http://nginx.org/download/nginx-1.19.2.tar.gz
tar xf nginx-1.19.2.tar.gz

cd nginx-1.19.2
patch -p1 < /root/nginx/ngx_http_proxy_connect_module-0.0.2/patch/proxy_connect_rewrite_1018.patch

# this is install on CentOS
yum install gcc cmake make cmake unzip ncurses-devel gcc gcc-c++ pcre pcre-devel -y
./configure --prefix=/usr/local/nginx --add-module=/root/nginx/ngx_http_proxy_connect_module-0.0.2
make && make install 
ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/nginx

# create if you want log
mkdir -p -m 755 /var/log/nginx/

# configure nginx_conf
cd /usr/local/nginx/conf/
cp nginx.conf{,.bak}

vim nginx.conf

配置 nginx.conf

```plain text user ubuntu; worker_processes auto;

#error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info;

pid /run/nginx.pid; events { worker_connections 10240; }

http {

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
sendfile on;
tcp_nopush on;
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 /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;


#keepalive_timeout  0;
keepalive_timeout 65;

gzip on;

server {
    listen 8000;

    # dns resolver used by forward proxying
    resolver 8.8.8.8 1.1.1.1 208.67.222.222 223.5.5.5;

    # forward proxy for CONNECT request
    proxy_connect;
    proxy_connect_allow 443 563;
    proxy_connect_connect_timeout 10s;

    # forward proxy for non-CONNECT request
    location / {
        proxy_pass http://$host;
        proxy_set_header Host $host;
    }
} } ```
This post is licensed under CC BY 4.0 by the author.