Nginx配置正向代理
阿新 • • 發佈:2021-12-17
最近一個支付專案,需要訪問外部支付介面,所以使用Nginx做一個正向代理,讓其使用。
記錄過程如下:
新增一個代理server
server { resolver 114.114.114.114; #指定DNS伺服器IP地址 listen 192.168.0.28:10080; location / { proxy_pass http://$host; #設定代理伺服器的協議和地址 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@appserver33 APP]# curl -I http://www.qq.com -x 192.168.0.28:10080 HTTP/1.1 302 Moved Temporarily Server: nginx/1.20.1 Date: Fri, 17 Dec 2021 10:19:19 GMT Content-Type: text/html Content-Length: 151 Connection: keep-alive Location: https://www.qq.com/ # 通過全域性設定代理 [root@appserver33 APP]# export http_proxy='192.168.0.28:10080' [root@appserver33 APP]# curl -I http://www.qq.com HTTP/1.1 302 Moved Temporarily Server: nginx/1.20.1 Date: Fri, 17 Dec 2021 10:22:12 GMT Content-Type: text/html Content-Length: 151 Connection: keep-alive Location: https://www.qq.com/
通過代理訪問http協議以實現。
發現問題,無法訪問https協議
[root@appserver33 APP]# curl -I https://www.qq.com -x 192.168.0.28:10080
HTTP/1.1 400 Bad Request
Server: nginx/1.20.1
Date: Fri, 17 Dec 2021 10:26:41 GMT
Content-Type: text/html
Content-Length: 157
Connection: close
curl: (56) Received HTTP code 400 from proxy after CONNECT
通過查詢資料發現: 預設nginx沒有載入https的代理模組,通過打補丁的方式,然後編譯安裝就可以。
https://github.com/chobits/ngx_http_proxy_connect_module
我這裡因為只需要代理http協議,就沒有往下走了。
傳統的正向代理軟體還有: squid