NGINX server block configuration for hosting multiple websites on Ubuntu 16.04

9,212

Your nginx.config needs something like this line. This is what I do on my server

include /etc/nginx/enabled-sites/*;

In that directory you can have one file with many servers, or you can do what I do and have servers grouped by domain.

File abcde.conf

server {
  listen 80;
  server_name www.abcde.org;

  root /var/www/home;

  # Any locations you want. PHP example that I use below.
  location ~ \.php$ {
    fastcgi_keep_conn on;
    fastcgi_intercept_errors on;
    fastcgi_pass   php;
    include        fastcgi_params;
    fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
  }
} # ends www.abcde.org server

# This server forwards to the www domain
server {
  listen 80;
  server_name abcde.org;
  return 301 https://www.abcde.org$request_uri;
} # ends abcde.org server

File example.conf

# server for a completely separate domain
server {
  listen 80;
  server_name www.example.com;

  root /var/www/example;

  # Any locations you want
} # ends www.example.com server

File default_server.conf

# This just prevents Nginx picking a random default server if it doesn't
# know which server block to send a request to
server {
  listen      80 default_server;
  server_name _;
  # This means "go away", effectively. You can also forward somewhere
  # or put default_server onto any of your server blocks.
  return      444; 
}
Share:
9,212

Related videos on Youtube

user58859
Author by

user58859

Updated on September 18, 2022

Comments

  • user58859
    user58859 over 1 year

    I want to host multiple wordpress websites on a Ubuntu 16.04 (Ubuntu-NGINX-MariaDB-PHP). I don't want to use wordpress multisite.

    I followed this guide . Everything is fine but I can host only one site. Whenever I created multiple server block configuration, it starts to show error and NGINX fail to start. I am not getting my config file correct. Here is the config file :

    server {
         listen [::]:80 ipv6only=off;
         server_name abcde.org www.abcde.org;
    
         root /var/www/abcde;
    
        # Add index.php to the list if you are using PHP
        index index.php     index.html index.htm index.nginx-debian.html;
    
        location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            # try_files $uri $uri/ =404;
            try_files $uri $uri/ /index.php?q=$uri&$args;
        }
    
                error_page 404 /404.html;
                error_page 500 502 503 504 /50x.html;
                location = /50x.html {
                    root /usr/share/nginx/html;
            }
    
         location ~ \.php$ {
            include snippets/fastcgi-php.conf;
        #
        #   # With php7.0-cgi alone:
            fastcgi_pass 127.0.0.1:9000;
        #   # With php7.0-fpm:
        #   fastcgi_pass unix:/run/php/php7.0-fpm.sock;
         }
    
    
         location ~ /\.ht {
            deny all;
         }
    }
    

    If I host only one website, it works fine. But as soon as I host the other website, NGINX fail to start. I use same config for both sites, after changing the server name and root directory.

    Kindly guide me for the right configuration for NGINX server blocks.