apache2反向代理配置

apache2 reverse proxy configuration

我有一个侦听 TCP 127.0.0.1:81 的应用程序。 我想完成以下重定向:

www.example.com/?requestid=123456 --> http://127.0.0.1:81/?requestid=123456
www.example.com/ANYTHING_ELSE --> MY_IP_THAT_APACHE_LISTENS_ON

我的理解是,如果我不显式重写某些内容,它将遵循通往 /var/www/html 的常规路径。

我的 /etc/apache2/sites-enabled/000-default.conf 配置:

<VirtualHost *:80>
        ServerName example.com
        ServerAdmin example@example.com
        DocumentRoot /var/www/html
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

    <Location />
        RewriteEngine On
        RewriteRule ^/?requestid(.*)$ http://127.0.0.1:81/ [P]
        ProxyPassReverse http://127.0.0.1:81/
        Order allow,deny
        Allow from all
    </Location>

</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

为什么重写不正常一直打正常补丁?

Not Found
The requested URL /bullshit was not found on this server.

Apache/2.4.25 (Debian) Server at example.com Port 80

来自RewriteRule Directive

In VirtualHost context, The Pattern will initially be matched against the part of the URL after the hostname and port, and before the query string (e.g. "/app1/index.html"). This is the (%-decoded) URL-path.

If you wish to match against the hostname, port, or query string, use a RewriteCond with the %{HTTP_HOST}, %{SERVER_PORT}, or %{QUERY_STRING} variables respectively.

因此,您将需要这样的东西:

RewriteEngine On
RewriteCond %{QUERY_STRING} requestid=(.+)
RewriteRule ^/$ http://127.0.0.1:81/?requestid=%1 [P]