0

I currently have example.com domain set up on NGINX and it works. I'd like to set up a subdomain test.example.com, but after following multiple different instructions I either keep getting 404 or being redirected to main domain.

My server configuration and file-structure look like this:

/usr/local/nginx
├── conf
│   ├── fastcgi_params
│   ├── includes
│   ├── nginx.conf
│   ├── nginx.conf.save
│   ├── sites
│   │   ├── example.com.conf
│   │   ├── test.example.com.conf
│   ├── uwsgi_params
│   └── win-utf
├── html
├── logs
└── snippets
    ├── ssl-example.com.conf
    └── ssl-params.conf

/var/www
├── example.com
│   └── ...
└── test.example.com
    └── index.html

I've updated DNS records so that "*.example.com" points to the same IP

Default domain example.com.conf file looks like this:

server {
    listen 80;
    server_name exmaple.com www.example.com;
    return 301 https://$server_name$request_uri;
}
server {
    listen 443 ssl;
    server_name example.com;
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    location ~ /.well-known {
        root /var/www/lets-encrypt/example.com;
        access_log off;
        expires max;
        break;
    }
    include /usr/local/nginx/conf/includes/m2-php71.conf;
}

And subdomain test.example.com.conf file:

server {
    listen 80;
    server_name test.example.com;
    return 301 https://$server_name$request_uri;    
}

server {
    listen 443 ssl;
    server_name test.example.com;
    ssl_certificate /etc/letsencrypt/live/test.example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/test.exampe.com/privkey.pem;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    location ~ /.well-known {
        root /var/www/lets-encrypt/test.example.com;
        access_log off;
        expires max;
        break;
    }
    include /usr/local/nginx/conf/includes/m2-php71.conf;
}

when I remove listen 443 part, it redirects to the main domain.

I'd like the request for sub-domain test.example.com to be served from var/www/test.subdomain.com but cant seem to get past 404 error

0

You need to do the following -

  1. remove www.example.com from one of the two configurations.
  2. Add alias in server section for both the configs.

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.