2010-07-28 39 views
3

我試圖實現一個使用nginx作爲靜態文件的前端Web代理並使用Starman作爲我的後端Web服務器的Catalyst應用程序。 (我可以使用Apache的FastCGI &和它工作得很好,但我真的很想得到理順整個PSGI /普拉克和星人的事情)uri_for包括重定向上的端口號

接龍啓動還好,並能處理我的要求只是罰款http://localhost:5000。當我啓動nginx作爲我的前端代理時,無論何時何地使用$c->uri_for方法,我的網址都會變得很難看,並且會隨端口號(5000)而變化。

例子:

 
$c->uri_for("/login") 
becomes 
http://myapp.example.com:5000/login 
rather than 
http://myapp.example.com/login 

我正在創建一些日誌,這樣我就可以看到什麼X-Forwarded-HostX-Forwarded-For設置爲。對於正常的請求,有設置的值(來自nginx),但每當使用$c->uri_for方法時,這些值都不存在。

有沒有其他人有這個問題?
我是否在我的nginx或Catalyst配置中缺少其他配置?

謝謝!

nginx的配置:

 
server { 
     listen  80; 
     server_name myapp.example.com; 

     location /static { 
      root /data/users/MyApp/root; 
      expires 30d; 
     } 

     location/{ 
      proxy_set_header Host $host; 
      proxy_set_header X-Forwarded-Host $host; 
      proxy_set_header X-Real-IP $remote_addr; 
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 

      proxy_pass http://localhost:5000/; 
     } 
    } 

事件儘管這將在同一臺物理服務器上運行,在MyApp的配置我已經設置:

 
MyApp->config(using_frontend_proxy => 1) 

版本:

 
Catalyst : 5.80024 
nginx : 0.7.67 
Plack : 0.9942 
Starman : 0.2006 

回答

4

我問題出在我的myapp.psgi文件中。

Catalyst::Engine::PSGI

,並期待在Plack::Middleware::ReverseProxy

... 
use Plack::Builder; 
use MyApp; 

MyApp->setup_engine('PSGI'); 
my $app = sub { MyApp->run(@_) }; 

builder { 
enable_if { $_[0]->{REMOTE_ADDR} eq '127.0.0.1' } 
     "Plack::Middleware::ReverseProxy"; 
$app; 
}; 
1

在我的情況下,前端在不同的主機上,添加

MyApp->config(using_frontend_proxy => 1) 

確實有所作爲,解決了這個問題。

+0

這是記錄在https://metacpan.org/pod/Catalyst#PROXY-SUPPORT,應該是接受的答案,因爲這是內置到催化劑,不需要修改生成的.psgi文件,並且可以由本地配置設置。 – 2018-01-10 04:43:47