Simple Configuration of NGINX on MAC: Part (2)

Nginx From Beginner to Advanced

This is the 2nd part in the series of NGINX on Mac. You can visit the 1st part here. In this part we are going to understand the configuration file of Nginx and tweak as per our requirements.

Configuration file

Your Nginx configuration file will be located at . You can fire to run a test configuration just to verify the location. Open the nginx.conf file to see it's contents. I usually use Microsoft's VS Code to edit any text files, so I ran the below command to open the file in VS Code. You can use any text editor you want.

code /usr/local/etc/nginx.conf

Once open you will see a the config file already has content in it most of which is commented out. You can clear everything so that we can take one step at a time. Run to test the configuration file we just cleared out. It will give an error saying which simply means there is no events context. Add the events context as follows:

events {}

The events context helps you with configuration of the server like how many worker connections can be open at the same time and how polling happens among them.

Now when you run you will not get any error but when you open http://127.0.0.1:8080 you will be faced with error since we have not set our server locations yet. Time to do that!

Below your events context add another context with name . Inside http context add context with name . Finally, inside server context add context named . It should look like below.

events {
}
http {
server {
# We are configuring for the / location
location / {
}
}
}

We have created a skeleton inside which we will now put some flesh and bones. Add a directive inside the location context with a path pointing to your html directory. For demo purposes I have created a folder on my Desktop. You can place your folder where ever you like. In the folder I have created an index.html with content . Here is the overall config file

events {}http {
server {
# listen on the port 8080
listen 8080;
# When 127.0.0.1:8080 is visited, serve content from www on Desktop
location / {
root /Users/arjav/Desktop/www;
}
}
}

That’s it!!! This is the simplest configuration you can have in nginx. From your terminal run to reload nginx and visit http://127.0.0.1:8080 in your browser and you will see a nice big HELLO WORLD. Press Cmd+Shift+R to hard refresh your browser in case you are seeing some cached content.

Next -> Configure self-signed SSL.

Royale Cheese: Co-Founder & Technical Director

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store