Linux – Apache proxypass redirect subdomain to port and path

apache-2.2linuxproxypassredirectsubdomain

Ok, I wasn't sure how to word the title but I'll try to explain my situation.

I have a home server running debian that, among other things, is running "plex media server".

I access my server remotely using a subdomain to a domain I own, lets call it "mydomain.com". I access my server on the subdomain "home.mydomain.com" (which at the moment just displays some basic html) and I access the "Plex web interface" on "home.mydomain.com:32400/web".

What I want to do is redirect the subdomain "plex.mydomain.com" to "home.mydomain.com:32400/web" for easier access to the "plex interface".


I've managed to get halfway there but I'm unsure on how to proceed, or if this is in fact possible to do.

These is my virtual hosts as of now:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName home.mydomain.com

    DocumentRoot /var/www/home.mydomain.com/public_html

    <Directory />
            Options FollowSymLinks
            AllowOverride None
    </Directory>
    <Directory /var/www/>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride None
            Order allow,deny
            allow from all
    </Directory>

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
            AllowOverride None
            Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
            Order allow,deny
            Allow from all
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

<VirtualHost *:80>
    ServerName plex.mydomain.com
    ProxyRequests Off
    ProxyPreserveHost On
    ProxyPass / http://localhost:32400/
    ProxyPassReverse / http://localhost:32400/
</VirtualHost>

Which allows me to access the plex interface at "plex.mydomain.com/web", but I still want to get rid of the "/web" part.

So to do that I tried:

ProxyPass / http://localhost:32400/web
ProxyPassReverse / http://localhost:32400/web

Which didn't work at all, that just gives my "404 Not Found".


So, can this be solved in the way I want? And if so, how do I do it.

Any help is much appreaciated.

Best Answer

You can get rid of the web context using a rewrite rule:

ProxyRequests Off
ProxyPreserveHost On
ProxyPass / http://127.0.0.1:32400/
ProxyPassReverse / http://127.0.0.1:32400/

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/web
RewriteCond %{HTTP:X-Plex-Device} ^$
RewriteRule ^/$ /web/$1 [R,L]

Note the rewrite conditions in the second section.

FYI, I got these settings from here: http://matt.coneybeare.me/how-to-map-plex-media-server-to-your-home-domain/

You should check that out for all the nitty gritty details. Of particular note would be setting up some basic authentication to protect your host.