DEV Community

loading...
Cover image for Simple Configuration of NGINX on Mac

Simple Configuration of NGINX on Mac

arjavdave profile image Arjav Dave ・2 min read

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
/usr/local/etc/nginx/nginx.conf. You can fire

nginx -t

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
Enter fullscreen mode Exit fullscreen mode

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

nginx -t

to test the configuration file we just cleared out. It will give an error saying

no "events" section in configuration

which simply means there is no events context. Add the events context as follows:

events {

}
Enter fullscreen mode Exit fullscreen mode

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

nginx -t

you will not get any error but when you open http://127.0.0.1:8080 you will be faced with

This site can’t be reached

error since we have not set our server locations yet. Time to do that!

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

events {
}

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

        }
    }    
}
Enter fullscreen mode Exit fullscreen mode

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

<h1>HELLO WORLD</h1>

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;
        }
    }    
}
Enter fullscreen mode Exit fullscreen mode

That's it!!! This is the simplest configuration you can have in nginx. From your terminal run

nginx -s reload

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.

Discussion (0)

Forem Open with the Forem app