Introducing NGINX and core concept

Tram Ho

1. What is NGINX?

NGINX is an open source software used as a web server, reverse proxy, caching, load balancing, media streaming, … Originally, it was designed to make web server highly efficient and stable. In addition to communicating by HTTP, NGINX can also act as an email server (IMAP, POP3, SMTP), reverse proxy, load balancer for servers using HTTP, TCP and UDP.

2. Biography

Initially, Igor Sysoev wrote NGINX to solve the 10k problem (C10k_problem), a term coined in 1999 to describe the problems faced by web servers of the time. It is handling large amounts of concurrent connections (C10k stands for concurrent ). With the architecture of event-driven (event-driven) and asynchronous (asynchronous), NGINX has made a revolution by becoming the fastest web server at that time.

After opening the source of the project in 2004 and seeing an exponential increase in user trust. Sysoev founded NGINX, Inc. to further support the development and commercialization of NGINX Plus with enterprise capabilities. Today, NGINX and NGINX Plus can handle hundreds of thousands of connections at once and are the foundation of more than 50% of the most visited websites on the internet.

3. NGINX as a Web Server

The ultimate goal of NGINX is to create the fastest web server and maintaining the current NGINX is still a major goal of the project. NGINX has consistently outperformed Apache and other web servers in terms of performance. Since the official launch of NGINX , the website has expanded from simple HTML pages to diverse and flexible content. NGINX has evolved with it and now supports all components of the modern web, including WebSocket, HTTP / 2, the ability to stream multiple video formats like HDS, HLS, RTMP, …

4. The story behind NGINX

Although NGINX has a reputation as being the fastest running web server, its scalable architecture has proven to be ideal for web servers in addition to serving content . Since it can handle large amounts of connections, NGINX is often used as a reverse proxy and load balancer to manage traffic and deliver them to slower servers (from legacy database to microservices ).

NGINX is also usually placed between the client and the primary server, to decrypt SSL / TLS or to speed up the web, acting as an intermediary server. NGINX effectively handles tasks that can slow down the server like decrypting SSL / TLS or compressing and caching content for increased performance. Dynamic websites often implement NGINX as a caching and reverse proxy in order to offload the servers and utilize the hardware most efficiently.

5. Important directories and commands

5.1. Files and folders

  • /etc/nginx
    • The /etc/nginx directory is the root directory for NGINX default configuration settings. In this directory you will find config files describing how NGINX works.
  • /etc/nginx/nginx.conf
    • This is NGINX ‘s default config file. This config file will set global things like worker process, tuning, logging, load module or references to other config files. By default, /etc/nginx/nginx.conf contains the top level block – http – which will add other config files in the directory below.
  • /etc/nginx/conf.d/
    • This is the directory that contains the default config files that use HTTP. Files with the .conf extension will be added to the /etc/nginx/nginx.conf file. In other versions, this folder was named sites-enable but this convention was removed.
  • /var/log/nginx/
    • This is NGINX ‘s default log directory. In this directory you will find access.log and error.log . The file access.log contains the information of each request to the NGINX server. The error.log file contains errors and debug information.

5.2. Important commands

  • nginx -h
    • NGINX help menu will appear
  • nginx -v
    • Show version of NGINX
  • nginx -V
    • Display NGINX version, build, configuartion, module, …
  • nginx -t
    • The command is used to test whether the NGINX config is correct or not
  • nginx -T
    • Like nginx -t and shows more help on the correct config
  • nginx -s signal
    • Option -s will send a signal to NGINX ‘s total process. You can send signals such as: stop , quit , reload , reopen . The stop signal will stop NGINX immediately, quit will stop NGINX as soon as it completes the current request, reload will reload the config, reopen used to reopen the log file.

With the above core knowledge of files, directories, and commands, you are now ready to practice with NGINX ! Just try to edit the config file and test with nginx -t if the test is successful, you should use nginx -s reload to reload the changes again.

6. Serving Static Content

Now let’s configure to serve static content with NGINX , rewrite the /etc/nginx/conf.d/default.conf file with the following content:

This config will serve static file by HTTP over port 80 from /usr/share/nginx/html/ .

  • The first line is defined as a server block – a context for NGINX listen.
  • The second line instructs NGINX to listen at port 80 and the default_server parameter tells NGINX to use this server as the default server for port 80.
  • On the third line, the server_name directive defines a hostname or names that requests will point to this server.
  • location block defines a config based on the path of the URL (also known as the URI). NGINX will match the request’s URI to the location block. The above example uses / to match all requests.
  • root directive tells NGINX which directory is the directory to search for the returned file for the client.
  • Finally, the index directive will give NGINX a file or a list of files to check and return to the client.

Above is the brief introduction and main concepts, hope you have a more overview of NGINX.

7. References

Share the news now

Source : Viblo