原由:
因想要使用一個免安裝且輕量 Proxy軟體,所以採用 nginx 軟體.
實測發現無法正向代理 https 網站(例如:網銀,Gmail)。
如果訪問 Https 網站,比如:https://www.google.com,Nginx
error.log 日誌如下:
"CONNECT www.google.com:443 HTTP/1.1" 400
無法使用,才手動打造 Windows環境下 nginx.
1.Install Cygwin 透過官網下載
https://cygwin.com/setup-x86_64.exe
1.1 安裝編譯套件及相關模組
setup-x86_64.exe -q -P cmake -P make -P gcc-core -P gcc-g++ -P libpcre-devel -P libpcre1
setup-x86_64.exe -q -P pcre2 -P libpcre2-devel -P pcre -P libpcre-devel -P zlib-devel -P libcrypt-devel -P patch -P pwget
ps:
如需查詢 Package 列表,參考下列網址
https://cygwin.com/packages/package_list.html#p
2.下載 ngx_http_proxy_connect_module 源碼及 patch file
GitHub 上打包下載 ngx_http_proxy_connect_module 壓縮的 zip 源碼檔:
https://github.com/chobits/ngx_http_proxy_connect_module
GitHub 上下載 nginx 相對應版本的 patch:
https://github.com/chobits/ngx_http_proxy_connect_module/blob/master/patch/proxy_connect_rewrite_102101.patch
3.建立編譯目錄 (D:\tnginx)
執行桌面捷徑 'Cygwin64 Terminal'
$ cd d:
$ cd tnginx
$ wget http://nginx.org/download/nginx-1.26.3.tar.gz
$ tar -xzvf nginx-1.26.3.tar.gz
3.1 將下載 ngx_http_proxy_connect_module-master.zip 解壓;
3.2 ngx_http_proxy_connect_module-master(目錄)更名為 ngx_http_proxy_connect_module
3.3 上述目錄搬移到 D:\tnginx\nginx-1.26.3 目錄下
3.4 將下載檔案 proxy_connect_rewrite_102101.patch 搬移到 D:\tnginx 目錄下
$ cd nginx-1.26.3
$ patch -p1 < ../proxy_connect_rewrite_102101.patch
$ ./configure --add-module=./ngx_http_proxy_connect_module --prefix=./nginx-1.26
$ make && make install
$ exit
4.建立執行環境 (D:\Cygwin)
4.1 將編譯完成 D:\tnginx\nginx-1.26.3\nginx-1.26目錄搬移到 D:\Cygwin目錄下
4.2 將D:\Cygwin\nginx-1.26\bin\nginx.exe 檔案搬移到 D:\Cygwin目錄下
4.3 D:\Cygwin\nginx.exe 所用到 dll檔(cygcrypt-2.dll;cygpcre2-8-0.dll;cygwin1.dll;cygz.dll)可在 C:\cygwin64\bin 目錄下找到並複製至此處
目錄結構如下:
D:\CYGWIN
│ cygcrypt-2.dll
│ cygpcre2-8-0.dll
│ cygwin1.dll
│ cygz.dll
│ nginx.exe
│
└─nginx-1.26
├─client_body_temp
├─conf
│ fastcgi.conf
│ fastcgi.conf.default
│ fastcgi_params
│ fastcgi_params.default
│ koi-utf
│ koi-win
│ mime.types
│ mime.types.default
│ nginx.conf
│ nginx.conf.default
│ scgi_params
│ scgi_params.default
│ uwsgi_params
│ uwsgi_params.default
│ win-utf
│
├─fastcgi_temp
├─html
│ 50x.html
│ index.html
│
├─logs
│ access.log
│ error.log
│
├─proxy_temp
├─scgi_temp
└─uwsgi_temp
5.驗證及測試:
5.1 測試:
D:\cygwin>nginx.exe -V
nginx version: nginx/1.26.3
built by gcc 12.4.0 (GCC)
configure arguments: --add-module=./ngx_http_proxy_connect_module --prefix=./nginx-1.26
5.2 編輯設定檔 (D:\Cygwin\nginx-1.26\conf\nginx.conf)
==========================================================================
worker_processes auto;
events {
worker_connections 1024;
}
http {
server_tokens off;
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 65;
#gzip on;
server {
listen 8080;
server_name localhost;
resolver 8.8.8.8;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
proxy_connect;
proxy_connect_allow 443 563;
proxy_connect_timeout 30;
location / {
proxy_pass http://$host;
proxy_set_header Host $http_host;
proxy_buffers 256 4k;
proxy_max_temp_file_size 0;
proxy_cache_valid 200 302 10m;
proxy_cache_valid 301 1h;
proxy_cache_valid any 1m;
}
}
}
==========================================================================
.配置緩存大小,關閉磁盤緩存讀寫減少I/O,以及代理連接超時時間。
proxy_buffers 256 4k;
proxy_max_temp_file_size 0;
proxy_connect_timeout 30;
.配置代理服務器 http 狀態緩存時間。
proxy_cache_valid 200 302 10m;
proxy_cache_valid 301 1h;
proxy_cache_valid any 1m;
5.3 參數驗證:
D:\cygwin>nginx.exe -t
nginx: the configuration file ./nginx-1.26/conf/nginx.conf syntax is ok
nginx: configuration file ./nginx-1.26/conf/nginx.conf test is successful
5.4 啟動服務
D:\cygwin>start nginx.exe
D:\cygwin>netstat -ano | findstr 8080
TCP 0.0.0.0:8080 0.0.0.0:0 LISTENING 5896
5.5 停止服務
D:\cygwin>nginx.exe -s stop