Serving two different root folders from ngix

April 26, 2018 474 views
Nginx DigitalOcean Ubuntu 16.04

Hi, I've got a working server running nginx with the site information below

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

    root /var/www/app1/public;

    index index.php;

    server_name 12.23.456.789;

    location / {
    try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
    }


    location ~ /\.ht {
    deny all;
    }

}

server {
listen 8080;
listen [::]:8080;

    root /var/www/app2/public;

    index index.php;

    server_name 12.23.456.789;

    location / {
    try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
    }


    location ~ /\.ht {
    deny all;
    }

}

Unfortunately I can not manage the get the same working setup in DigitalOcean Droplet... Any ideas might might be the case or what am I missing? Just getting errors about conflicting server name

conflicting server name "12.23.456.789" on 0.0.0.0:80, ignored
conflicting server name "12.23.456.789" on [::]:80, ignored
conflicting server name "12.23.456.789" on 0.0.0.0:8080, ignored
conflicting server name "12.23.456.789" on [::]:8080, ignored

2 Answers

EDIT: The config you copy/pasted above should actually work. There must be duplicate copies of your config, basically a place where you are defining a server block with the same server_name and port twice.

Take a look at this tutorial for complete context on nginx config for multiple sites: https://www.digitalocean.com/community/tutorials/how-to-set-up-nginx-server-blocks-virtual-hosts-on-ubuntu-16-04

nginx config would fail with the error conflicting server name if you have multiple server {} blocks with the same PORT and SERVER_NAME lines.

nginx config could be in files in /etc/nginx/sites-enabled/default or in /etc/nginx/conf.d - take a look and confirm that there are no places where you are defining the same port and server_name in two different server{} blocks.

by Justin Ellingwood
When using the Nginx web server, server blocks (similar to the virtual hosts in Apache) can be used to encapsulate configuration details and host more than one domain off of a single server. In this guide, we'll discuss how to configure server blocks in Nginx on an Ubuntu...
  • Thanks for the reply... Unfortunately I can not locate any duplicate server blocks. I've also deleted all default configs, folders symlinks etc... and the server is set up following the tutorials mentioned...

    The default (port 80) works fine, :8080 still doesn't respond

    • Do you have a firewall blocking port 8080?

      For example:

      • UFW Firewall: when you type ufw status does it say: "Status: Active"? Is 8080 allowed?
      • DO Cloud Firewall: Are you using Cloud Firewalls on this Droplet? If so is 8080 allowed
      by Melissa Anderson
      DigitalOcean Cloud Firewalls, available in all regions at no charge, provide a network-based, stateful firewall service for your DigitalOcean Droplets. They block all traffic that isn't expressly permitted by a rule. They're designed to be easy to configure, quick to apply, and automation-friendly. In this guide, we'll explore how to create and manage DigitalOcean Cloud Firewalls.
      • ufw status:

        Status: active

        To Action From


        OpenSSH ALLOW Anywhere
        Nginx HTTP ALLOW Anywhere
        OpenSSH (v6) ALLOW Anywhere (v6)
        Nginx HTTP (v6) ALLOW Anywhere (v6)

        Cloud Firewall is set up like this:

        Inbound
        Type Protocol Port Range Sources
        SSH TCP 22 All IPv4 All IPv6

        HTTP TCP 80 All IPv4 All IPv6

        Custom TCP 8080 All IPv4 All IPv6

        Outbound
        Type Protocol Port Range Destinations

        ICMP ICMP All IPv4 All IPv6

        All TCP TCP All ports All IPv4 All IPv6

        All UDP UDP All ports All IPv4 All IPv6

        • Sounds like you found a workaround, as a follow-up, it looks like you were blocking port 8080 in ufw.

          Running ufw allow 8080 would open up port 8080 to external access

Find a fix (at least a temporary one). I'm using a floating ip as the other servername. Seems to work...

Have another answer? Share your knowledge.