DEV Community

Cover image for How to Configure and Control Nginx?
Ganesh Kumar
Ganesh Kumar

Posted on

How to Configure and Control Nginx?

Hello, I'm Ganesh. I'm building git-lrc, an AI code reviewer that runs on every commit. It is free, unlimited, and source-available on Github. Star Us to help devs discover the project. Do give it a try and share your feedback for improving the product.

In previous article I explinaed how nginx works and what is the architecture of nginx.

Now let's configure it and controls nginx.

Understanding Nginx Controls

  1. Reload
nginx -s reload
Enter fullscreen mode Exit fullscreen mode

This signal is used to reload the configuration files of nginx.
It will not stop the nginx processes.
It will just reload the configuration files and start the new worker processes.
If old worker process serving few clients it will be served by old worker process.

Either master will stop old worker process or it will wait for the old worker process to finish serving the current requests.

This way if anything has issues nginx will not apply new configuration files.

  1. Quit
nginx -s quit
Enter fullscreen mode Exit fullscreen mode

This signal is used to quit the nginx processes.

It will stop the nginx processes and wait for the worker processes to finish serving the current requests.

  1. Stop
nginx -s stop
Enter fullscreen mode Exit fullscreen mode

This signal is used to stop the nginx processes.

It will stop the nginx processes and wait for the worker processes to finish serving the current requests.

To stop nginx with PID. We can use kill command.

kill -QUIT <master_pid>
Enter fullscreen mode Exit fullscreen mode

We can also find all PID

ps -ax | grep nginx
Enter fullscreen mode Exit fullscreen mode

Configure Nginx

nginx consists of modules which are controlled by directives specified in the configuration file.

These Directives are divided into simple directives and block directives.

Similar to C language, simple directive consists of the name and parameters separated by spaces and ends with a semicolon (;).

A block directive has the same structure as a simple directive, but instead of the semicolon it ends with a set of additional instructions surrounded by braces ({ and }).

If a block directive can have other directives inside braces, it is called a context (examples: events, http, server, and location).

Directives placed in the configuration file outside of any contexts are considered to be in the main context. The events and http directives reside in the main context, server in http, and location in server.

The rest of a line after the # sign is considered a comment.

Here is the simple example of nginx configuration file:

Main context: worker_processes 1;

events context: worker_connections 1024;

http context: we define server context here.

server context: In here we define this to listen port 80 which is http port.

location context: In here we define the root directory of the website.

So, any request coming to port 80 will be served by the worker process with the root directory of /var/www/html.

# This is a comment in the Main Context
worker_processes 1; 

events { 
    # This is a simple directive inside the 'events' context
    worker_connections 1024; 
}

http { 
    # We are now inside the 'http' context

    server { 
        # The 'server' context is nested inside 'http'
        listen 80; 

        location / { 
            # The 'location' context is nested inside 'server'
            root /var/www/html; 
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Conclusion

We understood how to control nginx and how to configure nginx.

So, this makes solid foundation for us to understand how work with nginx.

git-lrc

Any feedback or contributors are welcome! It’s online, source-available, and ready for anyone to use.
⭐ Star it on GitHub: https://github.com/HexmosTech/git-lrc

Top comments (0)