Running Nginx in front of a Play app

Handwritten on 12 Oct 2015

Running Nginx in front of a Play app

In my last blog post I have explained how to install Nginx on OSX El Capitan.
In this post we will try to make Nginx act as our frontend web server for a Play app.

So, what will we Play?

Play is a Java / Scala web framework. Play has a built in web server that is listening, by default, on port 9000. You can change this, just read the documentation. But for now we need to tell Nginx to forward all the requests to localhost.

Configure Nginx

The default Nginx configuration file is stored in: /usr/local/etc/nginx/nginx.conf. Let's open this file, because we need to edit it. I prefer an editor instead of a command line editor like vi. So I will open it with Atom:

open -a Atom /usr/local/etc/nginx/nginx.conf

Unlike the Apache configuration file (httpd.conf) this one has fewer lines. This does not necessarily mean we have fewer options, I hope, but it surely means that I can read it :-).
Let's find the lines that contain the following (should be around line 43):

location / {
    root   html;
    index  index.html index.htm;
}

We need to replace this by the following (Note that I like to save the original content, therefore I add my comments inline):

# START: Added by adis on 2015-10-13
proxy_buffering    off;
proxy_set_header   X-Real-IP $remote_addr;
proxy_set_header   X-Scheme $scheme;
proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header   Host $http_host;
proxy_http_version 1.1;

upstream my-backend {
   server 127.0.0.1:9000;
}

upstream my-backend {
   server 127.0.0.1:9000;
}

# END: Added by adis on 2015-10-13

# START: Added by adis on 2015-10-13

And:

#location / {
#    root   html;
#    index  index.html index.htm;
#}

location / {
   proxy_pass http://my-backend;
}

Ok, let's restart Nginx, to load it's new configuration file and there you go! You have just setup Nginx to act as a frontend proxy server for your Play app!

nginx play prox

Logo Adis.me

© 2024 Handcrafted in Nijmegen, The Netherlands