Skip to content

Nginx Basics

Links: 112 Nginx Index


NGINX Basics

Location of nginx configuration files in docker: /etc/nginx

Configuration file: nginx.conf

  • The key value pairs in nginx are known as directives.
    • Directives must end with semicolon.
    • attachments/Pasted image 20221103201502.jpg
  • The blocks of code in the curly braces is known as context.

    • Within the context we can have directives particular to that context.
  • When we change nginx configuration we need to RELOAD it.

    • nginx -s reload
  • Validating NGINX configuration files.
    • nginx -t

Variables

events {
}

http {
    server {
        listen 80;
        server_name nginx-handbook.test;
        return 200 "Host - $host\nURI - $uri\nArgs - $args\n";
    }
}
# output
# curl http://nginx-handbook.test/user?name=Farhan

# Host - nginx-handbook.test
# URI - /user
# Args - name=Farhan

Logging

  • By default, NGINX's log files are located inside /var/log/nginx
  • Any request to the server is logged by the acces.log file.
  • We can have different access file names for different contexts.
  • Example:
    events {}
    http {
        server {
            listen 80;
            server_name nginx-handbook.test;
    
            location / {
                return 200 "this will be logged to the default file.\n";
            }
    
            location = /admin {
                access_log /var/logs/nginx/admin.log;
                return 200 "this will be logged in a separate file.\n";
            }
    
            location = /no_logging {
                access_log off;
                return 200 "this will not be logged.\n";
            }
        }
    }
    
curl http://nginx-handbook.test/no_logging
# this will not be logged

sudo cat /var/log/nginx/access.log
# empty

curl http://nginx-handbook.test/admin
# this will be logged in a separate file.

sudo cat /var/log/nginx/access.log
# empty

sudo cat /var/log/nginx/admin.log 
# 192.168.20.20 - - [25/Apr/2021:11:13:53 +0000] "GET /admin HTTP/1.1" 200 40 "-" "curl/7.68.0"

curl  http://nginx-handbook.test/
# this will be logged to the default file.

sudo cat /var/log/nginx/access.log 
# 192.168.20.20 - - [25/Apr/2021:11:15:14 +0000] "GET / HTTP/1.1" 200 41 "-" "curl/7.68.0"
  • The error.log file holds the failure logs.
    • To make an entry to the error.log, you'll have to make NGINX crash.
For some reason if you are not seeing logs use nginx -s reopen

Custom logging format

  • Custom logging format can be defined only in the http directive

    events {
      worker_connections 768;
      # multi_accept on; 
    }
    
    http {
    
        log_format json_combined escape=json
          '{' 
            '"time_local":"$time_local",'
            '"remote_addr":"$remote_addr",'
            '"remote_user":"$remote_user",'
            '"request":"$request",'
            '"status": "$status",'
            '"body_bytes_sent":"$body_bytes_sent",'
            '"http_referrer":"$http_referer",'
            '"http_user_agent":"$http_user_agent",'
            '"request_time":"$request_time",'
            '"host": "$host", '
            '"request_method": "$request_method", '
            '"http_x_forwarded_for": "$http_x_forwarded_for", '
            '"http_cf_ray": "$http_cf_ray", '
            '"server_name": "$server_name", '
            '"upstream_address": "$upstream_addr", '
            '"upstream_status": "$upstream_status", '
            '"upstream_response_time": "$upstream_response_time", '
            '"request_uri": "$request_uri"'
          '}';
    
      server {
        listen 80; 
    
        location / { 
          access_log /test.log json_combined;
          proxy_pass http://localhost:3000/;
        }   
      }
    }
    

  • NGINX Access & Error Logs Configuration: Logging Explained - Sematext

Nginx Performance

  • A rule of thumb in determining the optimal number of worker processes is number of worker process = number of CPU cores.

    • Getting the number of CPU cores in linux nproc
  • NGINX provides a better way to deal with this issue.

    • You can simply set the number of worker processes to auto and NGINX will set the number of processes based on the number of CPUs automatically. -worker_processes auto;
  • Worker connection indicates the highest number of connections a single worker process can handle.

    • This can be found using ulimit -n
      events {
          worker_connections 1024;
      }
      

Last updated: 2022-11-06