Need Help: Configure Nginx Reverse Proxy for Node.js App (Nginx + Apache2 Setup)

HestiaCP Custom Nginx Proxy Template Tutorial

I managed to solve this issue, and I’m sharing here a simple step-by-step approach on how to resolve it correctly.

1 - Creating the Template

Create the HTTP template file:

vim /usr/local/hestia/data/templates/web/nginx/myproject-3001.tpl

File content (make sure to change the naming and port):

# Custom PM2 Template for HTTP
server {
    listen %ip%:80;
    server_name %domain_idn% %alias_idn%;
    error_log /var/log/%web_system%/domains/%domain%.error.log error;
    location ~ /\.(?!well-known\/|file) {
        deny all;
        return 404;
    }
    location / {
        proxy_pass http://127.0.0.1:3001; # Internal app port
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade; # WebSocket support
        proxy_set_header Connection "upgrade"; # WebSocket support
        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-Forwarded-Proto $scheme;
    }
    location /error/ {
        alias %home%/%user%/web/%domain%/document_errors/;
    }
    include %home%/%user%/conf/web/%domain%/nginx.conf_*;
}

2 - Creating the HTTPS Template

Now let’s create the HTTPS modification:

vim /usr/local/hestia/data/templates/web/nginx/myproject-3001.stpl

File content (make sure to change the naming and port):

# Custom PM2 Template for HTTPS
server {
    listen %ip%:443 ssl;
    server_name %domain_idn% %alias_idn%;
    error_log /var/log/%web_system%/domains/%domain%.error.log error;
    ssl_certificate %ssl_pem%;
    ssl_certificate_key %ssl_key%;
    ssl_stapling on;
    ssl_stapling_verify on;
    location ~ /\.(?!well-known\/|file) {
        deny all;
        return 404;
    }
    location / {
        proxy_pass https://127.0.0.1:3001; # Internal app port (adjust as needed)
        proxy_ssl_server_name on;
        proxy_ssl_name $host;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade; # WebSocket support
        proxy_set_header Connection "upgrade"; # WebSocket support
        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-Forwarded-Proto $scheme;
    }
    location /error/ {
        alias %home%/%user%/web/%domain%/document_errors/;
    }
    include %home%/%user%/conf/web/%domain%/nginx.ssl.conf_*;
}

3 - Restart Nginx

After doing this, restart nginx:

service nginx restart

4 - Apply the Template

That’s it! Now you just need to choose the template by editing the domain in proxy model.

Hope this helps!

3 Likes