对页面使用 ProxyPass,但对图像不使用

发布于 2024-07-14 16:19:22 字数 676 浏览 10 评论 0原文

由于可怕的错误,我们改变了我们将 Apache 连接到 Tomcat。 我们曾经使用 mod_jk

JkMount /path ajp13

现在我们使用 mod_proxy_ajp

ProxyPass /path ajp://localhost:8009/path
ProxyPassReverse /path ajp://localhost:8009/path

但是,JkMount 提供了一个功能,但 ProxyPass不:选择文件类型的能力。 这使得代理 html 文件成为可能,但不能代理图像 - 换句话说,让快速的 Apache 提供静态内容,而仅使用缓慢的 Tomcat 来提供动态内容。

JkMount /*.html ajp13

有没有办法用 ProxyPass 来实现这一点? 可能使用周围的 指令或类似的东西?

As a result of horrible, horrible errors, we've changed how we connect Apache to Tomcat. We were using mod_jk:

JkMount /path ajp13

Now we're using mod_proxy_ajp:

ProxyPass /path ajp://localhost:8009/path
ProxyPassReverse /path ajp://localhost:8009/path

However, there's a feature that JkMount offered but ProxyPass doesn't: the ability to select on file types. This made it possible to proxy html files, but not images - in other words, to let the nice fast Apache serve the static stuff, and resorting to the slow Tomcat only for the dynamic stuff.

JkMount /*.html ajp13

Is there any way of achieving this with ProxyPass? Possibly using a surrounding <Location> directive or something like that?

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

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

发布评论

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

评论(4

不回头走下去 2024-07-21 16:19:22

使用ProxyPassMatch

ProxyPassMatch ^/(path/.*\.html)$ ajp://localhost:8009/$1

编辑:Marcus Downing 的更正< /子>

Use ProxyPassMatch:

ProxyPassMatch ^/(path/.*\.html)$ ajp://localhost:8009/$1

Edited: Marcus Downing’s correction

半衬遮猫 2024-07-21 16:19:22

不是您的问题,而是使用此配置时需要注意的事项。 当使用 apache mod_proxy 连接到 tomcat 时,我的错误日志显示在中等负载下连接丢失。
将其添加到 httpd.conf 解决了我的问题。

SetEnv force-proxy-request-1.0 1
SetEnv proxy-nokeepalive 1

Not your issue but something to watch out for using this configuration. While using apache mod_proxy to connect to tomcat my error log was showing dropped connections under moderate load.
Adding this to httpd.conf solved my problems.

SetEnv force-proxy-request-1.0 1
SetEnv proxy-nokeepalive 1
七月上 2024-07-21 16:19:22

kmkaplan 的帖子是正确的答案,但它给了我错误:

Syntax error on line 32 of .../httpd-vhosts.conf:
ProxyPass Unable to parse URL

当我将指令更改为读取时,它起作用了:

ProxyPathMatch ^/(path/.*\.html)$ ajp://localhost:8009/$1

我只能假设将 $1 放在端口号 8009< 旁边/code> 令人困惑。

kmkaplan's post is the right answer, but it gave me the error:

Syntax error on line 32 of .../httpd-vhosts.conf:
ProxyPass Unable to parse URL

It worked when I changed the directive to read:

ProxyPathMatch ^/(path/.*\.html)$ ajp://localhost:8009/$1

I can only assume that putting the $1 right next to the port number 8009 was confusing it.

单挑你×的.吻 2024-07-21 16:19:22

我们使用以下内容让 Apache 提供图像并设置合理的过期标头:

<Virtualhost *:80>
    ServerName domain.com
    ServerAlias *.domain.com

    Alias /img/ /var/www/domain/img/
    <Directory /var/www/domain/img/>
        ExpiresActive On
        ExpiresByType image/gif "access plus 1 months"
        ExpiresByType image/jpg "access plus 1 months"
        ExpiresByType image/jpeg "access plus 1 months"
        ExpiresByType image/png "access plus 1 months"
        ExpiresByType image/x-icon "access plus 1 months"
        ExpiresByType image/ico "access plus 1 months"
        # This will prevent apache from having to check for a .htaccess file on each request.
        AllowOverride None
        # Allow symlinks. Otherwise, apache will make a separate call on each filename to ensure it is not a symlink.
        Options +FollowSymLinks -SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
    </Directory>

    ProxyRequests Off
    <Proxy *>
        Order deny,allow
        Allow from all
    </Proxy>

    # Prevent domain.com/img from being served by Tomcat
    ProxyPass /img !

    # Pass all other requests to Tomcat
    ProxyPass / ajp://localhost:8009/

    # 1. Note that usually no ProxyPassReverse directive is necessary. The AJP request includes
    #    the original host header given to the proxy, and the application server can be expected to
    #    generate self-referential headers relative to this host, so no rewriting is necessary. 
    # 2. If you still want to use it, read this first:
    #    http://www.humboldt.co.uk/2009/02/the-mystery-of-proxypassreverse.html
    # ProxyPassReverse / http://domain.com/
</Virtualhost>

但是,如您所见,我们将图像存储在 Tomcat 应用程序之外。 我不知道它是否也适用于应用程序内的图像。

We use the following to let Apache serve images and set reasonable expires headers:

<Virtualhost *:80>
    ServerName domain.com
    ServerAlias *.domain.com

    Alias /img/ /var/www/domain/img/
    <Directory /var/www/domain/img/>
        ExpiresActive On
        ExpiresByType image/gif "access plus 1 months"
        ExpiresByType image/jpg "access plus 1 months"
        ExpiresByType image/jpeg "access plus 1 months"
        ExpiresByType image/png "access plus 1 months"
        ExpiresByType image/x-icon "access plus 1 months"
        ExpiresByType image/ico "access plus 1 months"
        # This will prevent apache from having to check for a .htaccess file on each request.
        AllowOverride None
        # Allow symlinks. Otherwise, apache will make a separate call on each filename to ensure it is not a symlink.
        Options +FollowSymLinks -SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
    </Directory>

    ProxyRequests Off
    <Proxy *>
        Order deny,allow
        Allow from all
    </Proxy>

    # Prevent domain.com/img from being served by Tomcat
    ProxyPass /img !

    # Pass all other requests to Tomcat
    ProxyPass / ajp://localhost:8009/

    # 1. Note that usually no ProxyPassReverse directive is necessary. The AJP request includes
    #    the original host header given to the proxy, and the application server can be expected to
    #    generate self-referential headers relative to this host, so no rewriting is necessary. 
    # 2. If you still want to use it, read this first:
    #    http://www.humboldt.co.uk/2009/02/the-mystery-of-proxypassreverse.html
    # ProxyPassReverse / http://domain.com/
</Virtualhost>

However, as you can see, we store images outside of our Tomcat application. I don't know if it also works for images inside the application.

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