DEV Community 👩‍💻👨‍💻

Discussion on: Configure nginx to host multiple subdomains

kp profile image

Hi Santosh,

I've been struggling with setting up nginx subdomains on my linode instance and setting up CNAME redirects.

What I need is to be able to do:

  1. First set up wildcard subdomains on my server (, so that users can go to,, etc.
    My server is running nuxt.js on port 4001 (default port is 3000 but I chose to use 4001 as a non-standard port), so I guess I have to use reverse proxies:
    proxy_pass localhost:4001;

  2. Then for my users I need to set up CNAME redirects from to, and from to, so that if I visit , it would serve the contents (without redirecting me) of For testing purposes I have an additional domain ( that we could use.

However, I've not been able to get step 1 working. Any ideas?

Below is my nginx config from sites-available/

server {
    index index.html index.htm;

    location / {
        # WARNING: https in proxy_pass does NOT WORK!! I spent half a day debugging this.
        #proxy_pass https://localhost:4001;
        proxy_pass http://localhost:4001;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;


    # Kunal: create a custom 404 nginx page, from
    error_page 404 /custom_404.html;

    location = /custom_404.html {
        root /etc/nginx/sites-available/custom_nginx_error_pages;

    listen [::]:4001 ssl http2; # managed by Certbot, modified by Kunal to add http2
    listen 4001 ssl http2; # managed by Certbot, modified by Kunal to add http2
    #Install SSL certificates and configure https:// on a per-domain-basis by running:
    #sudo certbot --nginx
    #(when prompted, be sure to select the option to set up redirects from http to https and effectively "disable" http)
    ssl_certificate /etc/letsencrypt/live/; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

server {
    if ($host = {
        return 301 https://$host$request_uri;
    } # managed by Certbot
    listen 80 default_server;
    listen [::]:80 default_server;
    return 404; # managed by Certbot
Enter fullscreen mode Exit fullscreen mode