Nginx all the things!

Nginx proxy

Why would we wanna do that?  Because developers are increasingly working across multiple projects, and we need a sane method of changing contexts within our local development environment very quickly – or running them all at once – to remain efficient.  Our approach is to use an Nginx proxy to forward all requests on localhost:80 to our various applications, each running on their own unique port.

As coders make contributions to upstream dependencies and neighboring apps alike, and as we write more end-to-end automated browser tests that cross application boundaries, running a local Nginx proxy will be a requirement.

Ok, so let’s get you up and running …

Installing Nginx

We’ll use HomeBrew to install Nginx on our Mac.  This makes it easy to upgrade in the future, doesn’t require much manual configuration or running make, and doesn’t mess with any system files.

HomeBrew

Install nginx using the HomeBrew tap and formula.

Below is an example of the output you should expect after running this command, including some helpful tips for starting/stopping the server.

Configure Permissions

By default, Nginx will run on port 8080 so as to not require sudo.  But we’re going to want to run on port 80 to better simulate the production experience and route all requests properly, so we’ll need to set permissions on our directory appropriately.

Configure nginx.conf

You’ll need a robust configuration file to dynamically map incoming requests based on URL path to the appropriate apps running on different ports.  Many apps run on port 8080 by default, and some apps are easier than others to change, but we’ll need to run each app on its own port.

If you installed nginx via Homebrew, your nginx.conf file can be found at /usr/local/etc/nginx/nginx.conf.

Generate Self-Signed SSL Certificates

You’ll need to create a directory for your certs, then run the commands to generate them.  Notice these files are being created in the ssldirectory noted in your nginx.conf.

Start the Server

From your terminal, let’s start up nginx and make sure there are no errors returned:

Start Your Nginx Proxy

Each time you make changes to your nginx.conf file, you’ll need to reload the web server and ensure no errors were returned:

Reload Your Nginx Proxy

To stop the server, send the “stop” signal:

Stop Your Nginx Proxy

Start Up Your Apps

You should now be able start up each of your apps concurrently!  However, to do so you may still need to start those apps on the ports you specified with location directives, so check the your app’s README for how to do that.

As an example, within a typical Node.js app, it’s as simple as setting the port environment variable:

That’s the gist of it, we’re simply using Nginx to proxy all requests on port 80 to the various apps running locally on alternate ports.

Got questions, or even a better way to do any of this?  Please let me know in the comments section below!

Author: Grant Norwood

Designer of things you click. Vegetable rights activist. Volunteer with tiny humans. Austin-ish, Texas.

People describe me as a rad web developer, product manager, solutions architect, public speaker, volunteer, and non-profit board member.

Did you know you can leave a comment?