Configure multi site on nginx server through IP using Digital Ocean

140
October 15, 2018, at 03:00 AM

Problem: Unable to hit two websites handled under one nginx server i.e. <<ip-address>> & <<ip-address>>/web2

Configuration on Digital Ocean:

  1. 1 Droplet / Ubuntu 18 / LEMP
  2. I have two test PHP website in the CodeIgniter framework
  3. Folder config for 1st Website: /var/www/html/web1/
  4. Folder config for 2nd Website: /var/www/html/web2/

Nginx Server Block configuration for two sites

web1.com

server {
        listen 80;
        root /var/www/html/web1;
        index index.php index.html index.htm index.nginx-debian.html;
        server_name <<ip-address>>;
        location / {
                try_files $uri $uri/ /index.php;
        }
        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
        }
        location ~ /\.ht {
                deny all;
        }
}

web2.com

server {
        listen 80;
        root /var/www/html/web2;
        index index.php index.html index.htm index.nginx-debian.html;
        server_name <<ip-address>>/web2;
        location /web2/ {
                try_files $uri $uri/ /index.php;
        }
        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
        }
        location ~ /\.ht {
                deny all;
        }
}

I am totally new to nginx server, I do according to the documentation provided by a community under a digital ocean.

Please help!

Thanks.

Answer 1

What you're trying to do is not how nginx works out of the box. It could, with a lot of fiddling, end up working that way, but I don't think it's worth the effort.

See, nginx configuration expects server_name to be either a FQDN (fully qualified domain name) or an IP address, but not a full URL with path. In your case, the request for ip-address/web2 is probably actually matching web1's config (so pointing you to /var/www/html/web1/web2/ which doesn't exist)

Best way to work this out (assuming you want to keep both sites on the same droplet): get a FQDN for each site. It could be a subdomain for a domain you already have (i.e. web1.sharad.com and web2.sharad.com)... Then on each of nginx's config files use the appropriate server name (web1.sharad.com and web2.sharad.com), check for typos and errors with sudo nginx -t and if all is OK restart nginx with sudo systemctl restart nginx

Rent Charter Buses Company
READ ALSO
hidden mod rewrite to sub folder only for certain paths

hidden mod rewrite to sub folder only for certain paths

Im looking for some advice to deal with a less than standard rewrite setup i need to create using apache

136
Question on integrating windows, apache, mysql, php [on hold]

Question on integrating windows, apache, mysql, php [on hold]

I am aware that there is such a thing as WAMPDoes this exclusively refer to the program WAMPserver?

116
PHP 3 or more Conditions for a single IF statement [duplicate]

PHP 3 or more Conditions for a single IF statement [duplicate]

This question already has an answer here:

129
Is PHP all I need for my &ldquo;controller&rdquo; in MVC [on hold]

Is PHP all I need for my “controller” in MVC [on hold]

I and a couple friends are relatively new to programmingWe have a project we want to work on that is similar in style to lynda

121