【问题标题】:Using ProxyPass for pages but not images将 ProxyPass 用于页面而不是图像
【发布时间】:2010-10-08 20:44:59
【问题描述】:

由于horrible, horrible errors,我们改变了将 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 实现这一点?可能使用周围的<Location> 指令或类似的东西?

【问题讨论】:

    标签: apache proxy mod-jk ajp proxypass


    【解决方案1】:

    使用ProxyPassMatch:

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

    已编辑:马库斯·唐宁的更正

    【讨论】:

    • 使用正则表达式是否会影响性能?
    • 当表达式过于复杂时,正则表达式会出现一些性能问题。对于这种正则表达式是可以的。
    【解决方案2】:

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

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

    【讨论】:

    • 你能解释一下这些是做什么的吗?
    • 关闭保持活动连接并使请求使用 http 1.0 而不是 1.1
    • 正确。我相信原始错误消息是不同问题的症状,但我在同一个 tomcat 实例上的内部和第 3 方应用程序上都收到了它们。无论如何,这些更改清除了错误日志。
    【解决方案3】:

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

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

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

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

    我只能假设将$1 放在端口号8009 旁边会让人感到困惑。

    【讨论】:

      【解决方案4】:

      我们使用以下方法让 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 应用程序之外。我不知道它是否也适用于应用程序内的图像。

      【讨论】:

        猜你喜欢
        • 2013-03-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-30
        • 1970-01-01
        • 1970-01-01
        • 2014-10-05
        • 1970-01-01
        相关资源
        最近更新 更多