So let’s dive into configuring our Nginx
I assume that you already have a familiarity with Nginx. You should also have it installed on your machine.
Let’s dive in.
All of the configuration files are in this directory : /etc/nginx
Inside that directory, three directories and one file are important for use:
/sites-available /sites-enabled /conf.d nginx.conf
nginx.conf contains the general configuration of Nginx
/conf.d is a directory where you can define your specific configurations for Nginx
/sites-available is a directory where you define your configurations for a specific website (you may have three websites, like one is your blog, the other is your personal website, and the other is for your business. In that case, you need to define a configura…
So let’s dive into configuring our Nginx
I assume that you already have a familiarity with Nginx. You should also have it installed on your machine.
Let’s dive in.
All of the configuration files are in this directory : /etc/nginx
Inside that directory, three directories and one file are important for use:
/sites-available /sites-enabled /conf.d nginx.conf
nginx.conf contains the general configuration of Nginx
/conf.d is a directory where you can define your specific configurations for Nginx
/sites-available is a directory where you define your configurations for a specific website (you may have three websites, like one is your blog, the other is your personal website, and the other is for your business. In that case, you need to define a configuration file for each website, and you would put them in this directory.)
/sites-enabled is a directory that contains your linked configuration files from /sites-available directory. Suppose you only want your personal website to be active and served by Nginx ,then you only need to link your personal website in sites-available directory this the sites-enabled directory.
There are 2 ways to set up your Nginx:
First way:
Actually, you don’t need to know about those directories. You would open the nginx.conf directly, and at the end of the http block, you would add your own configurations like so:
http {
##
# Basic Settings
##
server{
# your configurations
}
}
And yeah, that would work, but that is really messy.
Better way:
If you take a close look at the nginx.conf file in http block, you will notice two lines that can help us create our own configurations in a better way.
Let’s check those lines:
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
You see! They are exactly those files that we talked about. Nginx has already included them using the include keyword and the address to those directories. It means if you have already written some configuration files in those directories, Nginx will add them directly to this general config file.
That * in the first address says bring me all files which have .conf extension and in the second address it says bring me all files in the sites-enabled directory and include them in this file.
Alright, so far so good. Now that we have understood the concept and directory structure, let’s begin to create our own configuration file for our website.
But before that, you may ask, "Ok, but what about sites-available?" We will get to that.
If you haven’t touched the sites-enabled since the installation, you will be able to see the default file, BUT if you run this command ls -l /etc/nginx/sites-enabled you will be able to see this part in your output:
... -> /etc/nginx/sites-available/default
That means this default file is actually linked to a file in sites-available directory. the original default is placed in sites-available and if you change that file, the default file in sites-enabled will change accordingly. why do we use this structure?
because that is very neat and clean, and if we want to disable a website, we simply just unlink the file from sites-enable and if we need to activate it again, we can link it again. How can we do that? Let me show you:
First, we need to create our config file in sites-available . You can use nano or vim or whatever you prefer:
sudo nano /etc/nginx/sites-available/<arbitrary_nam>
After you put your config in that file, you need to link that to sites-enabled using this command:
sudo ln -s /etc/nginx/sites-available/<name_of_your_config> /etc/nginx/sites-enabled/
Great job!
Now reload your Nginx, and you are ready to go:
sudo nginx -s reload