Nginx (SSL) proxy
Posted: Sat Aug 18, 2012 10:55 am
I run subsonic at home and I thought I'd share my configuration. In this case nginx is the httpd which runs on port 443 and proxies to all kinds of webapps I run locally.
My home webserver already runs on a subdomain for which I have an SSL certificate. Because of this I want to have subsonic at /subsonic and not as another subdomain as my SSL certificate wouldn't be valid.
There's two parts, first:
Subsonic is bound to 127.0.0.1, since we can access it from anywhere in the world through the HTTP proxy and the streaming clients also support this there's no reason to bind the daemon to listen on anything else than localhost.
I'd also advise anyone to just create a separate user for subsonic, there's no need to run subsonic as root as long as the subsonic user can read (and write if you want to modify tags) the music folder(s).
Now for the nginx part:
There's no real need for the location / block but I happen to have an index page there plus it's a fairly OK example of how try_files should be used.
The reason I separated the proxy_* directives into a separate file is that you need those settings for just about any app you want to proxy like this. It's cleaner to just have one line for the include instead of having those for lines for every location /someapp block.
This can easily be expanded to include HTTP Basic auth like so:
This is a neat little trick because it will only require HTTP authentication if you're not coming from you internal network. Unfortunately, because you can't pass HTTP authentication onto Subsonic you'll still be hit with the Subsonic login page either way. Because of that you might as well not have the basic authentication enabled for the Subsonic proxy.
My home webserver already runs on a subdomain for which I have an SSL certificate. Because of this I want to have subsonic at /subsonic and not as another subdomain as my SSL certificate wouldn't be valid.
There's two parts, first:
- Code: Select all
/etc/default/subsonic
SUBSONIC_HOST=127.0.0.1
SUBSONIC_PORT=8083
SUBSONIC_CONTEXT_PATH=/subsonic
SUBSONIC_ARGS="--max-memory=100 --host=${SUBSONIC_HOST} --port=${SUBSONIC_PORT} --context-path=${SUBSONIC_CONTEXT_PATH}"
SUBSONIC_USER=a_non_root_user
Subsonic is bound to 127.0.0.1, since we can access it from anywhere in the world through the HTTP proxy and the streaming clients also support this there's no reason to bind the daemon to listen on anything else than localhost.
I'd also advise anyone to just create a separate user for subsonic, there's no need to run subsonic as root as long as the subsonic user can read (and write if you want to modify tags) the music folder(s).
Now for the nginx part:
- Code: Select all
/etc/nginx/sites-available/glados
server {
listen [::]:443 ssl;
server_name home.mydomain.com;
ssl_prefer_server_ciphers on;
ssl_protocols SSLv3 TLSv1;
ssl_session_timeout 5m;
ssl_certificate /etc/nginx/ssl/mydomain.com.chain;
ssl_certificate_key /etc/nginx/ssl/mydomain.com.key;
root /usr/share/nginx/www;
index index.html index.htm;
client_max_body_size 20M;
location / {
root /var/www;
try_files $uri $uri/ =404;
}
location /subsonic {
include conf.d/params/proxy.conf;
proxy_pass http://127.0.0.1:8083;
}
[.. other apps .. ]
}
- Code: Select all
/etc/nginx/conf.d/params/proxy.conf
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
There's no real need for the location / block but I happen to have an index page there plus it's a fairly OK example of how try_files should be used.
The reason I separated the proxy_* directives into a separate file is that you need those settings for just about any app you want to proxy like this. It's cleaner to just have one line for the include instead of having those for lines for every location /someapp block.
This can easily be expanded to include HTTP Basic auth like so:
- Code: Select all
location /subsonic {
satisfy any;
allow my_ipv4_home_range/24;
allow my_ipv6_block/64;
auth_basic "Subsonic Authentication Required";
auth_basic_user_file /etc/nginx/auth/subsonic;
[..] the rest of the configuration as displayed about [..]
}
This is a neat little trick because it will only require HTTP authentication if you're not coming from you internal network. Unfortunately, because you can't pass HTTP authentication onto Subsonic you'll still be hit with the Subsonic login page either way. Because of that you might as well not have the basic authentication enabled for the Subsonic proxy.