Nginx sites-available & sites-enabled
Multiple virtual hosts with Nginx
Sites-available and sites-enabled permits you easily work with multiple sites and hosts on your local machine.
After the installation of Nginx I did not have sites-available and sites-enabled folders.
So I needed to create them manually
And added to the nginx.conf file
nano /usr/local/etc/nginx/nginx.conf
vim /usr/local/etc/nginx/nginx.conf
subl /usr/local/etc/nginx/nginx.conf
We need to add the 11th
include /usr/local/etc/nginx/sites-enabled/*.*;
Don’t forget to change the path to yours.
Sites-available
Then added a default.conf in sites-available
server {
listen 80;
server_name localhost;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
and now we’ll add our first site test.loc site configuration
server {
listen 80;
server_name test.loc;
#access_log logs/host.access.log main;
location / {
root /tmp;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
It will load an html from /tmp/test/index.html.
Let’s create it
cd /tmp && touch index.html
nano index.html
Our sites-available is ready
Sites-enabled
Now it’s time to do a link between two folders.
ln -s /usr/local/etc/nginx/sites-available/default.conf /usr/local/etc/nginx/sites-enabledln -s /usr/local/etc/nginx/sites-available/test.loc /usr/local/etc/nginx/sites-enabled
You should see two links
Reload nginx
Reload your nginx to take into the count changes
sudo nginx -s stop && sudo nginx
or simply run nginx if it did not work
sudo nginx
Change our hosts /etc/hosts
sudo nano /etc/hosts
Don’t forget to add a new virtual host
Test it
Now you should see your site http://test.loc/
What next?
I regularly add other posts. If you want to be notified you can also subscribe here.
Thank you for your attention
Thank you for your attention.
Do you need help with web and mobile development? Feel free to contact me here.
P.S. I really appreciate your like or share 🙏.