'Configuring Nginx Reverse Proxy for Node.js app

I have an Express application, server listening on 3000 port. It is hosted on VPS, Ubuntu. When I open {VPS server ip}:3000 in browser, all is ok. But now I want to get access to the app at example.com, that's my domain name. I've installed Nginx, enabled UFW. I edited file /etc/nginx/nginx.conf, now its contents is

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
    worker_connections 768;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # SSL Settings
    ##

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
    server {
        listen 80;
        server_name example.com[-->my real domain here<--];
    
        location / {
            proxy_set_header   X-Forwarded-For $remote_addr;
            proxy_set_header   Host $http_host;
            proxy_pass         http://[-->my real server ip here<--]:3000;
        }
    }
}


#mail {
#   # See sample authentication script at:
#   # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
# 
#   # auth_http localhost/auth.php;
#   # pop3_capabilities "TOP" "USER";
#   # imap_capabilities "IMAP4rev1" "UIDPLUS";
# 
#   server {
#       listen     localhost:110;
#       protocol   pop3;
#       proxy      on;
#   }
# 
#   server {
#       listen     localhost:143;
#       protocol   imap;
#       proxy      on;
#   }
#}


nginx -t says that syntax is ok. Then systemctl restart nginx, it's active. When I open example.com in a browser, my app is inavailable. What should I check? What should I edit or restart or activate? Thx!



Solution 1:[1]

You need to create a file in sites-enabled folder of your nginx folder ( /etc/nginx/sites-enabled/ ).

And add proxy configuration of listening the request on post 80 ( or 443 if you have SSL certificates ) and proxypass it to http://127.0.0.1:3000/

And then try running nginx -t restart the nginx server.

Hope it helps you!!

Solution 2:[2]

So, probably my experience can be useful. The http section of my nginx.conf is

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # SSL Settings
    ##

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
    
}

Obviously, the lines

include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;

make all work. The single file in /sites-available folder has name 'myserver.config' and its full content is

#The Nginx server instance
server{
        listen 80;
        server_name example.com;
        location / {
        proxy_pass http://myIP:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;       
    }
}

I deleted all symbolic links from /sites-enabled which I created during the process of clarifying and created a new one with the command sudo ln -s /etc/nginx/sites-available/myserver.config /etc/nginx/sites-enabled/

That works. Here's not anything new but it confirms that the existing instructions are really workable.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 sachin
Solution 2 Boris