multiple websites on nginx & sites-available

LinuxNginxWebserver

Linux Problem Overview


With the base install of nginx, your sites-available folder has just one file: default

how does the sites-available folder work and how would I use it to host multiple (separate) websites?

Linux Solutions


Solution 1 - Linux

Just to add another approach, you can use a separate file for each virtual domain or site you're hosting. You can use a copy of default as a starting point for each one and customize for each site.
Then create symlinks in sites-enabled. In this way you can take sites up and down just by adding or removing a symlink and issuing a service nginx reload.

You can get creative and use this method to redirect sites to a maintenance mode page while you are doing site maintenance.

So the structure looks like this:

/sites-available/ (you can use obvious file names like this)
| 
|-> a.mysite.com
|-> b.mysite.com
|-> someOtherSite.com

/sites-enabled/ (these are just symlinks to the real files in /sites-available)
| 
|-> a.mysite.com
|-> b.mysite.com

Notice that since there are only the first two entries are the only symlinked items in sites-enabled, the third entry, someOtherSite.com is therefore offline.

Solution 2 - Linux

If you look at nginx.conf, you will find include directive that includes all files from the sites-enabled directory. This directory stores symlinks to config files from sites-available in order to be convenient to switch on and off parts of your configuration.

As you can see, there's no magic with these directories.

If you want to host multiple websites you should use multiple server blocks and/or server_name directive. Official tutorials are here: Server names and How nginx processes a request.

Solution 3 - Linux

You symlink the default file from sites available to sites enabled. Then you modify the available site to include two server blocks each with a different server_name. see the following. This assumes you have to domains called example.com and example2.com. You would also have pointed your @records to the ip address of the server where you've installed nginx.

symlink the available site to an enabled site

sudo ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled/default

edit the file using your editor of choice (vim for me)

sudo vi /etc/nginx/sites-available/default

Here's contents of working nginx conf, assuming you are running web apps on the port 4567 and 4568.

server {

    server_name www.example.com

    location / {
        proxy_pass http://localhost:4567/;
    }

}


server {

    server_name www.example2.com

    location {
        proxy_pass http://localhost:4568/;
    }

}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKristianView Question on Stackoverflow
Solution 1 - LinuxCarlosView Answer on Stackoverflow
Solution 2 - LinuxVBartView Answer on Stackoverflow
Solution 3 - LinuxjmontrossView Answer on Stackoverflow