0

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!

2
  • Now I found a lot of instructions and it seems to me that there is no difference whether to create a special file for domain and include it into config file or write in the config file itself. However files should be in the /sites-available folder and I should create symbolic links to them in the /sites-enabled folder. Did all that, but still no effect yet... Commented May 13, 2022 at 23:20
  • Excuse me, now I know what was the trouble. I've edited the A-record, but not NS records. Now all is correct. You can follow any instruction that you found with request nginx reverse proxy setting for node js, they say truth! Commented May 16, 2022 at 12:57

2 Answers 2

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!!

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the answer. Where can I find an example of such a file /etc/nginx/sites-enabled/example.com with correct syntax? And should I create a link in the sites-available folder?
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.