CentOS 7.X下 -- 配置nginx正向代理支持https

環境說明:

        本次測試使用的操做系統爲:CentOS 7.2 x86 64位 最小化安裝的操做系統,系統基礎優化請參考:https://www.cnblogs.com/hei-ma/p/9506623.htmlhtml

        正向代理的nginx安裝正常安裝就能夠,沒有特別的要求,node

說明: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,app

而後咱們來訪問測試下:curl

一、若是訪問HTTP網站,能夠直接這樣的方式: curl --proxy proxy_server-ip:80 http://www.hm.net/
tcp

 

二、若是訪問HTTPS網站,例如https://www.alipay.com,那麼可使用nginx的HTTPS轉發的server:
curl --proxy proxy_server:443 http://www.alipay.com測試

 

三、使用瀏覽器訪問優化

這裏使用的是firefox瀏覽器

如何肯定訪問是否是走的代理那?

能夠在瀏覽器上設置好代理後,而後將你代理的nginx關掉,而後從新打開一個網頁,會發現測試不能夠訪問網站了!!

 

本篇博客參考地址:https://yq.aliyun.com/articles/490062

相關文章
相關標籤/搜索