Carlos Aguni

Highly motivated self-taught IT analyst. Always learning and ready to explore new skills. An eternal apprentice.


Nginx http -> https forward proxy

05 Apr 2022 »

https://stackoverflow.com/questions/46060028/how-to-use-nginx-as-forward-proxy-for-any-requested-location

server {
    listen       8888;

    location / {
        resolver 8.8.8.8; # may or may not be necessary.
        proxy_pass http://$http_host$uri$is_args$args;
    }
}

https://www.alibabacloud.com/blog/how-to-use-nginx-as-an-https-forward-proxy-server_595799#:~:text=NGINX%20was%20initially%20designed%20as,how%20to%20encrypt%20HTTPS%20traffic.

https://github.com/chobits/ngx_http_proxy_connect_module

  curl                     nginx (proxy_connect)            github.com
    |                             |                          |
(1) |-- CONNECT github.com:443 -->|                          |
    |                             |                          |
    |                             |----[ TCP connection ]--->|
    |                             |                          |
(2) |<- HTTP/1.1 200           ---|                          |
    |   Connection Established    |                          |
    |                             |                          |
    |                                                        |
    ========= CONNECT tunnel has been established. ===========
    |                                                        |
    |                             |                          |
    |                             |                          |
    |   [ SSL stream       ]      |                          |
(3) |---[ GET / HTTP/1.1   ]----->|   [ SSL stream       ]   |
    |   [ Host: github.com ]      |---[ GET / HTTP/1.1   ]-->.
    |                             |   [ Host: github.com ]   |
    |                             |                          |
    |                             |                          |
    |                             |                          |
    |                             |   [ SSL stream       ]   |
    |   [ SSL stream       ]      |<--[ HTTP/1.1 200 OK  ]---'
(4) |<--[ HTTP/1.1 200 OK  ]------|   [ < html page >    ]   |
    |   [ < html page >    ]      |                          |
    |                             |                          |

https://www.codetd.com/en/article/9782805

install deps

yum -y groupinstall "development tools"
yum -y install pcre-devel zlib-devel

pre

curl -O https://nginx.org/download/nginx-1.20.1.tar.gz
tar xzvf nginx-1.20.1.tar.gz

ngx_http_proxy_connect_module

pushd /root
git clone https://github.com/chobits/ngx_http_proxy_connect_module.git
cp ngx_http_proxy_connect_module/ngx_http_proxy_connect_module.c nginx-1.20.1/src/http/
pushd /root/nginx-1.20.1
mkdir modules
cp -r /root/ngx_http_proxy_connect_module modules/

patch

github.com/chobits/ngx_http_proxy_connect_module#install

patch -p1 < /root/\<patch-location>

compile && build

./configure --add-module=./modules/ngx_http_proxy_connect_module
make
make install

run

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    #include       /etc/nginx/mime.types;
    include       /usr/local/nginx/conf/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  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    #include /etc/nginx/conf.d/*.conf;
    
    server {
         listen  443;
        
         # dns resolver used by forward proxying
         #resolver  114.114.114.114;
         resolver 8.8.8.8;

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

         # forward proxy for non-CONNECT request
         location / {
             proxy_pass http://$host;
             proxy_set_header Host $host;
         }
     }
}
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
/usr/local/nginx/sbin/nginx -s stop