Knowledge Base

Preserving for the future: Shell scripts, AoC, and more

Use Apache httpd as a reverse proxy for Minetest Mapserver

I use the Luanti Mapserver project for my various worlds, one instance per world. And because I'm old-school, I'm still rocking Apache httpd and still call it httpd like the old Enterprise Linux days and not "apache2" like the Devuanites. And I use httpd as a reverse proxy (for TLS termination, for one thing) for the mapserver.

The webpage works without the websocket upgrade, but the mapserver throws a stack trace complaining about the lack of the upgrade token in the Connection header.

level=error msg=ws-upgrade err="websocket: the client is not using the websocket protocol: 'upgrade' token not found in 'Connection' header"

After enough Internet research I found the answer! So here is my snippet for mapserver in my httpd config (inside the virtual host) that makes the magic happen.

RewriteEngine on
ProxyPreserveHost On
# mapserver
RewriteRule ^/games/luanti/world3/map$ /games/luanti/world3/map/ [R,L]
<Location "/games/luanti/world3/map/">
   ProxyPreserveHost On
   ProxyPass        http://server4:30011/ retry=20 connectiontimeout=300 timeout=300 upgrade=websocket
   ProxyPassReverse http://server4:30011/
   RequestHeader    set X-Script-Name /games/luanti/world3/map
   RewriteEngine on
   RewriteCond %{HTTP:Connection} Upgrade [NC]
   RewriteCond %{HTTP:Upgrade} websocket [NC]
   RewriteRule /(.*) ws://server4:30011/$1 [P,L]
</Location>

Because not the whole world uses that new-fangled nginx.

References

The secret sauce came from apache - Apache2 can't set headers "Connection" and "Upgrade" - Stack Overflow

Comments