Configuring nginx for an Ember frontend and a Rails backend
One of the projects I'm working on at the moment is a todo list app, with an Ember.js frontend and a Rails API backend. Since it's something that only I'm going to be using (at least for the moment) I decided to keep things simple and just use basic HTTP authentication. With two pieces, I thought the easiest thing to do would be to host both parts on the same subdomain, so that I wouldn't have to deal with CORS and trying to forward the authentication.
I configured the routes for the Rails part to all start with api/v1
, so that makes it easy to distinguish requests for the API from requests for the frontend. While I was at it, I also wanted to leave "real" URLs for the various routes in the Ember app, instead of using hashbang URLs.
upstream todo-api {
server unix:/tmp/todo-api.unicorn.sock fail_timeout=0;
}
server {
listen 80;
server_name example.com; # replace with the actual server name
root /srv/sites/todolist/www;
access_log /srv/sites/todolist/logs/access.log;
error_log /srv/sites/todolist/logs/error.log;
auth_basic "Todolist";
auth_basic_user_file "/srv/sites/todolist/htpasswd";
location /api {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://todo-api;
}
location / {
try_files $uri $uri/ /index.html?/$request_uri;
}
}