PLEX virtual host configuration

I had googled all around trying to find a solution to the problem of setting up a virtual host configuration for a plex installation and hiding the URL for the port and making '/' just do the right thing.

Instead of typing this into a browser


I want to be able to enter this


This was pretty damn close:

However it did not completely work and required a small adjustment for my Plex server running on CentOS.

Modify my DNS server to setup a CNAME for cheese this already hosts an apache webserver on port 80 and its where I also run PLEX.

cheese          IN      A
plex            IN      CNAME cheese.local.

Create a file /etc/httpd/conf.d/plex.conf containing the following.

NameVirtualHost *:80
<VirtualHost *:80>
   ServerName cheese.local

<VirtualHost *:80>
    ServerName plex.local

    <Proxy *>
        Order deny,allow
        Allow from all

    ProxyRequests off
    ProxyPreserveHost On
    ProxyPass / ws://
    ProxyPassReverse / ws://
    ProxyPass / wss://
    ProxyPassReverse / wss://
    ProxyPass /
    ProxyPassReverse /

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

We need to preserve the host cheese so its webserver is not affected so we place this host first, then we define how the plex hostname works. The crucial piece of the puzzle is the QUERY_STRING conditional. We do NOT want to redirect “/?something” into “/web?something” as this will screw things up.

The inclusion of the WebSocket proxying is also crucial for activity feedback. This requires the module which is available with apache 2.4+

As I was running CentOS 6.5 x64 which only has apache 2.2.15 I had to compile this module up myself by following the instructions here:

For your convenience the precompiled module which can be unzipped and dropped into /usr/lib64/httpd/modules you may need to chmod this to 755 after unzipping don't forget to enable this as well.

Edit apache configuration to support websockets

# vi /etc/httpd/conf/httpd.conf
LoadModule proxy_wstunnel_module modules/
  • Disable SELINUX
  • Punch hole in firewall (or disable easier).
  • No need to install mod_proxy_wstunnel.

I find it useful to know how somebody managed to figure out what everybody else is struggling with so here we go.

This additional QUERY_STRING conditional was figured out by enabling debug on the rewrite rules by putting this into the virtual host rewrite section.

RewriteLog "/var/log/httpd/rewrite.log"
RewriteLogLevel 3

Now we can see what is happening and why it's going wrong. A request to / with a get query string was being redirected and we don't want that. - - [28/Jul/2014:12:07:53 +091800] [plex.local/sid#7f22bc664b68][rid#7f22bc70c778/initial] (3) applying pattern '^/$' to uri '/' - - [28/Jul/2014:12:07:53 +091800] [plex.local/sid#7f22bc664b68][rid#7f22bc70c778/initial] (2) rewrite '/' -> '/web/' - - [28/Jul/2014:12:07:53 +091800] [plex.local/sid#7f22bc664b68][rid#7f22bc70c778/initial] (2) explicitly forcing redirect with http://plex.local/web/ - - [28/Jul/2014:12:07:53 +091800] [plex.local/sid#7f22bc664b68][rid#7f22bc70c778/initial] (1) escaping http://plex.local/web/ for redirect - - [28/Jul/2014:12:07:53 +091800] [plex.local/sid#7f22bc664b68][rid#7f22bc70c778/initial] (1) redirect to http://plex.local/web/?X-Plex-Client-Identifier=68igxy5
41z4&X-Plex-Product=Plex+Web&X-Plex-Device=Windows&X-Plex-Platform=Firefox&X-Plex-Platform-Version=26.0&X-Plex-Version=2.1.12&X-Plex-Device-Name=Plex+Web+(Firefox) [REDIRECT/3

Once you see that its a small step to know you need a conditional that won't redirect / when a query string is present.

[...] [...]
  • blog/plex_virtual_hosting.txt
  • Last modified: 2019/12/13 21:18
  • by brett