Question

Nodejs app keeps showing Nginx welcome page.

Posted July 30, 2021 150 views
Linux BasicsNginxLinux Commands

I created a nodejs app on my Ubuntu server to run on socket.<mydomain>.com.
The nodejs app is running successfully on pm2 and I have already configured the nginx file but when I go to socket.<mydomain>.com it keeps showing me the nginx welcome page.

I’ve been searching online for the past 2 days and I’ve tried a lot of things but it still doesn’t work.

What else do you thing I can do about this issue please?

nodejs - server.js

import express from "express";
import cors from "cors";
import server from "http";
import { Server } from "socket.io";
import pkg from "dotenv";
pkg.config();

const app = express();
const serve = server.createServer(app);
const io = new Server(serve, {
  cors: {
    origin: process.env.CLIENT_SERVER,
    methods: ["GET", "POST"],
  },
});
const port = process.env.PORT || 5000;

app.get("/", (req, res) => {
  res.send("SOCKET SERVER!");
});
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));

io.on("connection", (socket) => {
  console.log("new socket connected");
  socket.on("join-room", (roomId, userId, name, mic, cam, screen = false) => {
    console.log(`new peer joined : ${name} `, userId);
    socket.join(roomId);
    socket.broadcast
      .to(roomId)
      .emit("user-connected", userId, name, mic, cam, screen);

    socket.on("disconnect", () => {
      console.log("disconnected", userId);
      socket.broadcast.to(roomId).emit("user-disconnected", userId);
    });
});

  socket.on("toggle media", (userId, roomId, type) => {
    console.log("toggle media for ", userId);
    socket.broadcast.to(roomId).emit("user toggled media", userId, type);
  });
});

// Server listen initilized
serve
  .listen(port, () => {
    console.log(`Listening on the port ${port}`);
  })
  .on("error", (e) => {
    console.error(e);
  });

nginx config - /etc/nginx/sites-available/socket.<mydomain>.com

server {
    if ($host = socket.<mydomain>.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen 80;
}


server{
    listen 443 ssl;
    access_log /var/log/nginx/socket.<mydomain>.com.log;

    location / {
        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_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_pass http://127.0.0.1:5000;
    }


}

I also created a symbolic link at /etc/nginx/sites-enabled/socket.<mydomain>.com

/etc/nginx/nginx.conf

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;

        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;

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

        ##
        # Gzip Settings
        ##

        gzip on;


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

When I run curl http://127.0.0.1:5000 and http://<myserverip>:5000 it shows the right output based on the nodejs app

  • My pm2 service is running successfully
  • I reloaded and restarted nginx after adding the configuration
  • Nginx service is running successfully
  • The subdomain dns is directed to my server ip, I confirmed that with dig https://socket.<mydomain>.com

After all of these, https://socket.<mydomain>.com still shows me the nginx welcome page on browser and with curl.

Please what do you think the problem is?

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
Submit an Answer
1 answer

Hi there,

It looks like that you do not have a server_name defined in your Nginx server block for port 443, you need to add the following line:

    server_name your_domain.com www.your_domain.com;

And then restart Nginx.

That way Nginx will know which server block to serve when you visit that specific domain name. At the moment it looks like Nginx is just service the default Nginx server block as your domain is not specified in your new Nginx server block.

Hope that this helps.
Regards,
Bobby

  • Hi Bobby. Thanks a lot for your reply.

    I added the server_name to the Nginx server block but it still doesn’t work. The page still shows the Nginx welcome page.

    Now, sudo nginx -t shows a warning that says conflicting server name “socket.my_domain.com” on 0.0.0.0:443, ignored.

    What else do you think I can do about this please?

    • Hi there @thedigerati,

      Did you add the server_name under the HTTPS server block? Also I could suggest making sure that the “socket.my_domain.com” domain is not specified in any of your other Nginx server blocks which could be causing the conflict.

      Let me know how it goes!
      Regards,
      Bobby

      • Thanks a lot @bobbyiliev.

        I checked the default file in the sites-available directory and I noticed that I’d initially added server blocks for the socket.my_domain.com while I was testing and forgot to remove it.

        Everything started working fine when I removed those lines.
        Thanks a lot.