1

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.

2

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.

  • I've tried that by duplicating my default block and editing it for the dev site , but I get the error nginx: [emerg] duplicate listen options for [::]:443 in /etc/nginx/sites-enabled/default – cwd Mar 28 at 3:47
  • 1
    @cwd That's an unrelated issue; the answer is here. Fix that, then duplicate the server block. – Michael Hampton Mar 28 at 3:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.