首页 文章

在Apache中为不同的端口重写规则

提问于
浏览
2

我的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)上

3 回答

  • 4

    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-namehttp://localhost:8080/servlet-name 的任何请求 .

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

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

  • 1

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

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

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

相关问题