Nginx Multiple Domains Same Port Only Get One Domain


Nginx Multiple Domains Same Port Only Get One Domain

Understanding the Problem

You just recently set up your Nginx web server and you’re trying to set up multiple domains on the same port only to find that one of the domains is taking higher priority than the rest, while the other domains can’t be accessed. This is because Nginx by default, only allows a single domain to be assigned to one port number. In order to allow multiple domains to use the same port, there are certain settings that you need to configure.

The problem lies not in Nginx, but in the DNS settings for each of the domains. By default, only one domain can be assigned to a single port and this has to be done through the server’s configuration. In this article, we’ll explain how to configure Nginx to allow you to assign multiple domains to the same port.

Configure The Nginx Configuration File

The first step to setting up Nginx for multiple domains is to modify the Nginx configuration file. This is usually located in the ‘/etc/nginx/sites-available’ folder and is named ‘nginx.conf’. The Nginx configuration file needs to be edited to allow for multiple domains to be used on the same port. To do this, you’ll need to first open the file in a text editor and then add the following settings:

  • In the http block, add the following lines of code:

    server_name_hash_max_size 8192;

    server_name_hash_bucket_size 128;

  • In the server block, add the following line of code:

    server_name _;

Once you’ve added the above settings, you need to save the configuration file and then restart the Nginx service for the changes to take effect. This can be done by running the following command:

sudo service nginx restart

Adding the Multiple Domains

Now that you’ve modified the Nginx configuration file, you can start to add the multiple domains that you want to use on the same port. To do this, you’ll need to open the ‘/etc/nginx/sites-available’ folder and create a new file for each of the domains. For example, if you wanted to add the domains ‘example1.com’ and ‘example2.com’, then you would create two files in the ‘sites-available’ folder, named ‘example1.com’ and ‘example2.com’. The contents of the files should look something like this:

server {
listen 80;
server_name example1.com;
root /var/www/example1.com
}

In this case, we’ve configured the server to listen on port 80 for the domain ‘example1.com’. We’ve also set the root directory for the domain to ‘/var/www/example1.com’. To add additional domains to Nginx, simply create new files with the same settings for each domain, only changing the ‘server_name’ and the ‘root’ directory to reflect the new domain.

Creating Symbolic Links

Once you’ve created the files for each of the domains, you need to create symbolic links to them in the ‘/etc/nginx/sites-enabled’ folder. This can be done by running the following command for each domain:

sudo ln -s /etc/nginx/sites-available/example1.com /etc/nginx/sites-enabled/example1.com

This will create a symbolic link for each of the domains in the ‘sites-enabled’ folder. Once all of the links have been created, make sure to restart the Nginx service for the changes to take effect. This can be done by running the following command:

sudo service nginx restart

Testing The Setup

At this point, you should have the multiple domains set up on the same port. To test this, simply open your browser and enter the address of the domain that you want to access. If everything is configured correctly, the domain should load without any issues. You can also use ‘curl’ to test the configuration as well.

Frequently Asked Questions

Q: Is it possible to run multiple domains on the same port in Nginx?

Yes, it’s possible to run multiple domains on the same port in Nginx. This can be done by editing the Nginx configuration files and adding the necessary settings to allow for multiple domains to be used.

Q: How do I create symbolic links for each domain in Nginx?

To create a symbolic link for each domain, open the ‘/etc/nginx/sites-available’ folder and run the ‘ln -s’ command for each domain. This will create a link between the ‘sites-available’ and ‘sites-enabled’ folders. Make sure to restart the Nginx service for the changes to take effect.

Conclusion

Setting up multiple domains on the same port in Nginx is relatively straightforward. Once you have edited the Nginx configuration file and added the necessary settings, you can add the multiple domains and then create symbolic links for each of the domains in the ‘sites-enabled’ folder. Once this is done, you should be able to access each of the domains on the same port.

Thank you for reading this article. Please don’t forget to read other articles.

Leave a Reply

Your email address will not be published. Required fields are marked *