Nginx to give HTTPS over port 10000 and Basic Auth for Express app?

Viewed 91

I have a Linux NodeJS/Express app that just exposes a text file to http://example.com/secret.txt and I would like to have only accessable over HTTPS on port 10000 and Basic Auth.

The secret.txt is not a file on the file system. It only exists in memory.

All this is easy to set up in Nginx, so the question becomes:

Question

Can Nginx be used to give my NodeJS/Express app HTTPS over port 10000 with Basic Auth?

1 Answers

I'm not sure I understand your environment correctly, but if I am, the very basic server block would look like

server {
    listen 10000 ssl;
    server_name example.com;
    ssl_certificate /path/to/your/certificate;
    ssl_certificate_key /path/to/your/certificate/key;

    location / {
        # how we must serve other requests? assuming we should return 404 not found
        return 404;
        # we can also use special nginx 444 code to close incoming connection immediately
    }
    location = /secret.txt {
        auth_basic "Restricted area"; # whatever you want message
        auth_basic_user_file /path/to/your/passwords/file;
        proxy_pass http://127.0.0.1:<your_nodejs_app_port>;
    }
}

It should be quite obvious that the node.js app port should not be exposed to the internet and should be accessible only locally via 127.0.0.1 or localhost.

Related