如何在 Apache 反向代理后面的子目录上设置 Play?

发布于 2024-12-25 01:50:59 字数 1304 浏览 3 评论 0原文

我有一个 Apache 2 前端,它提供两种请求:

mod-proxy.conf

ProxyPass        /playapp/ http://localhost:9000/
ProxyPassReverse /playapp/ http://localhost:9000/

最终结果是请求 http://mysite.com/playapp/Controller/action 到达Play 服务器为 http://localhost:9000/Controller/action

现在,开始玩吧!页面服务正常,但所有链接(包括 javascript、css 和其他页面的链接)均已损坏。例如,如果视图使用:

#{stylesheet 'style.css' /}

那么渲染的结果是

<link rel="stylesheet" type="text/css" href="/public/stylesheets/style.css" charset="utf-8" ></link>

因此最终用户尝试获取 http://mysite.com/public/stylesheets/style.css,这会返回 404,因为它不是实际上是Play的一部分!应用程序。

配置 Apache + Play 一起玩的正确方法是什么?

我正在寻找的结果是 Play!在最终呈现的 HTML 中返回这样的 URL(或者可能让 Apache 相应地重写 URL): http://mysite.com/playapp/public/stylesheets/style.css

另外,我确实需要一些在 Play 应用程序之外进行链接的能力。例如,我希望将主路由 (/) 映射到我的绝对根 (http://mysite.com/),而不是 Play 的根。

I have an Apache 2 frontend, that serves two kinds of requests:

  • Requests to the root folder (e.g. http://mysite.com/ and http://mysite.com/help) are served by the apache itself (PHP/Wordpress).
  • Specific requests to the '/playapp' subfolder are forwarded to Play! via a reverse proxy via mod-proxy:

mod-proxy.conf

ProxyPass        /playapp/ http://localhost:9000/
ProxyPassReverse /playapp/ http://localhost:9000/

The end result is that requests to say http://mysite.com/playapp/Controller/action reach the Play server as http://localhost:9000/Controller/action

Now, Play! serves the page correctly, but all links, including javascript, css and links to other pages, are broken. For example, if a view uses:

#{stylesheet 'style.css' /}

Then the rendered result is

<link rel="stylesheet" type="text/css" href="/public/stylesheets/style.css" charset="utf-8" ></link>

So the end user tries to fetches http://mysite.com/public/stylesheets/style.css, which returns a 404 because it's not actually part of the Play! app.

What's the correct way to configure Apache + Play to play along here?

The result I'm looking for is for Play! to return URLs such as this in the final rendered HTML (or perhaps for Apache to rewrite the URLs accordingly): http://mysite.com/playapp/public/stylesheets/style.css

Also, I do need some ability to link outside of the Play app. For example, I want the home route (/) to be mapped to my absolute root (http://mysite.com/), not to Play's root.

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

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

发布评论

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

评论(4

摘星┃星的人 2025-01-01 01:50:59

首先,有一点很重要:apache2 无法(轻松)更改页面中的链接。因此 Play 必须已经提供了正确的内容。

使用子域将使所有这些完全透明,但让我们解决您的问题。

您的问题确实有两点,

修复静态资源的子文件夹

使用路由只需设置

GET /playapp/public/ staticDir:public

您是否使用 http.path

我认为反向应该考虑到它......

外部链接

  • 另外,我确实需要一些在 Play 应用程序外部链接的能力。例如,我希望将主路由 (/) 映射到我的绝对根 (http://mysite.com/),而不是 Play 的根。

嗯,这听起来很简单:如果它在播放应用程序之外,那么您没有使用反向网址,因此只需将绝对路径放入链接中...或者您是否使用反向网址?如果是这样,你能举个例子吗?

First, something important: apache2 can't (easily) change links in the pages. So Play must provide the right ones already.

Using subdomain will make all of this completely transparent, but let's tackle your question.

You really have two points in your question,

Fix subfolders for static resources

Using routes just set

GET /playapp/public/ staticDir:public

Are you using http.path ?

I think reverse should take it into account....

outside links

  • Also, I do need some ability to link outside of the Play app. For example, I wan the home route (/) to be mapped to my absolute root (http://mysite.com/), not to Play's root.

Well this sounds easy: if it's outside the play app then you are not using a reverse url, so just put the absolute path in your links... or are you using a reverse? If so, can you provide an example?

苏别ゝ 2025-01-01 01:50:59

您是否配置了您的 application.conf

XForwardedSupport=127.0.0.1

和 apache.conf

ProxyPreserveHost on

如果这不起作用,则替代选项来自上一篇文章。

我相信我在另一篇文章中给出的答案与您的情况相关

如何在 Play Framework 的配置文件中使用“war.context”?

基本上,这意味着从属性文件中读取一些值,并将该值添加到所有路由中。这是 servlet 部署的建议方法,其中路径与默认播放设置使用的路径不同。

Have you configured your application.conf with

XForwardedSupport=127.0.0.1

and your apache.conf

ProxyPreserveHost on

An alternative option if that does not work is from a previous post.

I believe the answer I have given in this other post would be relevant to your situation

how to use "war.context" in Configuration file of Play Framework ?

Basically, it means reading some value from the properties file, and the pre-pending that value to all of your routes. It is the suggested method for servlet deployment where the path changes from that which the default play setup uses.

殤城〤 2025-01-01 01:50:59

期望 Apache 重写 HTML、JS 和 CSS 文件是愚蠢的。 SWF 文件或 JS 中动态构建的 URL 怎么样?不管怎样,你明白我的意思了。 ProxyPassReverse 文档 指出:

只有上面特别提到的 HTTP 响应标头才会被
重写。 Apache 不会重写其他响应头,也不会
重写 HTML 页面内的 URL 引用。这意味着如果
代理内容包含绝对 URL 引用,它们将绕过
代理。一个第三方模块,将查看 HTML 内部并
重写 URL 引用的是 Nick Kew 的 mod_proxy_html。

正如其中一条评论所建议的,一种更有可能成功的方法是配置另一个 DNS 名称(例如 play.ripper234.org)并创建如下配置:

<VirtualHost>
ServerName play.ripper234.org
ProxyPass / http://localhost:9000/
ProxyPassReverse / http://localhost:9000/
</VirtualHost>

即使这样,如果 Play 返回的文件也不会“拯救”您!将使用完全限定的 URL,例如 http://localhost:9000/http://www.yahoo.com/ 或其他。

至于向您推荐不同的 Web 服务器,我实际上认为您应该坚持使用 Apache。它具有非常合理和强大的配置,并且速度足够快,可以满足您的所有需求。总的来说,Apache 并不是特别慢。有些 Web 服务器更适合嵌入式使用,有些 Web 服务器更适合尽可能快地提供大量静态页面。在你变得真正变大之前,你不需要担心任何事情。

It'll be silly to expect Apache to rewrite HTML, JS and CSS files. How about SWF files or dynamically constructed URLs in JS? Anyway, you get my drift. The ProxyPassReverse documentation states:

Only the HTTP response headers specifically mentioned above will be
rewritten. Apache will not rewrite other response headers, nor will it
rewrite URL references inside HTML pages. This means that if the
proxied content contains absolute URL references, they will by-pass
the proxy. A third-party module that will look inside the HTML and
rewrite URL references is Nick Kew's mod_proxy_html.

As one of the comments suggested, an approach much more likely to succeed is to configure another DNS name (e.g. play.ripper234.org) and create a configuration such as:

<VirtualHost>
ServerName play.ripper234.org
ProxyPass / http://localhost:9000/
ProxyPassReverse / http://localhost:9000/
</VirtualHost>

Even this wouldn't "save" you if the files returned by Play! would use a fully-qualified URL such as http://localhost:9000/ or http://www.yahoo.com/ or whatnot.

As for recommending you a different web server, I actually think you should stick to Apache. It has very sensible and powerful configuration, and it's fast enough for all your needs. In general, Apache is not particularly slow. There are web servers better suited for embedded use, and there are web servers better suited for serving lots of static pages as fast as possible. It's nothing you should worry about until you become really big.

鱼窥荷 2025-01-01 01:50:59

Play 1.x 支持“http.path”设置。

Play 2.1-snapshot 已经支持配置设置“application.context”,将应用程序上下文放在子目录下。

请检查此提交:

允许使用 [application.context] 配置设置的根上下文。

Play 1.x support a "http.path" setting.

Play 2.1-snapshot already supports a configuration setting "application.context" to put the application context under a sub directory.

Please check this commit:

Allow root context using [application.context] configuration setting.

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