【问题标题】:Rewriting rules in Apache for a different port在 Apache 中为不同的端口重写规则
【发布时间】:2015-10-01 13:45:56
【问题描述】:

我的 Apache(Tomcat)-Spring 服务器在端口 8080 上运行。我想调用 localhost 默认端口 (80) 并希望重定向到端口 8080。

我启用了 mod-rewrite,DigitalOcean 的以下规则运行良好。

RewriteRule ^orange.html$ apple.html

我从Apache URL Rewriting Doc阅读了apache的重写规则

但是以下规则不起作用:

RewriteEngine On    # Turn on the rewriting engine
RewriteCond %{SERVER_PORT} !^80$
RewriteRule ^/(.*)         http://localhost:8080/$1 [L,R]

我的意图是允许跨域支持而不在 spring 控制器中启用 CORS。(这是一个硬性规则)

.htaccess 位于 /var/www/html。 此外,我不希望其他请求在端口 8080 上重定向,除非在 localhost/(my_specified_string)

【问题讨论】:

    标签: spring apache .htaccess mod-rewrite url-rewriting


    【解决方案1】:

    ProxyPass 是这里需要的,而不是重写引擎。

    遵循/etc/apache2/sites-available 中的000-default.conf 中的配置行就可以了。

    ProxyPass /servlet-name http://localhost:8080/servlet-name
    ProxyPassReverse /servlet-name http://localhost:8080/servlet-name
    

    这会将/servlet-name 上的任何请求重定向到http://localhost:8080/servlet-name

    可以将上面的“servlet-name”替换为它们各自的 servlet 名称。

    更多信息在这里: http://httpd.apache.org/docs/2.2/mod/mod_proxy.html

    【讨论】:

      【解决方案2】:

      您可以在 root .htaccess 中使用此规则:

      RewriteEngine On
      
      RewriteCond %{SERVER_PORT} =80
      RewriteRule ^my_specified_string http://localhost:8080%{REQUEST_URI} [NC,L,R]
      

      【讨论】:

        【解决方案3】:

        “重定向”在这种情况下不是正确的术语。 ProxyPass 和 ProxyPassReverse 指令设置反向代理,因此您不会将浏览器重定向到新 URL,而是接受一个端口上的流量并通过 apache httpd (mod_proxy) 将该流量反向代理到目标服务器:端口

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-06-29
          • 2011-07-24
          • 2014-06-30
          • 2016-08-09
          • 2011-09-02
          • 2011-10-26
          • 1970-01-01
          相关资源
          最近更新 更多