Apache 500 Error – Proxying API Subdirectory Fix

500-errorapache-2.4mod-proxymod-rewritePROXY

I am trying to redirect PUT request for a specific endpoint to another host.

The said endpoint resides under /internal and accepts only PUT requests.
The other endpoints under /internal will continue to be served by my main host/server.

I have tried setting it up using both rewrite rules and using the proxy ([P]) flag and using the ProxyPass directive – all resulting in a 500 Internal Server Error and the request never makes it to the new host

My client application uses a simple REST client that cannot handle redirects, so I have to use some kind of proxying.

Apache logs show the following

[Thu Feb 10 08:56:20.394444 2022] [rewrite:trace1] [pid 8579] mod_rewrite.c(480): [client XXX.XXX.XXX.XXX:XXXXX] XXX.XXX.XXX.XXX - - [subdomain1.mydomain.com/sid#55d4ed07ecb0][rid#55d4ed2c5f20/initial] go-ahead with proxy request proxy: https://subdomain2.mydomain.com/internal/my-endpoint [OK]

Here's the current configuration for the specific vhost

<VirtualHost *:80>
 ServerName subdomain1.mydomain.com
  ProxyPass /soap ajp://localhost:7007/soap retry=3
  ProxyPreserveHost On
  Redirect /  https://subdomain1.mydomain.com/
  ErrorLog /var/log/httpd/subdomain1_error
</VirtualHost>

<VirtualHost *:443>
  ServerName subdomain1.mydomain.com
  Options FollowSymlinks
  ProxyRequests On
  ProxyPreserveHost On
  #RewriteEngine On

  #RewriteCond %{REQUEST_URI} '^/internal/my-endpoint'
  #RewriteCond %{REQUEST_METHOD} ^(PUT)
  #RewriteRule "^/(.*)" "https://subdomain2.mydomain.com/internal/my-endpoint" [P]

  ProxyPass /internal/my-endpoint https://subdomain2.mydomain.com/internal/my-endpoint
  ProxyPassReverse /internal/my-endpoint https://subdomain2.mydomain.com/internal/my-endpoint
  ProxyPreserveHost On

  LogLevel alert rewrite:trace3
  CustomLog /var/log/httpd/subdomain1_access_log common
  ProxyPass / ajp://localhost:7007/ retry=3
  ProxyPassReverse / ajp://localhost:7007/ retry=3
  ProxyPreserveHost Off
  ErrorLog /var/log/httpd/subdomain1
  SSLEngine on
</VirtualHost>

Best Answer

Ok- after enabling the debug in the apache logs it turns out the issue was related to the SSL certificates as the proxying was to a server which was accepting HTTPS connections only

Had to update the apache configuration to the one below to make it work

<VirtualHost *:80>
 ServerName subdomain1.mydomain.com
  ProxyPass /soap ajp://localhost:7007/soap retry=3
  ProxyPreserveHost On
  Redirect /  https://subdomain1.mydomain.com/
  ErrorLog /var/log/httpd/subdomain1_error
</VirtualHost>

<VirtualHost *:443>
  ServerName subdomain1.mydomain.com
  Options FollowSymlinks
  ProxyRequests Off
  ProxyErrorOverride off
  ProxyPreserveHost On
######
  SSLEngine on
  SSLProxyEngine on
######

  ProxyPass /internal/my-endpoint https://subdomain2.mydomain.com/internal/my-endpoint
  ProxyPassReverse /internal/my-endpoint https://subdomain2.mydomain.com/internal/my-endpoint

  LogLevel alert rewrite:trace3
  CustomLog /var/log/httpd/subdomain1_access_log common
  ProxyPass / ajp://localhost:7007/ retry=3
  ProxyPassReverse / ajp://localhost:7007/ retry=3
  ProxyPreserveHost Off
  ErrorLog /var/log/httpd/subdomain1
  SSLEngine on
</VirtualHost>