Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache reverse proxy: path not added in location redirects

While creating a simple reverse proxy, I'm struggling with a path-related issue that I cannot solve.

Scenario

  • I have a webserver on a frontend machine: www.example.com.
  • The frontend machine serves a local website (from /var/www/html)
  • I created a new website and deployed it on port 'pppp' of an internal machine: xxx.xxx.xxx.xxx:pppp

Goal

  • The apache webserver on the frontend machine should serve content from xxx.xxx.xxx.xxx:pppp whenever the user request is www.example.com/path (whatever internal subpath or subpage is requested).

From what I have read, to achieve this it should be as easy as adding the following directives in /etc/apache2/sites-available/000-default.conf within the <VirtualHost> tag:

ProxyPreserveHost On
ProxyPass "/path/" "http://xxx.xxx.xxx.xxx:pppp/"
ProxyPassReverse "/path/" "http://xxx.xxx.xxx.xxx:pppp/"

Issues

  • Requests of the form: www.example.com/path/internal_page.html work fine
  • Requests of the form www.example.com/path/ work fine

The problem is with requests of (sub-)paths with no trailing slashes. For example:

  1. User requests www.example.com/path/subpath (no trailing slash!)
  2. Frontend apache correctly redirects to internal machine
  3. Internal machine (which has its own apache server) outputs (as it should) a 301 response. However, the "Location:" header in the response is "http://www.example.com/subpath/" instead of "http://www.example.com/path/subpath/".

What I'm doing wrong?

Thanks!

like image 814
Guido Avatar asked Sep 09 '26 12:09

Guido


1 Answers

I faced the same problem. Managed to fix it by adding

<Location /mypath>
  ProxyPreserveHost On
  ProxyPass http://127.0.0.1:5000/mypath
  ProxyPassReverse http://127.0.0.1:5000/mypath
</Location>

Daemon that runs ASP.NET Core app contains pathBase parameter:

[Unit]
Description=My Web App

[Service]
WorkingDirectory=/opt/sites/MyWebApp
ExecStart=/usr/bin/dotnet /opt/sites/MyWebApp/MyWebApp.dll --pathBase=/mypath --urls=http://0.0.0.0:5000
Restart=always
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=trialkeygen
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false

[Install]
WantedBy=multi-user.target
like image 107
Mike Avatar answered Sep 13 '26 17:09

Mike