Report this

What is the reason for this report?

Passing subdomains to a Node.js app running behind Nginx

Posted on June 18, 2017

I’m trying to implement wildcard subdomains in my Node.js app – for example, a user visiting subdomain.example.com will hit the app, in addition to example.com.

I currently have a Node app running on example.com, as well as an additional WordPress site running at example.com/blog. Nginx is acting as a reverse proxy for both of them (server block below). I’ve made changes locally for routing subdomains once they get to the Node app, and they seem to be working locally. But trying to access a subdomain on my Droplet just hangs. I’ve also previously tried setting up a Node app on a subdomain of the Droplet (ex. app.example.com) with similar lack of success – the page just hangs. (I don’t even get a 503 back.)

Something that I’ve noticed is that the server_name specified seems to be ignored; even without specifying one the routing still seems to happen normally.

Any help would be much appreciated!

# Point http requests to https
server {
    listen 80;

    server_name example.com www.example.com;
    return 301 https://$server_name$request_uri;
}

server {
    listen 443 ssl http2 default_server;
    listen [::]:443 ssl http2 default_server;
    include snippets/ssl-example.com.conf;
    include snippets/ssl-params.conf;

    root /var/www/html;
    index index.php index.html index.htm index.nginx-debian.html;

    server_name example.com *.example.com;

    location / {
        proxy_pass https://example.com:3000;
        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;
    }

    location /blog {
        try_files $uri $uri/ /index.php$is_args$args;
    }

    # For Lets Encrypt certbot
    location ~ /.well-known {
        allow all;
    }

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

    location ~ /\.ht {
        deny all;
    }

    location /favicon.ico { alias /var/www/html/example/favicon.ico; }
    location = /favicon.ico { log_not_found off; access_log off; }
    location = /robots.txt { log_not_found off; access_log off; allow all; }
# These lines seem to mess with serving static files on the Node app. Might need some other configuration
#    location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
#        expires max;
#        log_not_found off;
#    }

}

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Start building today

From GPU-powered inference and Kubernetes to managed databases and storage, get everything you need to build, scale, and deploy intelligent applications.