Nginx has a master process and as many worker processes as the admin/user of Nginx wants.
- Master process manages the worker process.
- It read the configuration file.
- It evaluate the configuration file and accordingly update the active services.
- Now all the incoming requests will be processed by these worker thread.
- nginx uses event-based model and OS-dependent mechanisms for the requests distribution to worker threads.
Basic Nginx Commands
Start the Server by running the executable file in Nginx folder.
- nginx -s stop => for fast shutdown
- nginx -s quit => for graceful shutdown
i.e., first let the worker thread finish the work, who are currently executing and then stop the execution of the server.
- nginx -s reload => for reloading the configuration file
i.e., if changes are made to the configuration file and now they are to be reflected by the nginx server, use reload command.
The validity, semantics and syntax of the config file will be checked before applying the changes.
If config file is correctly updated then all old worker threads will be shut down and new worker threads will be started.
If config file is not valid then old instance will keep on running.
- nginx -s reopen => for reopening the log files
- ps -ax | grep nginx => for displaying nginx processes in running state.
Structure Of Configuration File
Nginx config file is made up of Directives.
Directives is of two type:
- Simple Directive
- It can be defined by its name and parameter, separated by space and ended in semicolon.
- Eg: name parameter;
- Block Directive
- They define directives within a block and are referred as context.
- Eg: server, http, location etc.
Directives outside any context and placed within the config file will come into the main context.
Eg: events, http directives are present in this main context.
Some of the Use Cases
Serving Static Content
Nginx being a server has the capability of serving static content like static html pages, images etc.
Suppose a request is sent from http://localhost/images/image.png response will be sent from /data/images/images.png file.
If this file does not exist in the specified folder then, nginx will send 404 error response.
Use nginx -s reload to start the server with new configurations.
error_page 404 /404.html;
this directive specified in the config file specifies where to look for the error pages when error is 404.
error_page 500 502 503 504 /50x.html;
if 500, 502, 503 or 504 error has occurred then where to look, is specified by this directive again present in config file.
Most of the configurations are already present inside the configuration file and are just needed to be
Simple Proxy server
Here one server receives the request and passes the request forward to some other server, where actual processing will start.
Responses will now be sent back to these proxy servers and then to the clients.
Here the proxy server is working on port number 80.
Now, if a client hit the url http://localhost:80 then the request will be first sent to the nginx server that is working on port 80, but instead of looking for html files or images the server witnessed a proxy_pass directive, which is asking for passing the specified request to the http://localhost:3000.
Hence now the request will be served from this server at port 3000 rather than 80.
Load Balancer Configuration
Nginx can work as a Load Balancer as well.
Here a we can have various servers defined inside the upstream directive followed by the name of the load balancer.
This name know will be used by the user to make a request and all the incoming load will be distributed to the servers defined within the upstream directive.
This load balancer has to be passed as a parameter to the proxy_pass directive within the location context inside the server context defining this load balancer.