apache rewrite rule for http_host containing port

Viewed 1310

In the local network, I have synology server with a number of services running in docker on different ports and accessible in-browser like that 192.168.1.2:8989 or server.spb.lan:8989

How to make a rewrite rule to convert them like that 192.168.1.2/servicename or server.spb.lan/servicename?

Like that

192.168.1.2:8989     ->  192.168.1.2/servicename
server.spb.lan:8989  ->  server.spb.lan/servicename
3 Answers

Based on your shown samples, could you please try following. Please make sure you clear your browser cache after placing these rules in your htaccess file.

RewriteEngine ON
RewriteCond %{HTTP_HOST} 192\.168\.1\.2
RewriteCond %{REQUEST_URI} ^/servicename [NC]
RewriteRule ^ http://%{HTTP_HOST}:8989%{REQUEST_URI} [NE,L]

RewriteCond %{HTTP_HOST} server\.spb\.lan
RewriteCond %{REQUEST_URI} ^/servicename [NC]
RewriteRule ^ http://%{HTTP_HOST}:8989%{REQUEST_URI} [NE,L]

I assume, that those backend services operate based on the http protocol, since you did not specify anything else...

Easiest probably is to use the apache proxy module to expose those backend services. You can use it within the rewriting module which makes the approach pretty convenient:

RewriteEngine on
RewriteRule ^/?servicename(/.*)$ https://server.spb.lan:8989$1 [P]

An alternative to the rewriting module would be to implement a reverse proxy:

ProxyRequests off
ProxyPass /servicename https://server.spb.lan:8989
ProxyPassReverse /servicename https://server.spb.lan:8989

I suggest you read about the details in the apache documentation. As typical for OpenSource it is of excellent quality and comes with great examples.

I couldn't solve the issue with apache installed directly on Synology. So I deleted it, deleted web station. Then I install nginx in docker. Synology has integrated nginx running on port 80, which cannot be deleted and there is no access to its settings. So I just mapped internal port 80 of nginx container to port 82 on Synology, make forward port 80 from router to port 82 on Synology. And then in config of nginx container I did simply that for each of my app running in docker

server {
    server_name sonarr.lan;
    location / {
        proxy_pass http://192.168.1.2:8989;
    }
}
Related