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

Rewriting rules in Apache for a different port

我的 Apache(Tomcat)-Spring 服务器在端口 8080 上运行。我想调用本地主机默认端口 (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/ [L,R]

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

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

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

RewriteEngine On

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

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 名称替换上面的 "servlet-name"。

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

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