mod_rewrite反向代理的虚拟主机中



我在端口 8080 上使用 Tomcat,在端口 443 上的 Apache 2.4 后面。我已经在Apache虚拟主机上设置了通常的反向代理配置,该配置工作正常。这是我的设置:

<VirtualHost *:443>
ServerName www.example.com
ServerAlias www.example.com
ServerAdmin admin@example.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
ProxyRequests Off
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
ProxyPass / http://example.com:8080/
ProxyPassReverse / http:example.com:8080/
<Location />
Order allow,deny
Allow from all
</Location>
SSLEngine on
SSLCertificateFile /etc/ssl/example.com.crt
SSLCertificateKeyFile /etc/ssl/example.com.key
SSLCertificateChainFile /etc/ssl/CA.crt
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www. [NC]
RewriteCond %{HTTP_HOST} ^(?:www.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=301]
LogLevel alert rewrite:trace6
RewriteRule ^post$ post.xhtml [NC]
</VirtualHost>

我想在 Apache 上使用mod_rewrite,但是,这似乎不起作用。我意识到在 .htaccess 上进行重写时(这不适用于我的情况(,重写规则工作正常。但是,当我从虚拟主机尝试相同的规则时,它不起作用。例如,使用以下规则:

RewriteEngine on
LogLevel alert rewrite:trace6
RewriteRule ^test$ test.php [QSA]

我从错误日志中收到以下 404 错误:

[Tue Aug 14 12:12:40.135059 2018] [rewrite:trace2] [pid 11554] mod_rewrite.c(482): [client 127.0.0.1:53326] 127.0.0.1 - - [localhost/sid#7efc776e9518][rid#7efc776710a0/initial] init rewrite engine with requested uri /test
[Tue Aug 14 12:12:40.135108 2018] [rewrite:trace3] [pid 11554] mod_rewrite.c(482): [client 127.0.0.1:53326] 127.0.0.1 - - [localhost/sid#7efc776e9518][rid#7efc776710a0/initial] applying pattern '^test$' to uri '/test'
[Tue Aug 14 12:12:40.135121 2018] [rewrite:trace1] [pid 11554] mod_rewrite.c(482): [client 127.0.0.1:53326] 127.0.0.1 - - [localhost/sid#7efc776e9518][rid#7efc776710a0/initial] pass through /test

可能缺少什么?我希望可以对.htaccess进行重写规则

这是解决方案;首先,.htaccess不适用。根据文档,服务器配置虚拟主机是适用的mod_proxy的上下文(在我们的例子中,.htaccess 无论如何都没有意义(。

解决方案是使用带有标志 P 的重写规则,这会导致请求由 mod_proxy 处理,并通过代理请求处理。在此处查看更多内容。

应按如下方式修改虚拟主机:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.(?:css|js|jpe?g|gif|png)$ [NC]
RewriteRule ^/post/(.*)$ post.xhtml?id=$1 [P]
ProxyPass / http://example.com:8080/
ProxyPassReverse / http:example.com:8080/

最新更新