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


    环境说明:

            本次测试使用的操作系统为: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

  • 相关阅读:
    php 类文件加载 Autoloader
    windows下配置nginx+php
    crontab 定时调度
    PHP 常见语法 集合
    php socket
    tengine + mysql + nginx + php
    yii2 [行为] behaviors 拦截器
    .htaccess 语法以及应用
    nginx 安装
    Nginx HTTP负载均衡和反向代理配置
  • 原文地址:https://www.cnblogs.com/hei-ma/p/9722478.html
Copyright © 2020-2023  润新知