How do I point my Subdomain to correct root folder

cwd asked:

I have my production site set up and now I’m trying to setup a dev/staging site to run live tests in.

I’m having issues pointing my subdomain to the correct root path on my Laravel app.

I need the following:

  • dev.example.com to point to root /var/www/dev/public;
  • example.com and www.example.com to point to root /var/www/laravel/public;
  • currently everything is going to /var/www/laravel/public

Server:

  • Ubuntu 18.10 x64
  • Nginx

I have a domain example.com and I made a subdomain called dev.example.com
each reference to the domain is in my SSL Cert via certbot/LetsEncrypt. www.example.com, example.com and dev.example.com

DNS for Subdomain:

A (Record) | dev (Name) | 140.xx.xx.xx (Data)

I have 2 folders located here:

  • /var/www/laravel/public (My Main Production site)
  • /var/www/dev/public (My Development Site)

In sites-available /etc/nginx/sites-available I have the default file with the following configurations.

(I’ve tried multiple variation of this, but this is where I left off)

 server {

    root /var/www/laravel/public;

    index index.php index.html index.htm index.nginx-debian.html;

    server_name example.com www.example.com dev.example.com;

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

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

    location ~ /\.ht {
        deny all;
    }

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}

# Virtual Host configuration for example.com

server {

    if ($host = dev.example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    listen 80;
    listen [::]:80;
    server_name dev.example.com;
    root /var/www/dev/public;
    return 404; # managed by Certbot

}

server {

    if ($host = www.example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    listen 80;
    listen [::]:80;
    server_name www.example.com;
    return 404; # managed by Certbot

}

server {

    if ($host = example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    listen 80 default_server;
    listen [::]:80;
    server_name example.com;
    return 404; # managed by Certbot

}

Again…

  • I need dev.example.com to point to root /var/www/dev/public;

  • I need example.com and www.example.com to point to root /var/www/laravel/public;

Any help in figuring this out would be appreciated.

My answer:


You need to create a new server block to serve the new hostname with the root that you desire. You can either copy the rest of the configuration from the existing block, or later, make use of includes to DRY it up.


View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.