環境說明:
本次測試使用的操作系統為:CentOS 7.2 x86 64位 最小化安裝的操作系統,系統基礎優化請參考:https://www.cnblogs.com/hei-ma/p/9506623.html
正向代理的nginx安裝正常安裝就可以,沒有特別的要求,
說明:
nginx當正向代理的時候,通過代理訪問https的網站會失敗,而失敗的原因是客戶端同nginx代理服務器之間建立連接失敗,並非nginx不能將https的請求轉發出去。因此要解決的問題就是客戶端如何同nginx代理服務器之間建立起連接。有了這個思路之后,就可以很簡單的解決問題。我們可以配置兩個SERVER節點,一個處理HTTP轉發,另一個處理HTTPS轉發,而客戶端都通過HTTP來訪問代理,通過訪問代理不同的端口,來區分HTTP和HTTPS請求。
下面看nginx的配置文件如下:
# cat nginx.conf # For more information on configuration, see: # * Official English Documentation: http://nginx.org/en/docs/ # * Official Russian Documentation: http://nginx.org/ru/docs/ user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid; include /usr/share/nginx/modules/*.conf; events { worker_connections 1024; } http { 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; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; include /etc/nginx/mime.types; default_type application/octet-stream; include /etc/nginx/conf.d/*.conf; #HTTP proxy #這里位http的正向代理配置 server{ resolver 8.8.8.8; access_log /var/log/nginx/access_proxy-80.log main; listen 80; location / { root html; index index.html index.htm; proxy_pass $scheme://$host$request_uri; proxy_set_header HOST $http_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; } } #HTTPS proxy #這里為:https的正向代理配置 server{ resolver 8.8.8.8; access_log /var/log/nginx/access_proxy-443.log main; listen 443; location / { root html; index index.html index.htm; proxy_pass https://$host$request_uri; 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,
然后我們來訪問測試下:
1、如果訪問HTTP網站,可以直接這樣的方式: curl --proxy proxy_server-ip:80 http://www.hm.net/
2、如果訪問HTTPS網站,例如https://www.alipay.com,那么可以使用nginx的HTTPS轉發的server:
curl --proxy proxy_server:443 http://www.alipay.com
3、使用瀏覽器訪問
這里使用的是firefox瀏覽器
如何確定訪問是不是走的代理那?
可以在瀏覽器上設置好代理后,然后將你代理的nginx關掉,然后重新打開一個網頁,會發現測試不可以訪問網站了!!
本篇博客參考地址:https://yq.aliyun.com/articles/490062