如何使Nginx将https流量作为https代理服务器转发到上游?

时间:2019-07-27 07:31:04

标签: nginx proxy load-balancing tor

在这里,我想使用nginx设置一个Tor集群作为负载均衡器,以提供HA tor服务。我选择nginx模块“ https://github.com/reiz/nginx_proxy”来提供https_proxy功能。它与https连接一起很好地工作。但没有将https流量转发到上游。

发生了什么事

情况1:

当我运行以下cmd时,它将从上游Tor服务返回一个随机ip,它可以很好地处理http流量。

curl -x localhost:8090 http://api.ipify.org

情况2:

由于https流量失败,我从访问日志中看到的是

"CONNECT api.ipify.org:443 HTTP/1.1" 200 6176 "-" "curl/7.54.0" "-"

是的,nginx可以作为https代理使用,但是无法将https流量转发到Tor上游。下面的命令总是返回相同的IP,即我电脑的当前IP。

curl -x localhost:8090 https://api.ipify.org

所以,我想要的是如何使nginx将https流量转发到上游?

nginx.conf

user www-data;
worker_processes  auto;
daemon off;

events {}


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;
    error_log /var/log/nginx/error.log;
    include /usr/local/nginx/conf/conf.d/*.conf;
}

default.conf

upstream torProxy {
    server tor-cluster_tor_1:8118 max_fails=3 fail_timeout=30s;
    server tor-cluster_tor_2:8118 max_fails=3 fail_timeout=30s;
    server tor-cluster_tor_3:8118 max_fails=3 fail_timeout=30s;
    server tor-cluster_tor_4:8118 max_fails=3 fail_timeout=30s;
    server tor-cluster_tor_5:8118 max_fails=3 fail_timeout=30s;
    server tor-cluster_tor_6:8118 max_fails=3 fail_timeout=30s;
    server tor-cluster_tor_7:8118 max_fails=3 fail_timeout=30s;
    server tor-cluster_tor_8:8118 max_fails=3 fail_timeout=30s;
    server tor-cluster_tor_9:8118 max_fails=3 fail_timeout=30s;
    server tor-cluster_tor_10:8118 max_fails=3 fail_timeout=30s;
}

server {
    listen      8090;
    proxy_connect;
    proxy_max_temp_file_size 0;
    resolver 8.8.8.8;
    location / {
        proxy_pass http://torProxy;
        proxy_set_header Host $http_host;
        proxy_set_header Authorization "";
    }
}

2 个答案:

答案 0 :(得分:0)

您可以使用nginx的stream moduleHere是一篇很好的入门文章。

答案 1 :(得分:-1)

最简单的方法是在对HTTP流量进行https://upstream时使用proxy_pass。 这不会完全转发客户端发送的https流量,但会在您的nginx和上游之间创建一个新的https连接。