Mopify Behind Apache Reverse Proxy

I’ll be writing a post about how great Mopify is shortly but essentially it’s a web plugin for Mopidy which provides a web front end to Spotify running on just about any machine. It means you can control Spotify running on a machine plugged into your HiFi without the needs for the Spotify App on your device. This is handy where you have other people in the house with their own accounts but still want them to be able to control the music on the main HiFi.

spotify-tile

Of course, an ideal host machine might well be a Raspberry Pi with a decent USB Soundcard.

Mopidy runs its own little web server which means it needs its own little port to listen on. That’s all good and well but it’s likely you already have a web server running on your Pi. For me this is the ye old Apache. As cumbersome as it is for some reason I still prefer it for most tasks.

Apache has a nice module called mod_proxy, which, when enabled allows you to have Apache listening and serving content on the front end, while in the background it is proxying requests to another server – in this case Mopidy.

For this to work with Mopidy we need first to ensure the modules are loaded:

a2enmod proxy
a2enmod proxy_http
a2enmod proxy_wstunnel
a2enmod rewrite

Next we need to configure a virtual host from which will proxy the requests to Mopidy.

I have not yet got this working properly with a subdirectory of an already enabled vhost. The reason for this is subdirectories and how they are handled in a proxy configuration.

So, here is what our shiny new virtual host will look like:

 ServerAdmin [email protected]
 ServerName mopidy.domain.com
 ErrorLog /dev/null
 CustomLog /dev/null combined
 RewriteEngine on
 RewriteCond %{REQUEST_URI} ^/$
 RewriteRule (.*) /mopify/ [R=301]
 ProxyPass /mopidy/ws ws://192.168.0.100:6680/mopidy/ws
 ProxyPassReverse /mopidy/ws ws://192.168.0.100:6680/mopidy/ws
 ProxyPass /mopify/queuemanager ws://192.168.0.100:6680/mopify/queuemanager
 ProxyPassReverse /mopify/queuemanager ws://192.168.0.100:6680/mopify/queuemanager
 ProxyPass / "http://192.168.0.100:6680/"
 ProxyPassReverse / "http://192.168.0.100:6680/"

There’s a few things going on here but the important part is the Rewrite/Proxy section. The example assumes Mopidy is listening on 192.168.0.100 port 6680 (default port). You will likely be able to use localhost instead of the LAN IP of your machine.

Issue a cheeky Apache reload…

service apache2 reload

… and you’re done! Visit http://mopidy.domain.com and you should see Mopify as usual.

Note: In Mopify’s Settings page (within the web UI) you may need to set the Port to that of your Apache web server (80 or 443 etc).

One Comment

Comments are closed.