When using the Nginx web server, server blocks (similar to virtual hosts in Apache) can be used to encapsulate configuration details and host more than one domain on a single server.
In this guide, we’ll discuss how to configure server blocks in Nginx on an Ubuntu 16.04 server.
Deploy your applications from GitHub using DigitalOcean App Platform. Let DigitalOcean focus on scaling your app.
We’re going to be using a non-root user with sudo
privileges throughout this tutorial. If you do not have a user like this configured, you can create one by following our Ubuntu 16.04 initial server setup guide.
You will also need to have Nginx installed on your server. The following guides cover this procedure:
When you have fulfilled these requirements, you can continue on with this guide.
For demonstration purposes, we’re going to set up two domains with our Nginx server. The domain names we’ll use in this guide are example.com and test.com.
Note: for more information on setting up a domain with DigitalOcean, please see our Domains and DNS product documentation.
If you do not have two spare domain names to play with, use placeholder names for now and we’ll show you later how to configure your local computer to test your configuration.
By default, Nginx on Ubuntu 16.04 has one server block enabled. It is configured to serve documents out of a directory at /var/www/html
.
While this works well for a single site, we need additional directories if we’re going to serve multiple sites. We can consider the /var/www/html
directory the default directory that will be served if the client request doesn’t match any of our other sites.
We will create a directory structure within /var/www
for each of our sites. The actual web content will be placed in an html
directory within these site-specific directories. This gives us some additional flexibility to create other directories associated with our sites as siblings to the html
directory if necessary.
We need to create these directories for each of our sites. The -p
flag tells mkdir
to create any necessary parent directories along the way:
Now that we have our directories, we will reassign ownership of the web directories to our normal user account. This will let us write to them without sudo
.
Note: Depending on your needs, you might need to adjust the permissions or ownership of the folders again to allow certain access to the www-data
user. For instance, dynamic sites will often need this. The specific permissions and ownership requirements entirely depend on your configuration. Follow the recommendations for the specific technology you’re using.
We can use the $USER
environmental variable to assign ownership to the account that we are currently signed in on (make sure you’re not logged in as root). This will allow us to easily create or edit the content in this directory:
The permissions of our web roots should be correct already if you have not modified your umask
value, but we can make sure by typing:
Our directory structure is now configured and we can move on.
Now that we have our directory structure set up, let’s create a default page for each of our sites so that we will have something to display.
Create an index.html
file in your first domain:
Inside the file, we’ll create a really basic file that indicates what site we are currently accessing. It will look like this:
<html>
<head>
<title>Welcome to Example.com!</title>
</head>
<body>
<h1>Success! The example.com server block is working!</h1>
</body>
</html>
Save and close the file when you are finished. To do this in nano
, press CTRL+o
to write the file out, then CTRL+x
to exit.
Since the file for our second site is basically going to be the same, we can copy it over to our second document root like this:
Now, we can open the new file in our editor:
Modify it so that it refers to our second domain:
<html>
<head>
<title>Welcome to Test.com!</title>
</head>
<body>
<h1>Success! The test.com server block is working!</h1>
</body>
</html>
Save and close this file when you are finished. We now have some pages to display to visitors of our two domains.
Now that we have the content we wish to serve, we need to create the server blocks that will tell Nginx how to do this.
By default, Nginx contains one server block called default
which we can use as a template for our own configurations. We will begin by designing our first domain’s server block, which we will then copy over for our second domain and make the necessary modifications.
As mentioned above, we will create our first server block config file by copying over the default file:
Now, open the new file you created in your text editor with sudo
privileges:
Ignoring the commented lines, the file will look similar to this:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files $uri $uri/ =404;
}
}
First, we need to look at the listen directives. Only one of our server blocks on the server can have the default_server
option enabled. This specifies which block should serve a request if the server_name
requested does not match any of the available server blocks. This shouldn’t happen very frequently in real world scenarios since visitors will be accessing your site through your domain name.
You can choose to designate one of your sites as the “default” by including the default_server
option in the listen
directive, or you can leave the default server block enabled, which will serve the content of the /var/www/html
directory if the requested host cannot be found.
In this guide, we’ll leave the default server block in place to serve non-matching requests, so we’ll remove the default_server
from this and the next server block. You can choose to add the option to whichever of your server blocks makes sense to you.
server {
listen 80;
listen [::]:80;
. . .
}
Note: You can check that the default_server
option is only enabled in a single active file by typing:
If matches are found uncommented in more than on file (shown in the leftmost column), Nginx will complain about an invalid configuration.
The next thing we’re going to have to adjust is the document root, specified by the root
directive. Point it to the site’s document root that you created:
server {
listen 80;
listen [::]:80;
root /var/www/example.com/html;
}
Next, we need to modify the server_name
to match requests for our first domain. We can additionally add any aliases that we want to match. We will add a www.example.com
alias to demonstrate.
When you are finished, your file will look something like this:
server {
listen 80;
listen [::]:80;
root /var/www/example.com/html;
index index.html index.htm index.nginx-debian.html;
server_name example.com www.example.com;
location / {
try_files $uri $uri/ =404;
}
}
That is all we need for a basic configuration. Save and close the file to exit.
Now that we have our initial server block configuration, we can use that as a basis for our second file. Copy it over to create a new file:
Open the new file with sudo
privileges in your editor:
Again, make sure that you do not use the default_server
option for the listen
directive in this file if you’ve already used it elsewhere. Adjust the root
directive to point to your second domain’s document root and adjust the server_name
to match your second site’s domain name (make sure to include any aliases).
When you are finished, your file will likely look something like this:
server {
listen 80;
listen [::]:80;
root /var/www/test.com/html;
index index.html index.htm index.nginx-debian.html;
server_name test.com www.test.com;
location / {
try_files $uri $uri/ =404;
}
}
When you are finished, save and close the file.
Now that we have our server block files, we need to enable them. We can do this by creating symbolic links from these files to the sites-enabled
directory, which Nginx reads from during startup.
We can create these links by typing:
These files are now linked into the enabled directory. We now have three server blocks enabled, which are configured to respond based on their listen
directive and the server_name
(you can read more about how Nginx processes these directives here):
example.com
: Will respond to requests for example.com
and www.example.com
test.com
: Will respond to requests for test.com
and www.test.com
default
: Will respond to any requests on port 80 that do not match the other two blocks.In order to avoid a possible hash bucket memory problem that can arise from adding additional server names, we will also adjust a single value within our /etc/nginx/nginx.conf
file. Open the file now:
Within the file, find the server_names_hash_bucket_size
directive. Remove the #
symbol to uncomment the line:
http {
. . .
server_names_hash_bucket_size 64;
. . .
}
Save and close the file when you are finished.
Next, test to make sure that there are no syntax errors in any of your Nginx files:
If no problems were found, restart Nginx to enable your changes:
Nginx should now be serving both of your domain names.
If you have not been using domain names that you own and instead have been using placeholder values, you can modify your local computer’s configuration to let you to temporarily test your Nginx server block configuration.
This will not allow other visitors to view your site correctly, but it will give you the ability to reach each site independently and test your configuration. This works by intercepting requests that would usually go to DNS to resolve domain names. Instead, we can set the IP addresses we want our local computer to go to when we request the domain names.
Note: Make sure you are operating on your local computer during these steps and not a remote server. You will need to have root access, be a member of the administrative group, or otherwise be able to edit system files to do this.
If you are on a Mac or Linux computer at home, you can edit the file needed by typing:
If you are on Windows, you can find instructions for altering your hosts file here.
You need to know your server’s public IP address and the domains you want to route to the server. Assuming that my server’s public IP address is 203.0.113.5
, the lines I would add to my file would look something like this:
127.0.0.1 localhost
. . .
203.0.113.5 example.com www.example.com
203.0.113.5 test.com www.test.com
This will intercept any requests for example.com
and test.com
and send them to your server, which is what we want if we don’t actually own the domains that we are using.
Save and close the file when you are finished.
Now that you are all set up, you should test that your server blocks are functioning correctly. You can do that by visiting the domains in your web browser:
http://example.com
You should see a page that looks like this:
If you visit your second domain name, you should see a slightly different site:
http://test.com
If both of these sites work, you have successfully configured two independent server blocks with Nginx.
At this point, if you adjusted your hosts
file on your local computer in order to test, you’ll probably want to remove the lines you added.
If you need domain name access to your server for a public-facing site, you will probably want to purchase a domain name for each of your sites.
You should now have the ability to create server blocks for each domain you wish to host from the same server. There aren’t any real limits on the number of server blocks you can create, so long as your hardware can handle the traffic.
Want to easily configure a performant, secure, and stable Nginx server? Let us spin up an Nginx virtual machine for you in seconds, so you can focus on building a great application.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Hello;
I have a user “dev” and the site is installed in dev directory. whenever i upload a .zip file like wp theme and extract it permissions check to “root” instead of “dev” who is added to the sudo group.
any idea how to do this? i don’t want to be changing permissions whenever i upload themes via command like.
On serverpilot when if you upload the file with root, so long as you extract it with serverpilot the file permission goes to serverpilot user.
Thanks
thank you so much!!
Thanks … very well written… I noticed a typo… to check that the default_server option is only enabled in a single active file by typing: grep -R default_server /etc/nginx/sites-enabled/
should be grep -R default_server /etc/nginx/sites-available/
sites-enabled is symlink for enabled site configs so it is ok
This how guides should be made.Thank you.
This comment has been deleted
Hello. What´s the difference between
listen 80;
andlisten [::]:80;
Thanks in advanced!
@luismuzquiz: Hello! The first line applies to connections made using IPv4, while the second line is used for IPv6 connections. Both are needed if you desire connectivity using both protocols. Hope that helps!
thanks!
Hi I have followed all the steps but site is not running. when I access domain yournexthosting.com in browser it download a file automatically. when I access yournexthosting.com/index.html it downloads index.html page… plz help.
Yea I’m having a similar issue. I just get the Nginx welcome screen.
What are some specifics? I might be able to help debug this.
Thanks for this, it’s super helpful. I wanted to host a Django server and node server on one Ubuntu instance, and this got me started in the right direction. I wrote a little bit about setting up multiple server blocks
I am using LEMP on Ubutu 16.04 with a virtual host(server block). When I log into phpmyadmin, I get a 404 page. But when I go back to /phpmyadmin I am logged in. So it is logging me in, but redirecting to a 404. The url (after logging in) is index.php(Plus a ton of other characters). Any idea what is going on?
Its totally shocking. In every article since 2012, you always skip the part where you are supposed to create the virtual host and instead copy the default.
can anyone help at the end I get an error 404 not found? it happens to any site even if I use http://ipadd/test.php same error
@jellingwood Thanks in advance for the tutorial. I’m experiment some problems, I follow all the steps and this is my server configuration:
Even though when I go to:
I got “HTTP ERROR 500” page It’s not working.
Plase help. Thanks.
this tutorial work great. But www.example.com not working. Only example.com work. I already added CNAME for www could you help me please?
@bambangyudhotomo It can take awhile for DNS to propogate (up to 48 hours in some cases) so if it was a recent change, I would wait a bit longer. The other component is to make sure that your Nginx server blocks are set up to respond to
www.example.com
requests as well.In your Nginx server blocks, make sure that your
server_name
points to both the bare domain as well as the “www” domain. So in this case, make sure your server block is set up like this:If you forget to do this, Nginx won’t know which block to use for
www.example.com
requests. Don’t forget to restart Nginx to pick up your changes. Hope that helps.@jellingwood thank you so much. It worked! I forgot that it need up to 48 hours for DNS to propogate.
i try to use domain demo www.demo.demo, it redirect me to router page. the 192.168.0.1 Page
am i using wrong server’s public IP address? and i used
curl ifconfig.me
to get my server public IP address.anyone encounter this issue?
Can I place server block from /var/www/example.com/html to another directory like /home/someuser/example.com/html ?
Thank you for making a beautiful & clean tutorial. I would like to know, How can I create subdomain? Again, thanks!
Hello,
Unfortunately I am stuck at step three because I can’t find the /etc/nginx/sites-available/ and site-enable directories / config files in my clean nginx install. The server is running and shows the ‘welcome to nginx’ message in my browser. That html file is located in /usr/share/nginx/html. But I cant figure out whats going wrong while missing the default server block config in /etc/nginx/… Many thanks in advance for your help!!
@michiel0 If you do not have an
/etc/nginx/sites-available
or/etc/nginx/sites-enabled
directory, it’s likely a sign that you have installed Nginx from a location other than Ubuntu’s default repositories (like the Nginx project’s repositories) or that you are trying to complete this guide on a different version of Ubuntu. This guide assumes that you are using the Nginx package available in Ubuntu 16.04’s default repositories. If your setup looks different, it’s very possible this won’t work.Hi!
I have followed this tutorial and I have setup nginx and added two blocks for my two domains and created symlinks, but only one domain is working. First domain throws Server not found error in Firefox, and second loads perfectly.
I tried removing symlink for second domain and what happend is that the second domain is now loading first domains page, and first domain is still not working.
First server block looks like this:
server { listen 80; listen [::]:80;
}
and second server block looks like this:
server { listen 80; listen [::]:80;
}
Nginx error log is empty, firewall is set to allow Nginx Full, both domains point to ns1.digitalocean.com - ns3.digitalocean.com
I can’t seem to find a problem
Can anyone help me troubleshoot this?
Hi, are you still having this issue? You will find in Nginx’s documentation in reference to server names that:
" They may be defined using exact names, wildcard names, or regular expressions:
server { listen 80; server_name example.org www.example.org; … } "
I am using ssl through LetsEncrypt on my website. I tried this tutorial to add another website, and when i go to the new domain it redirects me to my first domain that uses ssl. Any ideas?
@n8techy Hey there. Nginx uses a combination of the
listen
directive (which dictates which port the server block is configured to respond to), thedefault_server
option for thelisten
directive (which defines one server block per port to use as a fallback), and theserver_name
directive (which designates the IP addresses or domain names that the server block is supposed to respond to). You can find a more detailed explanation here.If you are having this issue, the first thing I’d do is make sure each of your server blocks uses the
server_name
directive to define exactly which domain it is supposed to serve requests for. While evaluating each of your server blocks, consider removing anydefault_server
declarations from thelisten
directives you may find. If more than one server block uses the sameserver_name
value andlisten
port, you will run into problems.I had my default server set in my first site. I have removed it and restarted nginx, but its still redirecting to the first domain. Here are my server blocks
@n8techy Hey there. There seem to be a few problems here, but I’m not sure if they’re transcription errors. The second block (the one dealing with SSL for the first site) has a
default_server
in the middle of its port specification. You’ll need to fix that to get this to run. If you’re removing thedefault_server
option, you should also make sure you set theserver_name
like you did with the regular port 80 block. It should look like this:Make sure you include the closing brace at the end there. It isn’t present in the comment you posted.
Next, in your third server block (the port 80 block for the new site), you have a 301 redirect that’s pointing back to itself. It’s redirecting to
http://$server_name$request_uri
, which is the exact same block. I’m not sure if you set it up that way to help yourself debug, but that configuration will create a redirect loop (it’ll try to look up the redirect, but it will always match the same block). You should either remove theredirect
line, or uncomment the SSL block.Either of those errors should have caused Nginx to refuse to restart. Are you restarting the the Nginx process in between tests?
First, check the syntax by typing:
If that complains about anything, go back to your configuration file to try to find the problem. Once that test passes, you can restart Nginx by typing:
Beyond that, it’s a bit difficult to debug since it appears you have some syntax errors in the comment at least, if not on your server itself. Hopefully this gets you started in the right direction though.
I have been restarting and i can get this working on another vm only difference is i’m not using ssl. Also I have cleaned up the code, and restarted nginx with no issues, but it still redirects to my primary site. Let me know what you think.
@n8techy If you’re still having problems, it may be that at some point during this process, you set up a 301 redirect from your new domain to your old domain. 301 redirects are cached for a very long time by most browsers because the specification says that they indicate a permanent move. Try accessing the server from a different browser or, better yet, a different computer to see if you are able to access your new domain. You will have to look up the instructions for how to clear 301 redirects in your original browser if you are able to access the new domain from a different client.
A 301 redirect usually causes browsers to not even make the original request the second time the redirect is processed. This means that if in the past the browser came across instructions to permanently redirect your new domain to your old domain, any requests for the new domain will be automatically translated to the old domain in the browser before reaching out to the server. This means that your log files might not indicate the redirect activity on subsequent requests. However, it’s worth checking into anyways. You can find the Nginx logs at
/var/log/nginx/access.log
and/var/log/nginx/error.log
to see if you can find any additional information to help you troubleshoot.Fantastic guide—thanks so much!!
Following your guide I was almost losing my mind because it didn’t work. In fact, if I remove just the default config file, I’m unable to connect to any site. Only default config works, and if I delete it, the other two configs of the other two sites didn’t work. But after several days of attempts, unistalling and reinstalling nginx, resetting server etc etc, I found out where the problem was. In you guide you wrote to create a symbolic link using the command “ln -s” but it will never work thi s way. Buy if you use just “ln” without “-s” option, it works.
@allexj The
ln
command without the-s
flag does not create symbolic links, it creates hard links, which is very different. Hard links can work for this procedure, but they are generally not recommended. If you are having trouble getting your symbolic links to resolve correctly, make sure that you are using an absolute path instead of a relative path when creating them.On a fresh install, you can see that Nginx’s
default
server block is created using symbolic links as well:The first character indicates that the file is a symbolic link. You can see that this file is linked to the file in
/etc/nginx/sites-available
in the last column of the output. However, when you create hard links (by usingln
without the-s
), the file will appear as a normal file with an extra link count in the second column:If you’re having trouble getting the symbolic linking to work, it’s likely that your link is broken. If it helps, you can move into the
/etc/nginx/sites-enabled
directory and create the links from there by typing:I hope that helps.
I didn’t lose my mind like allexj, but it did take me 45 minutes to figure out why the boilerplate nginx server page was showing instead of the server blocks. The issue was the linking. I used relative paths instead of absolute paths. Hope this helps someone in the future!
@jellingwood I got the link problem also , your solution helps me. Thanks a lot.
Im not sure if this supposed to be like this, but i have used this guide many of times to add .co.uk domains which within /etc/nginx/sites-available/ they show the filetype UK file. however for .com domains the filetype is ms-dos application. is this correct? i am not on a windows server, im using ubuntu 16.04 nginx fpm. i followed the same instructions again with another .com domain same thing happened it saved the filetype as ms-dos application when creating the file using nano. am i missing something here?
@maltonge Both files should be plain text. I’m not sure what utility you’re using to tell you the MIME type, but it looks like it’s just reading the actual extension instead of looking at the formatting of the file itself.
So it looks like it’s seeing a file that ends in
.uk
and assuming it’s this file type and reading a.com
file and assuming it’s this file type. Windows and Windows tools use file extensions to tell it what format a file is, but Linux and other Unix-like operating systems look at the content and formatting of the file itself. On your Ubuntu server, you can view the actual file type by typing:For both instances, the results will probably be similar to this:
So the file being reported in some tools may not be accurate. Hope that helps clear up the confusion.
Nginx is serving the html files, and so when I put in a php script it just serves the file and it gets downloaded. How do I make nginx run the php files ?
I’m having this issue as well. I get a 403 forbidden, but it tries to download some file.
@fuadquazi @jbkaps01 Our guide on setting up a LEMP stack on Ubuntu 16.04 covers how to configure Nginx with PHP-FPM to serve PHP content. If you do not need a database, you can skip the MySQL steps. Hope that helps.
After following this tutorial I setup Nginx server block for my WordPress site but I m getting (403 Forbidden nginx/1.10.3 (Ubuntu) ) error and even know I have got WordPress files in there following this Tutorial but I m not able to Install WordPress because it’s not loading the WP Installation page instead it’s showing the above mention error.
My domain (russellbishop.co.uk) still shows the default nginx welcome screen. Any ideas why?
Great tutorial! Thanks
Hi all, I have a couple domains all setup at /var/www/html/ and /var/www/html/site2.
What I’d like to do is add a new domain (I already have the DNS setup). Right now, when I go to the new domain, it just redirects to the default domain of /var/www/html/ so I know the domain is working correctly.
However, what I’d like it to go to /var/www/newdomain.com/html (like in this tutorial) instead of /var/www/html/newdomain (like I have the other sites setup).
Right now, all my server blocks are in /etc/nginx/nginx.conf in one big file and not sites-available individual files. Can I add an individual file for a new single domain despite all the others being in nginx.conf?
Furthermore, I need to add SSL (I use letsencrypt for the other domains) to the new domain, as all the others are using it under the same cert. And, I’m not quite sure how to add that new domain (or do I need a new cert?). If it’d be easier to just do a new cert, I’m all for that also.
So, it’s a little daunting and I’m not sure how to do this just using the tutorial here. Any help would be immensely appreciated.
Hello, this way would be the best way to create a website with Wordpress REST API and a front end with, let’s say, Nuxt.js?
I could configure Wordpress in one server block and the frontend on another?
Sorry if I said something horribly wrong, I am quite a beginner :) Thanks
Hello, I can’t seem to make nginx serve anything other than the sample page.
I followed these:
https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-ubuntu-16-04
https://www.digitalocean.com/community/tutorials/how-to-set-up-nginx-server-blocks-virtual-hosts-on-ubuntu-16-04
I tried using sym links to the built lantea dist folder of my site and pass those to the “root” directive, I tried using the direct path directly to it, even without any sym links in the way, I tried changing the permissions like this: “chmod +rX -R /root/Lantea/current/dist” where current is a symlink. I also tried without the symlink with the absolute path, but nothing seems to work.
When I serve the sample page from “/var/www/html” it works, but not when I replace it with my index.html from any other path.
I tried that with the default server bloc, and creating a new server block like it’s described in this guide, but it didn’t work.
I don’t know what to try anymore, please help.
Thank you
I did not had to look for another article on this topic. Your article did it for me.Thank you.
Hello,
I set this up and everything went fine. Are those html pages supposed to go away on their own though? I hadn’t transferred files over until just now hoping that once I did my site would show up instead of that page.
What else do I need to do?
Hi,
Are there other ways to configure this for multiple projects/domains. Let say 50 domains so, I need to create 50 configurations?
I had a few question about this. im trying to understand how block is functioning start from someone request from ip or domain to enabledd site to blablabla to index.php or index.html Sorry for dumb question. But i not asking to answer all my question
Create the First Server Block File First, we need to look at the listen directives. Only one of our server blocks on the server can have the default_server option enabled.
im start confusing by this😯. coz i got error after
sudo nginx -t
and it show double default server.Then i run this command
grep -R default_server /etc/nginx/sites-enabled/
and it show me 2 default server. Im stuck at here now. I guess to edit file in enabled site to remove default server word from it. But what block should i remove it? Damn… so confusing! suggestion please?again… Im not asking to answer all my question. A few also help me a lot
@foodiezalpha Hey there. Let me see if I can help clear up some of this.
First off, if you’re interested in understanding how the
listen
andserver_name
directives and thedefault_server
option help Nginx decide which configuration to use, you can check out this article.To try to address your specific questions:
1. i had error because of multiple default server. actually only two. how to solve it?
Nginx is telling you here that the
default_server
option has been set on multiplelisten
directives of the same IP address / port specificity. Since Nginx uses thedefault_server
option as a tie breaker to determine which configuration to use in ambiguous situations, defining the option twice is not allowed. To fix this, you’ll need to figure out which files are definingdefault_server
and remove that option from one of them. Which one you remove will depend on how you want your server to operate. If a client sends a request to your server that doesn’t match any of theserver_name
values you’ve defined, which configuration do you want to use? If you’ve set up theserver_name
properly in each of your server blocks, this situation shouldn’t occur frequently, so it’s not too important of a decision.2. What purpose that default server is use? What im understand myself, it is main block to point if someone request from ip directly rather than domain. what block should be default server?
Even if the client is using a domain, the
listen
directive anddefault_server
setting can come into play. The web browser always contacts the server using an IP address regardless of whether a domain is used (it uses the DNS system to find the right IP address if necessary). Thedefault_server
is used when:listen
directives that match the requested IP address / port combination.server_name
that matches the request.So, imagine that the DNS for
one.com
,two.com
, andthree.com
all point to your server’s IP address. You have defined two server blocks, each listening on port 80. One block hasserver_name one.com;
and the other hasserver_name two.com
. If a client requeststhree.com
, Nginx will need to know which server block to use since the information in the request does not match any of your configuration.3. Why cant only use one default to make all domain and subdomain or block to be refered to?
I’m having a bit of trouble understanding this one actually, so I’m going to skip it.
4. Why does we need to use sites enabled and available?
The
sites-enabled
andsites-available
pattern is one that Debian-based systems originally adopted for managing Apache. This isn’t required, but the directory organization does provide some pretty strong benefits that are intended to help you manage your site configurations easier.Using this pattern, Nginx is configured to read files in the
/etc/nginx/sites-enabled
directory and append them to its configuration. In contrast, Nginx never reads files from the/etc/nginx/sites-available
directory. Instead, thesites-available
directory is used as a kind of staging area to hold all of the configuration files you may wish to apply. You can then change the configuration by creating or removing links from files in the/etc/nginx/sites-avialable
directory to the/etc/nginx/sites-enabled
directory and restarting Nginx. By removing the link, you can disable configuration without deleting it or commenting it out. Many users find this helpful, but if you don’t like it, you can also create files directly in the/etc/nginx/sites-available
directory or append your configuration to the/etc/nginx/nginx.conf
file directly.5. What does this purpose? i mean… “ln -s” command. why dont just copy paste to enabled site? and how to disable site after after run this command? and how to prevent it become default server if i use this command?
sudo ln -s /etc/nginx/sites-available/test.com /etc/nginx/sites-enabled/
The
ln -s
command creates a link in the/etc/nginx/sites-enabled
directory to a file in the/etc/nginx/sites-available
directory. As mentioned above, you can definitely just put the configuration directly in the/etc/nginx/sites-enabled
directory if you prefer, but by creating links, you gain the ability to easily enable (by creating a link withln -s
) or disable (by removing the link withrm
) configuration files.If you you have linked the a file using
ln -s
to the/etc/nginx/sites-enabled
directory to enable it, you can disable the configuration by removing the link:sudo rm /etc/nginx/sites-enabled/name-of-link
and restarting Nginx:sudo systemctl restart nginx
. Your file will still be available in the/etc/nginx/sites-available
directory if you change your mind later.Hope that helps a bit!
You da best!
i cant get this to work it always redirects me to default :(
Hey, thank you for the comprehensive guide but for some reason only the www.[DOMAIN_NAME].com works, the [DOMAIN_NAME].com doesn’t return anything. Thanks!
Nevermind, this eventually worked. Might have been browser cache.
On the stage three, creating server block, you have the bellow as an example
Can I have something like the bellow?
That is having total different domain name on one server block under server_name
I have followed every step but, test.com is not showing. Nginx -t shows all configurations are okay but i can’t execute my server block. I ain’t got any idea as I am new to nginx
wow… great article. thanks a lot.
Hello;
I did everything the same as you did, but the end result I got was “Hmmm… can’t reach this page”. Can anyone please help me, thanks!!!
how to enable php
This tutorial was really helpful. I was able to host 3 sites with single public IP flawlessly. Once i understood the pattern of filename and content format, it was piece of cake!
thank you very much.