DEV Community

Cover image for Intro to NGINX
zachmarullo
zachmarullo

Posted on

Intro to NGINX

What is NGINX?

NGINX is a powerful and popular open-source web server that is known for its ability to handle a high volume of connections simultaneously while maintaining excellent performance. One of the primary benefits of using a reverse proxy is that it can distribute incoming requests to multiple backend servers, allowing you to scale your application or website more easily. It also provides security by hiding the IP addresses of your backend servers from the internet, and can improve performance by caching frequently-requested content.

NGINX History and More

NGINX was developed by Igor Sysoev in 2002 and has since become a widely-used server software. NGINX is able to process a large number of requests simultaneously, which makes it a great choice for high-traffic websites. It is usually installed on a Linux server and its configuration is often stored in the "etc" directory.

By default, the nginx configuration file can be located in:

/etc/nginx/nginx.conf,  
/usr/local/etc/nginx/nginx.conf, or  
/usr/local/nginx/conf/nginx.conf  
Enter fullscreen mode Exit fullscreen mode

NGINX Basic Configuration

The NGINX configuration file is the main configuration file for the NGINX web server. It specifies how NGINX should behave in different scenarios, including how it should respond to incoming requests and how it should interact with other servers or services. Below is an example of a really basic NGINX configuration file that listens for HTTP requests:

server {
    listen       80;
    server_name  localhost;

    location / {
        root   html;
        index  index.html index.htm;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
}
Enter fullscreen mode Exit fullscreen mode

Above, the server block specifies the main configuration for the server. The listen directive tells NGINX to listen for incoming requests (on port 80 in this case). The server_name directive specifies the server name that should be used in the "host" header of the HTTP request. The location blocks tell NGINX where to look for different URI paths. The error_page directive tells the location of the error page that should be displayed for HTTP error codes such as 500, 502, 503, and 504. The location block for the error page specifies that the error page should be served from the html directory. This is only a basic configuration and only covers the most essential features NGINX has to offer. Developers are able to extend and customize the configurations to suit specific needs by using additional server blocks, and/or using directives.

Directives

As a developer, you have a lot of control over the behavior of your NGINX server through the use of directives. Directives are key-value pairs that provide instructions for the server, and they can be used to customize a wide range of features. For example, you can use directives to specify the location of your raw files on the file system, set up SSL certificates, implement rewrites, and route requests to a proxy server. Below is an example of an NGINX directive that sets the maximum size of the client request body:

client_max_body_size 50m;

Enter fullscreen mode Exit fullscreen mode

This directive specifies that the maximum size of the client request body is 50 megabytes. This can be used to limit the size of file uploads amongst other things. Limiting the size of file uploads can have a few advantages:

  • Preventing abuse: Limiting the maximum size of the client request body can help to prevent malicious actors from attempting to abuse your server by sending excessively large requests. This can help to protect your server from resource exhaustion and other types of attacks.

  • Improving performance: Allowing clients to send excessively large requests can consume a significant amount of server resources, which can negatively impact the performance of your server. By limiting the maximum size of the client request body, you can help to ensure that your server has sufficient resources available to handle other requests.

What exactly does NGINX do?

One of the primary responsibilities of NGINX is serving static content such as images and HTML. NGINX keeps track of requests to the server through an access log. An access log is a record of all incoming requests to the server. This log can be useful for debugging issues, analyzing traffic patterns, and monitoring the performance of your server.

What Companies use NGINX?

NGINX is a popular and widely-used web server software that is trusted by many well-known companies. Some of these include:

  • Netflix: Netflix uses NGINX as a reverse proxy to distribute incoming requests to its back-end servers and improve the performance of its streaming service.

  • Airbnb: Airbnb uses NGINX to improve the performance of its website and mobile apps even with millions of users.

  • Hulu: Hulu uses NGINX to improve the performance and scalability of its streaming service.

  • Instagram: Instagram uses NGINX as a reliable and high-performance web server to handle incoming requests and improve the performance of their platform.

NGINX Companies

Conclusion

NGINX is a versatile and performant web server that is well-suited for handling a large volume of traffic. Its asynchronous event-driven architecture allows it to handle a large number of connections efficiently, and its customizable directive system allows developers to fine-tune its behavior to meet their specific needs.

Sources:
NGINX Javatpoint
NGINX Docs
Free Code Camp NGINX Introduction

Top comments (0)