AtulHost
  • Business
    • Career and Education
    • Finance and Investments
    • Marketing Strategies
  • Productivity
  • Technology
    • Consumer Electronics
    • Solar Energy
  • Tutorials

Nginx Config (Explained by Example)

By AtulHost in Tutorials ⋅ October 14th, 2020
Facebook Tweet LinkedIn

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.

Content is tagged with nginx, web dev, web hosting, web server.
Facebook Tweet LinkedIn

You’ll also like:

  • How to fix “No Input File Specified” error?
  • How to Start, Stop, or Restart Nginx?
  • What is shared hosting?
  • How to choose budget-friendly hosting service for WordPress?
  • How to Enable HTTP/2 on NGINX?
  • What does headless mean?
  • How to enable hotlink protection in Nginx?
  • Shared vs. Virtual Private vs. Dedicated Servers
  • Query Strings Remover: WordPress Plugin
  • How to Setup 301 Redirect in Nginx?

Published by AtulHost

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

Comments and feedback

  1. Rijhu Sinha says:
    October 15th, 2020 at 11:49 AM

    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.

    Reply
    • AtulHost says:
      October 15th, 2020 at 1:23 PM

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

      Reply

Make a comment Cancel reply

Required fields are asterisked and your email address will not be shared.

Subscribe

Recent Posts

  • Transitioning your startup out of the beginning stages
  • HR trends for 2021
  • How to pick the right financing company for your customers
  • How to reduce initial server response time?
  • What every ecommerce business needs to know when it comes to product images
  • What is shared hosting?
Taking care of communication within your small business
Best laptop brands
Cool business ideas

We serve cookies on this site to offer the excellent user service.

  • About
  • Contact
  • Privacy Policy
  • Terms and Conditions
  • Write for Us
This website and its content are copyrighted © 2014-2021 by atulhost.com and individual contributors.