Home > Back-end >  nodejs application work but nginx proxy doesn't catch it
nodejs application work but nginx proxy doesn't catch it

Time:11-05

I have got a Nodejs application running on my server so I wanted to use Nginx reverse proxy to have the API calls from a nice domain name instead of typing IP and port and I also generated a free let's encrypt certificate to use for that.

the problem is the reverse server doesn't work if I use an IP and port then it works if I use domain name it gives me an Nginx template test page anyways here are my configs :

Nginx config http { log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"';

access_log  /var/log/nginx/access.log  main;

sendfile            on;
tcp_nopush          on;
tcp_nodelay         on;
keepalive_timeout   65;
types_hash_max_size 2048;

include             /etc/nginx/mime.types;
#include /etc/nginx/sites-enabled/*;
default_type        application/octet-stream;

# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;

server {
    listen       80 default_server;
    listen       [::]:80 default_server;
    server_name  _;
    root         /usr/share/nginx/html;

    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;

    location / {
    }

    error_page 404 /404.html;
        location = /40x.html {
    }

    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }
}

my reverse proxy config and location

/etc/nginx/conf.d/api1.conf

Backend API server { server_name api.domain.com;

location / {
    proxy_pass http://localhost:26423/all; #whatever port your app runs on
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
}

}

enter image description here

whenever I try to see the API from a browser or postman I get this page

CodePudding user response:

It's not clear if you have a domain or not. If you have one, it should point to the server's public ipv4 ip and then create a nginx vhost that proxies to your nodejs app.

Assuming your public server ipv4 ip address is 1.2.3.4 and you have a domain like nodejsappdomain.com, then the nodejsappdomain.com should point to 1.2.3.4 and the nginx vhost will be something like this:

server { listen 80;

server_name nodejsappdomain.com www.nodejsappdomain.com;

location / {
    proxy_pass  http://127.0.0.1:nodejsapp_port;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
}

}

  • Related