{"id":5721,"date":"2021-02-22T15:15:57","date_gmt":"2021-02-22T15:15:57","guid":{"rendered":"https:\/\/blog.ssdnodes.com\/blog\/?p=5721"},"modified":"2025-05-18T19:18:38","modified_gmt":"2025-05-18T19:18:38","slug":"nginx-configuration-files","status":"publish","type":"post","link":"https:\/\/www.ssdnodes.com\/blog\/nginx-configuration-files\/","title":{"rendered":"Nginx Basics &#8211; Part 0: Configuration Files"},"content":{"rendered":"<p>Nginx is one the most useful pieces of software when it comes to the web. It has quickly grown from a simple web server to a fully-featured load balancer, reverse proxy server, and a key component in technologies like Kubernetes as an ingress point. Developers, DevOps and sysadmins all come across its installations in some capacity or the other. So let's dig into what are the various nuts and bolts of a typical Nginx installation. We will focus only on the essential parts of configurations -- The Basics.<\/p>\n<\/p>\n<h2>TL;DR<\/h2>\n<p>A. For <strong><em>Debian and Ubuntu<\/em><\/strong> like systems the main configuration files can be found at:<\/p>\n<ul>\n<li><strong><em>\/etc\/nginx\/nginx.conf\u00a0<\/em><\/strong> where all the main configuration lives. You need not edit this.<\/li>\n<li>Directory <strong>\/etc\/nginx\/sites-enabled\/<\/strong> with a default file already there for reference<\/li>\n<\/ul>\n<p>Any custom configuration you wish to add can be added in the directory <strong><em>\/etc\/nginx\/sites-available<\/em><\/strong> and you can then create a symlink of that file to the directory <strong><em>\/etc\/nginx\/sites-enabled<\/em><\/strong>:<\/p>\n<pre>$ sudo ln -s \/etc\/nginx\/sites-available\/my-configuration.conf \/etc\/nginx\/sites-enabled\n$ sudo systemctl reload nginx\n<\/pre>\n<p>That's the Debian way of doing things. You can also add configurations to <strong>\/etc\/nginx\/conf.d\/<\/strong><\/p>\n<p>B.\u00a0On <strong>CentOS, Fedora<\/strong> and <strong>RedHat<\/strong> systems:<\/p>\n<ul>\n<li>All the default configuration is crammed inside <strong><em>\/etc\/nginx\/nginx.conf<\/em><\/strong><\/li>\n<li>For most use cases, the custom configuration will go to\u00a0<strong><em>\/etc\/nginx\/conf.d<\/em><\/strong> with file names ending in\u00a0<strong><em>.conf\u00a0<\/em><\/strong><\/li>\n<\/ul>\n<h2>Setup<\/h2>\n<p>To get started you will need the following:<\/p>\n<ol>\n<li>Root access to a VPS with a Public IPv4 address. If you don't already have one, you can get one from <a href=\"https:\/\/www.ssdnodes.com\/\">SSDNodes<\/a>.<\/li>\n<li>Ubuntu 20.04 LTS is used as the base operating system. Although, we will discuss the difference between Nginx on different distributions too.<\/li>\n<\/ol>\n<h2>The Default Nginx Setup<\/h2>\n<p>Let's install, enable and start Nginx:<\/p>\n<pre>$ sudo apt install nginx -y;\n$ sudo systemctl enable --now nginx;<\/pre>\n<p>By default Nginx will start a simple HTTP server, and serve this default web page:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-5684\" src=\"https:\/\/www.ssdnodes.com\/wp-content\/uploads\/2021\/02\/Nginx-1024x436.png\" alt=\"Nginx Default Webpage\" width=\"1024\" height=\"436\" srcset=\"https:\/\/www.ssdnodes.com\/wp-content\/uploads\/2021\/02\/Nginx-1024x436.png 1024w, https:\/\/www.ssdnodes.com\/wp-content\/uploads\/2021\/02\/Nginx-300x128.png 300w, https:\/\/www.ssdnodes.com\/wp-content\/uploads\/2021\/02\/Nginx-768x327.png 768w, https:\/\/www.ssdnodes.com\/wp-content\/uploads\/2021\/02\/Nginx-1536x654.png 1536w, https:\/\/www.ssdnodes.com\/wp-content\/uploads\/2021\/02\/Nginx-2048x872.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<p>You can view the webpage, by simply typing in the Public IP address of your VPS\u00a0 in a web browser. The actual webpage might be different for RedHat like distros and Ubuntu, but ultimately it will be a simple html file that Nginx is configured to serve. Which brings us to the next part -- Configuration.<\/p>\n<h2>Configuration Files<\/h2>\n<p>All the configuration for Nginx lives in the directory\u00a0<strong><em>\/etc\/nginx<\/em><\/strong> with the\u00a0<strong>\/etc\/nginx\/nginx.conf\u00a0<\/strong>file at the heart. This is where things get interesting. For the purposes of this blog post, we will ignore various miscellaneous file like in <strong><em>\/etc\/nginx<\/em><\/strong>\u00a0<em>koi-win, koi-utf, mime.types proxy_params\u00a0<\/em>and the rest. For this post, the basics, the files that matter us are:<\/p>\n<ol>\n<li><strong><em>nginx.conf:\u00a0<\/em><\/strong>The main configuration file of Nginx server<\/li>\n<li><strong><em>conf.d<\/em><\/strong>: The directory where user specific configurations are stored.<\/li>\n<li><strong><em>site-available<\/em><\/strong> and <strong><em>sites-enabled<\/em><\/strong>: The directory where user specific configurations are stored for Ubuntu and Debian like systems.<\/li>\n<\/ol>\n<h2>Default nginx.conf<\/h2>\n<p>Removing all the comments, for Ubuntu 20.04 the default configuration comes with the following:<\/p>\n<p><strong>nginx.conf<\/strong><\/p>\n<pre>user www-data;\nworker_processes auto;\npid \/run\/nginx.pid;\ninclude \/etc\/nginx\/modules-enabled\/*.conf;\n\nevents {\n    worker_connections 768;\n}\n\nhttp {\n\n    sendfile on;\n    tcp_nopush on;\n    tcp_nodelay on;\n    keepalive_timeout 65;\n    types_hash_max_size 2048;\n\n    include \/etc\/nginx\/mime.types;\n    default_type application\/octet-stream;\n\n    ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;\n    ssl_prefer_server_ciphers on;\n\n    access_log \/var\/log\/nginx\/access.log;\n    error_log \/var\/log\/nginx\/error.log;\n    gzip on;\n\n    include \/etc\/nginx\/conf.d\/*.conf;\n    include \/etc\/nginx\/sites-enabled\/*;\n}\n<\/pre>\n<p><strong>sites-enabled\/default<\/strong><\/p>\n<pre>server {\n    listen 80 default_server;\n    listen [::]:80 default_server;\n\n    root \/var\/www\/html;\n    index index.html index.htm index.nginx-debian.html;\n    server_name _;\n\n    location \/ {\n        try_files $uri $uri\/ =404;\n    }\n}\n<\/pre>\n<p>This is all there is to the man configuration. Each single line ending with a semi-colon is called a directive, in a bit more formal terms:<\/p>\n<ol>\n<li>A <strong>Simple Directive <\/strong>consists of the name of that directive name followed by a list of parameters, seperated by spaces, and ends with a semicolon (;).<br \/>\nFor example, the line:<\/p>\n<pre>user www-data;<\/pre>\n<p>Signifies that the Nginx worker process will start as the Unix user\u00a0<em>www-data<\/em> and you can modify it to run as any other user that you want it to work as.<br \/>\nSimilarly, other directives like ssl_protocols is followed by a list of TLS protocols that Nginx supports. If you want to remove older versions like TLSv1, simply remove them from the list of parameters and reload nginx by running the command\u00a0<em>systemctl reload nginx<\/em> and TLSv1 will no longer be supported. However, I don't recommend fiddling with any of the defaults, unless you really know what you are doing. If you wish to go beyond the basics, the <a href=\"https:\/\/nginx.org\/en\/docs\/\" target=\"_blank\" rel=\"noopener\">official documentation<\/a> is always a great place to go.<\/li>\n<li>A <strong>Block Directive<br \/>\n<\/strong>A block directive is made up of the directive name, followed by a pair of curly braces (<em>{}<\/em>) within which there are multiple single directives. The\u00a0<em>http <\/em>block is a really important example for this one, and it is something you will be dealing with a lot. When you are within a block, that block is also called\u00a0<em>context.\u00a0<\/em>So, for example, the lines:<\/p>\n<pre>include \/etc\/nginx\/sites-enabled\/*;\ninclude \/etc\/nginx\/conf.d\/*.conf;\n<\/pre>\n<p>Are the directives within the <em>http<\/em> context. All the user specific configuration that lives in <em>\/etc\/sites-enabled\u00a0<\/em>and <em>\/etc\/conf.d\u00a0<\/em>are brought into the http context, because of the above two lines in <em>nginx.conf<\/em>.<\/li>\n<\/ol>\n<h2>Difference between RedHat-like systems and Debian like systems:<\/h2>\n<h3>Debian and Ubuntu<\/h3>\n<p>Nginx configuration files are quite modular to begin with. You won't find yourself in need of changing the main configuration file unless you are really fine-tuning something. On Debian and Ubuntu, like systems, this modularity is taken a bit further. All your configuration files live like simple text files in the folder <strong><em>\/etc\/nginx\/sites-available\/\u00a0<\/em><\/strong>and if you want to <strong><em>&quot;enable&quot; <\/em><\/strong>a certain configuration you will do so by creating a symlink (a shortcut) of that file to the folder <strong><em>\/etc\/nginx\/sites-enabled:<\/em><\/strong><\/p>\n<pre>$ sudo ln -s \/etc\/nginx\/sites-available\/my-config \/etc\/nginx\/sites-enabled\/my-config<\/pre>\n<p>This lets users experiment freely with different configurations. If you need to remove a particular configuration, just remove the symlink from the <em>sites-enabled <\/em>directory and reload nginx. In fact, even the default file that was shown in the previous section is a symlink and the actual file lives in the <em>sites-available.<br \/>\n<\/em><br \/>\nThere is also <em>conf.d\u00a0<\/em>file that allows you to add configurations in a way that works across all distributions. The only difference is that you have to name your files with a <em>.conf\u00a0<\/em>extension as specified in the <em>nginx.conf<\/em><\/p>\n<h3>CentOS, RedHat and Fedora systems<\/h3>\n<p>In case of CentOS, RHEL and other similar systems, Nginx gets its user specific configuration mostly from the <em>conf.d\u00a0<\/em>subdirectory as decribed by this line in the file <em>nginx.conf<\/em> in the <em>http <\/em>context:<\/p>\n<pre>include \/etc\/nginx\/conf.d\/*.conf;<\/pre>\n<h2>Configuring a simple static site server<\/h2>\n<p>To finish off our tour of Nginx configuration, let's write a configuration that will serve a static website from the directory <em>\/var\/www\/my-web.<\/em><\/p>\n<p>Let's create a simple website page:<\/p>\n<pre>$ sudo mkdir -p \/var\/www\/my-web\n$ cd \/var\/www\/my-web\n$ vim index.html<\/pre>\n<p>Put the follow contents in the index.html:<\/p>\n<pre>&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n&lt;head&gt;\n    &lt;title&gt;Page Title&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n    &lt;h1&gt;This is a Heading&lt;\/h1&gt;\n    &lt;p&gt;This is a paragraph.&lt;\/p&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/pre>\n<p>This isn't much, but it will serve well as an example. You can, of course create a complete static site using software like <a href=\"https:\/\/gohugo.io\/\" target=\"_blank\" rel=\"noopener\">Hugo<\/a>.<\/p>\n<p>Next we can add a configuration, instead of going through site-available folders and then creating symlinks, I will just write the server definition in <em>conf.d <\/em>folder. It will work on all platforms and it is simpler to manage:<\/p>\n<pre>$ vim \/etc\/nginx\/conf.d\/my-web.conf\nserver {\n    listen 80 default_server;\n    listen [::]:80 default_server;\n\n    root \/var\/www\/my-web;\n    index index.html index.htm index.nginx-debian.html;\n    server_name _;\n\n    location \/ {\n        try_files $uri $uri\/ =404;\n    }\n}\n<\/pre>\n<p>Once the configuration is in place, let's remove the old configuration and reload:<\/p>\n<pre>$ sudo rm \/etc\/sites-enabled\/default\n$ sudo systemctl reload nginx<\/pre>\n<p>If you wish to bring back the original configuration, simply remove <em>my-web.conf<\/em> file and recreate the symlink for the default config.<\/p>\n<h2>Conclusion<\/h2>\n<p>If you wish to learn more about Nginx, check out <a href=\"https:\/\/www.ssdnodes.com\/blog\/hosting-multiple-sites-on-a-single-vps-using-docker-nginx-and-certbot\/\">this post<\/a> detailing how to use it as a reverse proxy endpoint to serve multiple websites. More information about various Nginx directives can be found <a href=\"https:\/\/nginx.org\/en\/docs\/\" target=\"_blank\" rel=\"noopener\">here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Nginx is one the most useful pieces of software when it comes to the web. It has quickly grown from a simple web server to a fully-featured load balancer, reverse proxy server, and a key component in technologies like Kubernetes as an ingress point. Developers, DevOps and sysadmins all come across its installations in some  &#8230;<\/p>\n","protected":false},"author":20,"featured_media":5736,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"footnotes":""},"categories":[18,30],"tags":[211],"class_list":["post-5721","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops","category-tutorials","tag-nginx"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.ssdnodes.com\/wp-json\/wp\/v2\/posts\/5721","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ssdnodes.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ssdnodes.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ssdnodes.com\/wp-json\/wp\/v2\/users\/20"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ssdnodes.com\/wp-json\/wp\/v2\/comments?post=5721"}],"version-history":[{"count":3,"href":"https:\/\/www.ssdnodes.com\/wp-json\/wp\/v2\/posts\/5721\/revisions"}],"predecessor-version":[{"id":13049,"href":"https:\/\/www.ssdnodes.com\/wp-json\/wp\/v2\/posts\/5721\/revisions\/13049"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.ssdnodes.com\/wp-json\/wp\/v2\/media\/5736"}],"wp:attachment":[{"href":"https:\/\/www.ssdnodes.com\/wp-json\/wp\/v2\/media?parent=5721"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ssdnodes.com\/wp-json\/wp\/v2\/categories?post=5721"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ssdnodes.com\/wp-json\/wp\/v2\/tags?post=5721"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}