Wildcard subdomains in Nginx

Kunal Punjabi kunalspunjabi at gmail.com
Wed Jul 22 06:41:31 UTC 2020


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 (tinyadults.com), so that
users can go to abc.tinyadults.com, xyz.tinyadults.com, 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 http://localhost:4001;

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

However, I've not been able to get step 1 working. Can someone who is
experienced with nginx setup please guide me?

Below is my nginx config from sites-available/tinyadults.com.conf:

server {

    index index.html index.htm;

    server_name tinyadults.com www.tinyadults.com;


    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
https://www.digitalocean.com/community/tutorials/how-to-configure-nginx-to-use-custom-error-pages-on-ubuntu-14-04

    error_page 404 /custom_404.html;

    location = /custom_404.html {

        root /etc/nginx/sites-available/custom_nginx_error_pages;

        internal;

    }


    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/tinyadults.com-0001/fullchain.pem; # managed by
Certbot

    ssl_certificate_key
/etc/letsencrypt/live/tinyadults.com-0001/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



}


server {

    server_name tinyadults.com;

    if ($host = tinyadults.com) {

        return 301 https://$host$request_uri;

    } # managed by Certbot


    listen 80 default_server;

    listen [::]:80 default_server;

    return 404; # managed by Certbot

}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.nginx.org/pipermail/nginx/attachments/20200721/85055f7d/attachment.htm>


More information about the nginx mailing list