Setup Nginx in Mac OS

Prakhar Gurawa
2 min readMar 12, 2020

In this article, we will set up Nginx. The operating system used is Mac OS Mojave Version 10.14.5.

Nginx is basically a web server, which can be used as a load balancer, reverse proxy, HTTP Cache, etc. You can read this article to have an overview:

https://kinsta.com/knowledgebase/what-is-nginx/

First, let us install brew which acts as a package manager for Mac OS.

Open the mac terminal and use the command:

ruby -e “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Note: You need to install Xcode before installing brew.

After this, we will install Nginx using the command:

brew install nginx

After the installation you can see a new folder is created at location:

/usr/local/etc/nginx

This folder contains the nginx.conf file, which is the configuration file of Nginx. You can talk to Nginx through this file.

To start the Nginx just use the command:

sudo nginx

You can test the working at the URL:

http://localhost:8080

Note: If you are testing in safari, just clear the cache every time you reload the Nginx. This is just for better working.

To stop the Nginx:

sudo nginx -s stop

If you make changes and want to reload the Nginx:

sudo nginx -s reload

You can read more about its working using the help command:

sudo nginx -h

To test Nginx working:

sudo nginx -t

If you want your own custom page, rather than the default Nginx, you can go to the directory:

/usr/local/var/www

Here you can modify the index.html, according to your need. You can have CSS or images in this folder, which can be linked to your Html code.

Now you can store a copy of the original nginx.conf file as a backup. As we will be changing it for learning purposes.

Now change the content of nginx.conf file to following:

This file is conveying that we are changing the port to 80 and created a virtual host, which is working at our IP address.

Try accessing :

<your ip address>

Which will give the index file as output. After this try accessing:

<your ip address>/hello

Which will give hello world as result. This is how routing works in Nginx.

You can explore more about prefix match, exact match, regex match, variable declaration, rewrite and redirects.

Maybe in future, I will write on logging, rate-limiting, worker processes, and load balancing.

Thank you.

--

--

Prakhar Gurawa

Data Scientist@ KPMG Applied Intelligence, Dublin| Learner | Caricaturist | Omnivorous | DC Fanboy