Nginx Config (Explained by Example)

Nginx configuration file located at /etc/nginx/nginx.conf has everything that makes a server flies or sinks. Below we have explained what each directive does. Let’s have a look and fine-tune your settings:

NGINX

Before we hit to configurations directly understand the structure first.

  1. HTTP Block: Global configurations.
  2. Server Block: Domain level configurations. Add inside the HTTP block.
  3. Location Block: Directly level configurations. Add inside the Server block.

There are a few more blocks/contexts like mail, map, upstream, and etc. We’ll learn them in some other tutorial as they are used under uncommon conditions.

I assume that you know the file structure of Nginx. Server block is explained in the config file instead of /etc/nginx/conf.d/*.conf.

/etc/nginx/nginx.conf

user www-data;
# Default is nobody, PHP-FPM uses www-data.

worker_processes auto;
# Default is auto, but you can use numbers too like 2 for dual core.

worker_rlimit_nofile 100000;
# Number of file descriptors used for nginx, the limit for the maximum FDs on the server is usually set by the OS. If you don't set FD's then OS settings will be used which is by default 2000.

error_log logs/error.log;
# Log the errors.

pid logs/nginx.pid;
# Process Identification Number (PID) for Nginx.

events {
    worker_connections 2048;
    #Default is 1024, or 1024 x CPU Cores.

    use epoll;
    # Don't use it.
    # Optimized to serve many clients with each thread, essential for linux --for test environment.

    multi_accept on;
    # Don't use it.
    # Accept as many connections as possible, may flood worker connections if set too low --for testing environment.
}

http {
    include /etc/nginx/mime.types;
    # Add all known mime (file extension) types in Nginx.

    default_type application/octet-stream;
    # When the type is unknown, the "octet-stream" subtype is used to indicate that a body contains arbitrary binary data.

    access_log off;
    # Dafult is on, useful in test environment, but disable it to boost I/O on storage.

    sendfile on;
    # Default is on, it copies data between one FD and other from within the kernel which is faster than read() + write().

    tcp_nopush on;
    # Send headers in one piece, it is better than sending them one by one.

    tcp_nodelay on;
    # Don't buffer data sent, good for small data bursts in real time.

    keepalive_timeout 65;
    # Keep the connection open for default 65 seconds, you can increase or decrease according to your need.

    gzip on;
    # Reduce the data that needs to be sent over network. It needs further customization see below.
    gzip_min_length 10240;
    gzip_comp_level 1;
    # Set compression level 1 to 9, more levels = more CPU overhead.
    gzip_vary on;
    gzip_types
        # text/html is always compressed by HttpGzipModule
        text/css application/javascript;

    include /etc/nginx/conf.d/*.conf;
    # Load the server configuration file.
}

If you read the last directive of the above configuration file, the config includes a new *.conf file. Here the setup includes all the config files of the conf.d directory.

Here asterisk symbol (*) means include all the files in given directory.

Note: In some old version of the Nginx we can see this server config file under /etc/nginx/sites-available/default so do not get confused here.

/etc/nginx/conf.d/default.conf

server {
    listen 80;
    # Listen to the Port 80.
    listen [::]:80;
    # Listen to the Port 80 under IPv6 address.

    server_name localhost;
    # Name the server block: _, localhost, IP address, or domain name.

    root /usr/share/nginx/html;
    # Locate the root location of web files.

    index index.php index.html index.htm;
    # Set the default index file format. Many file formats can be assigned as mentioned above.

    location / { try_files $uri $uri/ /index.php }
    # Location block (example) for URLs.

    location ~* .(css/js)$ { access_log off; expires max; }
    # Location block (example) for static files.
}

There are a few more directives that I have not added to this article because they are self-explanatory or doesn’t need any description. Even if you think or want to know anything which is not covered ask it in the comments.

Published by AtulHost

Atul Kumar Pandey is a creative blogger who loves experiments with the latest tech trends like automation, artificial intelligence, data science, cloud computing, edge computing, hardware as well as networking, and the internet of things.

2 comments on Nginx Config (Explained by Example)

  1. Hello Atul Sir,

    Rijhu this side.

    This is really very very informative post. Thanks for your detailed research and sharing this with us.

    After reading this post I really learned a lot and very sure this article is definitely going to help many others too. A complete info on Nginx Config.

    I really appreciate the way you have explained making the concept very clear even for newbies too. Thanks and do keep sharing more similar and informative post.

    Regards.

Join the discussion!

Your email address will not be published. Required fields are marked *