Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mstdmstd's avatar

How to set multiple domain names on 1 nginx server ?

Hello, I installed nginx under new ubuntu 18 installation under AWS and I want to make multiple domain names on 1 server. Fot this

  1. I created at freenom new hosting
demo1.myaccount-apps.tk
and with whatsmydns service I check that it is resolved ok:
https://www.whatsmydns.net/#CNAME/demo1.myaccount-apps.tk
  1. I installed nginx with "Welcome to nginx!" greeting page by
http://ec2-NN-NN-NN-NN.us-east-2.compute.amazonaws.com/

url

  1. Created /var/www/votes.com/index.php file with text
File <b>/var/www/votes.com/index.php </b> index
<?php
echo ‘Hello all!’;
phpinfo();
?>
  1. I created file /etc/nginx/sites-available/votes.com with text:
pstream votes-backend { # Lets you define a group of servers
   server unix:/var/run/php7.2-fpm.sock;
}

server {
   listen 80;

   root /var/www/votes.com;
   index index.php;

   server_name demo1.myaccount-apps.tk;


   # We keep this block, because it doesn't make sense to pass
   # everything to PHP.
   location / {
      # try_files $uri $uri/ =404;
      try_files $uri $uri/ /index.php index.php;

   }

   location ~ \.php$ {
     fastcgi_split_path_info ^(.+\.php)(/.+)$; # this defines a regular expression to separate the SCRIPT_FILENAME and PATH_INFO for later use
     try_files $uri =404; # OR Set cgi.fix_pathinfo=0 in php.ini (for security)
     fastcgi_pass votes-backend; # OR unix:/var/run/php7.2-fpm.sock OR 127.0.0.1:9000
     fastcgi_index index.php; # appends index.php to a URI ending with a slash
     include fastcgi_params;
   }

}
  1. editied /etc/hosts (I am not sure if i have to edit it):
127.0.0.1 localhost
127.0.0.2   localhost test.com
127.0.0.3   demo1.myaccount-apps.tk
  1. created symbol link :
sudo ln -s /etc/nginx/sites-available/votes.com /etc/nginx/sites-enabled/
  1. restart nginx service and php:
sudo service nginx restart
sudo systemctl restart php7.2-fpm
  1. Check The Syntax Of nginx Files
sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
  1. check that /etc/nginx/nginx.conf has links to /etc/nginx/sites-enabled/:
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
        worker_connections 768;
        # multi_accept on;
}

http {

        ...
        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
}
  1. but running http://demo1.myaccount-apps.tk/ in browser I got 404 error also running
curl http://demo1.myaccount-apps.tk/

in console of my server I got empty line, but I expected content of my /var/www/votes.com/index.php file?

What is wrong and how to fix it ?

Thanks!

0 likes
13 replies
ftiersch's avatar

Have you edited the /etc/hosts file on your local machine?

In that case you would try to access the page on IP 127.0.0.3 (which I guess your server doesn't have :)). You can skip that step. With it you tell the machine "for this URL always use that IP" which you don't want to do.

mstdmstd's avatar

On my local server in my /etc/hosts I have a line :

127.0.0.3       local-tasks.com

as I use appache with several my php apps. How to resalve this issue ?

mstdmstd's avatar

Searching in net I found hint to edit in /etc/hosts new line

ec2-NN-NN-NN-NN.us-east-2.compute.amazonaws.com   demo1.myaccount-apps.tk;

where demo1.myaccount-apps.tk - my domain created at freenom and ec2-NN-NN-NN-NN.us-east-2.compute.amazonaws.com - my aws account, I got "Welcome to nginx!" greeting page by it

and in /etc/nginx/sites-available/votes.com defined:

server_name demo1.myaccount-apps.tk;

but that did not help...

mstdmstd's avatar

I cleared log files and restarted php and nginx

running

curl http://demo1.myaccount-apps.tk/ 

at server's console I got :

...
...
    <body class="antialiased font-sans">
        <div class="md:flex min-h-screen">
            <div class="w-full md:w-1/2 bg-white flex items-center justify-center">
                <div class="max-w-sm m-8">
                    <div class="text-black text-5xl md:text-15xl font-black">
                        404                    </div>

                    <div class="w-16 h-1 bg-purple-light my-3 md:my-6"></div>

                    <p class="text-grey-darker text-2xl md:text-3xl font-light mb-8 leading-normal">
                        Sorry, the page you are looking for could not be found.                    </p>

                    <a href="http://demo1.myaccount-apps.tk">
                        <button class="bg-transparent text-grey-darkest font-bold uppercase tracking-wide py-3 px-6 border-2 border-grey-light hover:border-grey rounded-lg">
                            Go Home
                        </button>
                    </a>
                </div>
            </div>

            <div class="relative pb-full md:flex md:pb-0 md:min-h-screen w-full md:w-1/2">
                <div style="background-image: url(http://demo1.myaccount-apps.tk/svg/404.svg);" class="absolute pin bg-cover bg-no-repeat md:bg-left lg:bg-center">
</div>
            </div>
        </div>
    </body>
...
...

404 error ruuning demo1.myaccount-apps.tk at browser.

Files /var/log/nginx/access.log and /var/log/nginx/error.log are empty File /var/log/php7.2-fpm.log has:

20-Mar-2019 04:30:05] NOTICE: Terminating ...
[20-Mar-2019 04:30:05] NOTICE: exiting, bye-bye!
[20-Mar-2019 04:30:05] NOTICE: fpm is running, pid 4304
[20-Mar-2019 04:30:05] NOTICE: ready to handle connections
[20-Mar-2019 04:30:05] NOTICE: systemd monitor interval set to 10000ms

