2017-03-09 81 views
0

我想用Nginx反向代理api。我有以下配置:Nginx proxy_pass返回[無主機],而反向代理

worker_processes 4; 

events { worker_connections 1024; } 

http { 

    upstream some_upstream { 
      server 1.something.com; 
      server 2.something.com; 
    } 

    server { 

      listen 80; 


      location ~/proxyNow/(?<zvar>(\w+))/(?<xvar>(\w+))/(?<yvar>(\w+))/ { 
        proxy_pass http://some_upstream/hello/something/$zvar/$xvar/$yvar/somethingelese; 
        proxy_set_header Host $http_host; 
        proxy_set_header Host   $host; 
        proxy_set_header X-Forwarded-For $remote_addr; 
        proxy_cache maps_cache; 
        proxy_cache_valid 200 302 365d; 
        proxy_cache_valid 404  1m; 
        proxy_redirect off; 

      } 

    } 


} 

當我嘗試撥打以下網址http://localhost:82/proxyNow/1/2/3/?app_id=someAppId&app_code=someCode

我收到以下錯誤信息:

無效的URL

請求的URL 「 http://%5bNo%20Host%5d/hello/something/1/2/3/somethingelese「,是 無效。參考#9.be35dd58.1489086561.5c9bd3c

似乎主機不能被nginx檢索。但是,如果我直接執行呼叫:

http://1.something.com/hello/something/$zvar/$xvar/$yvar/somethingelese?app_id=someAppId&app_code=someCode

http://2.something.com/hello/something/$zvar/$xvar/$yvar/somethingelese?app_id=someAppId&app_code=someCode

看來,Nginx的出於某種原因無法解析主機

+0

您有'proxy_set_header Host'和'proxy_cache_valid'重複的條目。你應該先嚐試糾正。 –

回答

0

你應該看看到文檔。

http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass

當使用正則表達式指定的位置。

在這種情況下,指令應該指定爲不帶URI。

我建議以下解決方案沒有昂貴的正則表達式的位置。
http://nginx.org/en/docs/http/ngx_http_core_module.html#location

location /proxyNow/ { 
    rewrite /proxyNow/(?<zvar>(\w+))/(?<xvar>(\w+))/(?<yvar>(\w+))/.* /hello/something/$zvar/$xvar/$yvar/somethingelese$is_args?$args break; 
    proxy_pass http://some_upstream; 
... other nginx diretives; 
}