【问题标题】:nginx transparent proxy: how it works?nginx 透明代理:它是如何工作的?
【发布时间】:2014-03-11 18:00:53
【问题描述】:

我有一个在 8008 端口上运行的透明代理服务器,我想向这个代理发送一些 $uri。这是我在代理服务器中的:

server
{
    listen 8008;
    location / {
        resolver 8.8.8.8;
        proxy_pass http://$http_host$request_uri;
    }
    access_log /var/log/nginx/proxy_access.log;
}

这确实有效 - 如果我将 host:8008 放在我的任何浏览器设置中,proxy_access.log 将显示如下记录:

myb.row.ser.ip - - [12/Feb/2014:20:09:24 -0600] "GET http://col.stb01.s-msn.com/i/4B/7CC71C50462D34411C88C3623A69A.jpg HTTP/1.1" 200 9075 "http://www.msn.com/" "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko"

请注意“GET http://col.stb01...”部分。

在我的一个虚拟服务器下,我设置了一个位置(示例 uri:/www.patterns.com/cat/subcat/sample.html):

location ~* (patterns)
{
    set $hostx "";  
    set $addrs "";  
    if ( $uri ~ "^/(.*?)(/.*)$") {  
        set $hostx $1;  # could be www.patterns.com
        set $addrs $2;  # could be /cat/subcat/sample.html
    }
proxy_pass http://127.0.0.1:8008/http://$hostx$addrs;
    proxy_set_header referer "";
}

上面的代码确实将捕获的 uri 发送到我的代理服务器,但是代理服务器看到的与 proxy_access.log 不同:

127.0.0.1 - - [12/Feb/2014:20:44:28 -0600] "GET /http://www.patterns.com/cat/subcat/sample.html HTTP/1.0" 502 574 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.107 Safari/537.36"

请注意前面的斜杠:"GET /http://www.patterns.co...",我相信这会导致代理服务器返回 502 代码。

现在我的问题是:在我的位置,如何将正确的 uri 发送到代理服务器?

谢谢,

【问题讨论】:

    标签: nginx proxy


    【解决方案1】:

    您应该删除 proxy_pass 中的“/http://”部分。

    其实我不太明白为什么要获取$hostx和$addrs,查看nginx doc

    While passing request nginx replaces URI part which corresponds to location with one indicated in proxy_pass directive. But there are two exceptions from this rule when it is not possible to determine what to replace:
    
    if the location is given by regular expression;
    if inside proxied location URI is changed by rewrite directive, and this configuration will be used to process request (break):
    location  /name/ {
      rewrite      /name/([^/] +)  /users?name=$1  break;
      proxy_pass   http://127.0.0.1;
    }
    For these cases of URI it is transferred without the mapping. 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-11
      相关资源
      最近更新 更多