3

I have tried all the solution on SO but no success. I want to use Nginx as a "Node.js" app reverse proxy. With my current configurations, I was able to make it work when connecting to it through the server IP but not when using its domain name.My configuration details pastebin.com/gMqpmDwj

http://Ipaddress:3000 works but http://example.com doesn't.

Here is the configuration of my Nginx proxy, stored in /etc/Nginx/conf.d/domain.conf.

server {
    listen 80;
    server_name domain_name;

    location / {
        proxy_set_header   X-Forwarded-For $remote_addr;
        proxy_set_header   Host $http_host;
        proxy_pass         http://ipaddress:3000;
    }
}

But when I try to access it works fine on ip:port but when on domain:port or without port it doesn't

scrowler
  • 24,273
  • 9
  • 60
  • 92
adamwood
  • 60
  • 1
  • 1
  • 10
  • Try setting `server_name` to your domain name. For example: `server_name example.com www.example.com`. On the other hand, is the server working on port 80 when you try to access using its IP? – guzmonne Jul 19 '19 at 17:53
  • I did try using server name as domain name but no success. Yes it is working on port 80. I am using CWP and its showing default page onf both port 80 and domain name. – adamwood Jul 19 '19 at 18:34
  • Its been more than 12 hours I am stuck on this issue.I tried all the combinations :( – adamwood Jul 19 '19 at 18:35
  • And you are sure that you have configured your DNS server correctly? Can you check if your domain name is resolving to the proper IP? – guzmonne Jul 19 '19 at 18:40
  • Yes I am sure. It was working properly when I was using apache but not on nginx. – adamwood Jul 19 '19 at 18:52
  • https://pastebin.com/gMqpmDwj my configuration details. – adamwood Jul 19 '19 at 18:58
  • Sorry, I have created a `minimum example` using NodeJS and NGINX and I can replicate the problem. Here is the GitHub repo: https://github.com/guzmonne/nodejs_nginx. Try to run this on your machine, and see if it helps you figure out what is wrong with your project. – guzmonne Jul 21 '19 at 01:04

2 Answers2

2

Try this configuration:

/etc/nginx/nginx.conf

user                nobody;
worker_processes    1;
pid                 /var/run/nginx.pid;

events {
    worker_connections  1024;
    multi_accept on;
}

http {
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 15;
    types_hash_max_size 2048;
    client_max_body_size 8M;
    server_tokens off;

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

    access_log off;
    error_log /var/log/nginx/error.log crit;

    gzip on;
    gzip_min_length 100;
    gzip_http_version 1.1;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    include /etc/nginx/cloudflare.inc;
    include /etc/nginx/conf.d/*.conf;
}

/etc/nginx/conf.d/domain.conf

upstream nodejs_app {
    server <ipaddress>:3000;
    keepalive 8;
}

server {
    listen 80;
    listen [::]:80;
    server_name <domain_name>;

    location / {
        # websocket support
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";

        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;

        proxy_pass http://nodejs_app/;
        proxy_redirect off;
    }
}
Qteb
  • 483
  • 4
  • 14
0

I solved my issue after following this link.I had multiple configuration files active that was causing problem. How to Configure Nginx Reverse Proxy for Nodejs on Centos

adamwood
  • 60
  • 1
  • 1
  • 10