Search code examples
node.jsnginxplesk

nginx reverse proxy nodejs subdomain


I am having a problem.

I have a node application running on :3000 on a subdomain inside of Plesk.

I have the subdomain https://xxx.flamingocams.co.uk ; when I navigate to the subdomain it displays the default plesk page and this is the problem;

I have tried to change the port of the node application to 80 and 443 however this conflicts with plesk. I have no issues when accessing the node application on https://xxx.flamingocams.co.uk:3000.

Now the only other thing I've seen other people attempt is a reverse proxy;

I found this example;

server {
        listen 0.0.0.0:80;
        server_name xxx.flamingocams.co.uk;
        access_log "/var/log/nginx/xxxflam.log";
        location / {
            proxy_pass http://127.0.0.1:3000/;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-NginX-Proxy true;
            proxy_cache_bypass $http_upgrade;
        }
}

I am running Plesk Obsidian v18.0.34_build1800210325.10 os_Ubuntu 16.04 so my question is, Where would I place this config to get the subdomain to point only to the nodejs application?

And is this config correct for what I'm trying to achieve?

I have little to no knowledge on nginx configuration my apologies

I have checked out this post and the answer says I need to add a config /etc/nginx/sites-available/yourdomain.com however I do not have the directory sites-available

response to comments // xxx.flamingocams.co.uk.conf

server {
        listen 0.0.0.0:80;
        server_name xxx.flamingocams.co.uk;
        access_log "/var/log/nginx/xxxflam.log";
        location / {
            proxy_pass http://127.0.0.1:3000/;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-NginX-Proxy true;
            proxy_cache_bypass $http_upgrade;
        }
}

server {
        listen 0.0.0.0:443;
        server_name xxx.flamingocams.co.uk;
        ssl_certificate             /opt/psa/var/certificates/scfZc0CwJ;
        ssl_certificate_key         /opt/psa/var/certificates/scfZc0CwJ;
        server_name xxx.flamingocams.co.uk;
        access_log "/var/log/nginx/xxxflam.log";
        location / {
            proxy_pass http://127.0.0.1:3000/;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-NginX-Proxy true;
            proxy_cache_bypass $http_upgrade;
        }
}

Solution

  • In your config, the server is running only on port 80

      listen 0.0.0.0:80;
    

    With this config, the page : http://xxx.flamingocams.co.uk will display your application (the request come to the NginX proxy then is forwarded to NodeJS application on port 3000). Because there aren't any server block listen on port 443, the default Plesk screen is displayed.

    In order to have your app running on https, you need to listen on port 443 on NginX, you also need to configure the SSL certificate

    The config would be :

    server {
            listen 0.0.0.0:443;
            server_name xxx.flamingocams.co.uk;
            ssl_certificate     path_to_your_ssl_certificate;
            ssl_certificate_key path_to_your_ssl_key;
    
            # The rest of your config is ok :)
    
    }