Mod rewrite 反向代理虚拟主机中的mod_重写

Mod rewrite 反向代理虚拟主机中的mod_重写,mod-rewrite,reverse-proxy,virtualhost,Mod Rewrite,Reverse Proxy,Virtualhost,我在8080端口上使用Tomcat,在443端口上使用Apache2.4。我已经在Apache虚拟主机上设置了通常的反向代理配置,该配置工作正常。这是我的设置: <VirtualHost *:443> ServerName www.example.com ServerAlias www.example.com ServerAdmin admin@example.com ErrorLog ${APACHE_LOG_DIR}/error.log C

我在8080端口上使用Tomcat,在443端口上使用Apache2.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>
我从错误日志中获得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_代理应用的上下文(.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/
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/