Default NGINX Configuration

Published in

on

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 take 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, 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 CPU, 4 for Quad-core CPU. If you know how many CPU cores the server have, better add that as a number.

worker_rlimit_nofile 100000;
# Number of file descriptors used for nginx. This line is not available in default installation and need to add manually. The limit for the maximum FDs on the server is usually set by the OS itself. If you don't set file descriptors 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 Core/s.

    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 --used 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;
    # Default 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. 4 to 6 is the best compression anything over 6 wasted CPU efforts. Keep it low (like: 1 to 3) if hardware is low-end.
    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 a 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 the way it will be accessed.
    # _, localhost, IP address, or domain name.

    root /usr/share/nginx/html;
    # Locate the root location of web files.
    # Some installation have this location - /var/www/html.

    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.

Leave a Reply

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

Responses to “Default NGINX Configuration”

  1. Rijhu Sinha Avatar
    Rijhu Sinha

    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.

    1. Atul Kumar Pandey Avatar

      Thanks for the feedback. Stay tuned for more upcoming resources.