Any ideas ?

mstdmstd's avatar

Thank you for your feedback! As I am new in AWS, could you please explain what is "hosted zone in Route 53" Is it service available with my free account?

ettore's avatar

You find Route 53 here: https://console.aws.amazon.com/route53/home

Thats where you do the DNS management. When you are satisfied with your domains and all pointing to your server, then you can set up the server blocks from the digital ocean guide i posted in my last reply.

Another option if you dont have domain is to set up NGINX to listen to different ports,

example of your url ec2-NN-NN-NN-NN.us-east-2.compute.amazonaws.com:81

Then your request gets channeled to a specific port on the server that NGINX listens to like port 81. You might have to update your AWS Security Groups to allow communication on that port if you do it this way.

Cruorzy's avatar

Does you nginx.conf include include /etc/nginx/sites-enabled/*; at all?

mstdmstd's avatar

@CRUORZY - Yes I wrote about it in first topic ; check that /etc/nginx/nginx.conf has links to /etc/nginx/sites-enabled/:

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
        worker_connections 768;
        # multi_accept on;
}

http {

        ...
        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
}

dO YOU MEAN THIS ?

mstdmstd's avatar

@ETTORE - I try first method mentioned by you and I created “sn.votes.demo.org” and I see https://prnt.sc/n1166s with domain names name servers at right.

By documentation “How To Set Up Nginx Server Blocks (Virtual Hosts) on Ubuntu 16.04” in your link looks like domain name “nsn.votes.demo.org” I created must be used instead of “example.com” in this article. Is it so ?

ettore's avatar

Yes, and did you create the record set inside that hosted zone, and make them point to your server? Every record is the url you want to point to you server.

Then you just set up normal serverblocks like described in that guide ;) You use the url from the records you have created inside the server block file in the folder: sites-available/*

mstdmstd's avatar

Thank you for your help and provided link. Actually what I did was similar to this article, but opening my site in browser I see some different site opened.

My /etc/nginx/sites-available/votes.com :

upstream votes-backend { # Lets you define a group of servers
   server unix:/var/run/php7.2-fpm.sock;
}

server {
   listen 80;
   listen [::]:80;

   root /var/www/votes.com;
   index index.php;

   server_name  nsn.votes.demo.org  www.nsn.votes.demo.org;


   # We keep this block, because it doesn't make sense to pass
   # everything to PHP.
   location / {
      # try_files $uri $uri/ =404;
      try_files $uri $uri/ /index.php index.php;

   }

   location ~ \.php$ {
     fastcgi_split_path_info ^(.+\.php)(/.+)$; # this defines a regular expression to separate the SCRIPT_FILENAME and PATH_INFO for later use
     try_files $uri =404; # OR Set cgi.fix_pathinfo=0 in php.ini (for security)
     fastcgi_pass votes-backend; # OR unix:/var/run/php7.2-fpm.sock OR 127.0.0.1:9000
     fastcgi_index index.php; # appends index.php to a URI ending with a slash
     include fastcgi_params;
   }

}

where nsn.votes.demo.org url is new created Domain Name - Public hosted zone (I provided a link to it's printscreen)

In /etc/hosts :

127.0.0.1 localhost
127.0.0.2   localhost test.com
#  127.0.0.3   demo1.nilov-sergey-demo-apps.tk
ec2-13-NN-NN-NN.us-east-2.compute.amazonaws.com   nsn.votes.demo.org  www.nsn.votes.demo.org

# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts

Is it valid format ? I checked log files : /var/log/nginx/access.log, /var/log/nginx/error.log, /var/log/php7.2-fpm.log But did not find any errors.

Opening www.nsn.votes.demo.orgin browser I see quite different site opened

and I see ref to this site by console command :

$ curl www.nsn.votes.demo.org
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>302 Found</title>
</head><body>
<h1>Found</h1>
<p>The document has moved <a href="http://www.untergrund.net">here</a>.</p>
<hr>
<address>Apache/2.2.15 (CentOS) Server at www.nsn.votes.demo.org Port 80</address>
</body></html>

What is wrong ?

Please or to participate in this conversation.