DEV Community

adhanaAshu
adhanaAshu

Posted on • Originally published at yuki0.hashnode.dev

Nginx Basics

what is Nginx?

  • primarily one of the fastest web servers but can also be used as a proxy, reverse proxy, API Gateway, content cache and load balancer.

  • Its usage as a reverse proxy and load balancer is what makes it stand out.

  • NGINX efficiently handles tasks that might slow down your web server, such as negotiating SSL/TLS or compressing and caching content to improve performance.

Elements of Nginx configuration file

  • Nginx is very simple to install and once installed you can start by editing the config file which will usually exist in /etc/nginx or /usr/local/etc/nginx

  • nginx.conf is the text-based configuration file for Nginx.

  • It contains elements called Directives and Contexts. Directives are written like key-value pairs on each line ending with semicolons(;) Context on the other hand are top-level directives that apply to different kinds of incoming traffic.

    • events – General connection processing
    • http – HTTP traffic
    • mail – Mail traffic
    • stream – TCP and UDP traffic
  • Some directives use { } to group directives together these are called blocks. One of such important directives is the Location [modifier] [URI]{ ... } this directive is used to route the incoming traffic to various file systems.

  • Examples of a few directives are given below.

# These are directives key value; like pairs
user             nobody;
error_log        logs/error.log notice;
worker_processes 1;
Enter fullscreen mode Exit fullscreen mode
  • Every time you make any changes to the nginx.conf , the nginx must be reloaded to take effect. you can use the command nginx -s reload

Few important directives to discuss

  • include directive: This directive includes another file inside the config file. include mime.types is one of the popular usages, a web server needs to know what kind of data is incoming, and this data is embedded into the headers of the incoming requests. nginx, suggest using this directive to break down your config files into simpler manageable files.

    http { 
    include mime.types; 
    
        server {
            . . .
        }
    }
    
  • location block:This directive routes the requests to the correct location within the file system by matching the URI against the parameter . It is one of the most important directives to learn. each URL is matched against the location block to determine what content it will serve.

    # the uri : / is the parameter to the location block.
    # users visiting localhost:8080/ would be served content from this block
    http {
        server { 
                .
                .
                location / {
                    ...
                }    
                .
                .
        }
    }
    
  • root directive: The parameter /draft gets appended to the root location folder.

    URL gets matched against /draft here and then nginx serves content from the location /users/rocky/site/draft/

    # users visiting localhost:8080/draft would be served content from this block
    http {
        server { 
                .
                .
                location /draft {
                    root /users/rocky/site; 
                }     
                .
                .
        }
    }
    
  • alias directive: The parameter /test gets dropped and the nginx serves content from /users/rocky/site/draft . It allows us to remap URLs to a different directory completely other than the root location. It is useful for serving static files from a different directory

     # users visiting localhost:8080/test would be served content from this block
    http {
        server { 
                .
                .
                location /test {
                    alias /users/rocky/site/draft; 
                }    
                .
                .
        }
    }
    
  • try_files directive: This directive recursively search for files in a specific order and serves the file located first. we've mentioned here =404 meaning we don't let it run forever, if the server doesn't find the index.html in a single pass, it would throw the error 404.

    # users visiting localhost:8080/draft would be served content from this block
    http {
        server { 
                .
                .
                location /draft {
                    try_files /drafts/index.html /default/index.html =404;
                }  
                .
                .
        }
    }
    
  • redirect and rewrite directives: if a user wants to go to /final but we want the page to redirect to /draft it is very simple to do. Look at the below example to achieve this.

    http {
        server { 
                .
                .
                location /final {
                    # 307 is a http redirect code 
                    # we are redirecting from final to draft
                    # users visting localhost:8080/final 
                    # would be redirected to localhost:8080/draft
                    return 307  /draft
                }  
                .
                .
        }
    }
    

    In the previous example if you observe the URL changes from /final to /draft but what if we don't want the URL to change, this kind of behaviour is where we can make use of the rewrite directive. Look at the below example to achieve this

    http {
        server { 
                .
                .
                # anybody visiting localhost:8080/final 
                # will be redirected to /draft 
                # but the url will remain the same i.e. localhost:8080/final
                rewrite ^/final? /draft break; 
                location /draft {
                    ...
                }  
                .
                .
        }
    }
    

These are just a few of the directives to get you a bit familiar with nginx. Users can go through the nginx docs to learn in-depth about the intricacies of the nginx here

References

Top comments (0)