uri_for 包括重定向上的端口号

发布于 2024-09-12 11:58:35 字数 1389 浏览 1 评论 0原文

我正在尝试使用 nginx 作为静态文件的前端 Web 代理,并使用 Starman 作为我的后端 Web 服务器来实现 Catalyst 应用程序。 (我可以使用 Apache 和 FastCGI,它工作得很好,但我真的很想解决整个 PSGI / Plack 和 Starman 的问题)

Starman 启动正常,并且可以在 http 上很好地处理我的请求: //本地主机:5000。当我启动 nginx 作为前端代理时,无论何时何地使用 $c->uri_for 方法,我的 url 都会变得丑陋并与端口号 (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 conf 配置中是否缺少其他内容?

谢谢!

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

I'm attempting to implement a Catalyst application using nginx as a frontend web proxy for static files, and using Starman for my backend webserver. (I could use Apache & FastCGI and it works just fine, but I'd really like to get the whole PSGI / Plack and Starman thing ironed out)

Starman starts up okay and can handle my requests just fine on http://localhost:5000. When I fire up nginx to use as my front-end proxy, my urls become ugly and mangle with the port number (5000) whenever or wherever I use the $c->uri_for method.

Example :

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

I have some logs being created so I can see what X-Forwarded-Host and X-Forwarded-For are set as. For normal requests, there are values set (coming from nginx), but whenever the $c->uri_for method is used, those values do not exist.

Has anyone else had this problem?
Am I missing something else in my configuration of either nginx or my Catalyst conf?

Thanks!

nginx config :

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/;
        }
    }

Event though this will be ran on the same physical server, in MyApp config I have set :

MyApp->config(using_frontend_proxy => 1)

Versions :

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

顾挽 2024-09-19 11:58:35

我的问题出在我的 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;
};

My problem was in my myapp.psgi file.

from Catalyst::Engine::PSGI and look at 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;
};
假情假意假温柔 2024-09-19 11:58:35

就我而言,前端位于不同的主机中,添加

MyApp->config(using_frontend_proxy => 1)

确实产生了影响并解决了问题。

In my case the frontend was in a different host and adding

MyApp->config(using_frontend_proxy => 1)

did make a difference and solved the problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文