skip to Main Content

I am trying to install a new Nginx reverse proxy for a NodeJS application.
I’ve done it several times in the past and after hours of comparing the different configurations everything seems the same on both, but one works and the other one shows me the page:

Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required."

I am using an EC2 instance, I installed nginx with the following command:

sudo yum update -y
sudo yum install nginx

There are NO sites-available nor sites-enabled directories by default.
I configured the other server directly within conf.d (for testing purposes).
sudo vim /etc/nginx/conf.d/testing.api.example.com

I wrote the following configuration:

server {
        listen 80 default_server;
        listen [::]:80 default_server;
        server_name testing.api.example.com;

        location / {
                    proxy_pass http://127.0.0.1:8082;
                    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;

            }
}

I started nginx:

sudo nginx -t
sudo systemctl restart nginx

From the server I run a test to see if the server is running:

$ curl 127.0.0.1:8082
Hello Node!

The NodeJS server is running OK.

But when calling the port 80 for the reverse proxy, I’ve got the default "Welcome to nginx!" page.

It seems the nginx.conf file includes the conf.d folder:

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

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;
    keepalive_timeout   65;
    types_hash_max_size 4096;

    include             /etc/nginx/mime.types;
    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;
        listen       [::]:80;
        server_name  _;
        root         /usr/share/nginx/html;

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

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

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

# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2;
#        listen       [::]:443 ssl http2;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers PROFILE=SYSTEM;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        error_page 404 /404.html;
#        location = /404.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#        location = /50x.html {
#        }
#    }

}

I’ve tried several changes, but none are working…

Is there any other configuration missing?

Maybe the name_server is not correct?

I’ve tried:

server_name testing.api.example.com;
server_name 3.89.65.77;
#server_name 3.89.65.77; (commented)
server_name wach.quest;

2

Answers


  1. If you are seeing the "Welcome to nginx!" page instead of your NodeJS application, it means that Nginx is not correctly configured to proxy the requests to your application.

    To resolve this issue, you can follow these steps:

    1. Make sure your NodeJS application is running and listening on a specific port (e.g., 3000).

    2. Open your Nginx configuration file. The default location for the configuration file is usually /etc/nginx/nginx.conf or /etc/nginx/sites-available/default depending on your system.

    3. Inside the configuration file, locate the server block that corresponds to your domain or IP address. If it doesn’t exist, you can create a new server block.

    4. Within the server block, add the following configuration to define the reverse proxy:

    location / {
        proxy_pass http://localhost:3000;  # Replace with the actual address of your NodeJS application
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
    
    1. Save the configuration file and exit.

    2. Restart Nginx to apply the changes:

    sudo service nginx restart
    

    Make sure to replace http://localhost:3000 in the proxy_pass directive with the actual address where your NodeJS application is running. This could be a local IP address, a domain name, or a different port depending on your setup.

    After restarting Nginx, it should properly proxy the requests to your NodeJS application, and you should no longer see the "Welcome to nginx!" page.

    location / {
        proxy_pass http://localhost:3000;  # Replace with the actual address of your NodeJS application
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
    
    Login or Signup to reply.
  2. It seems the nginx.conf file includes the conf.d folder:

    If you look at the actual line in nginx.conf that includes the conf.d folder, you will see it only includes files in that folder that end in the .conf extension:

    include /etc/nginx/conf.d/*.conf;
    

    Your config file /etc/nginx/conf.d/testing.api.example.com does not have a .conf extension. You whould rename it to something like /etc/nginx/conf.d/testing.api.example.com.conf

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search