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

相关文章
相关标签/搜索