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
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?
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
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.
Sign up for Infrastructure as a Newsletter.
Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
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: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.
In addition to what has already been mentioned, here is also a quick video on how to deploy Node.js with PM2, Nginx as a reverse proxy, and Cloudflare for SSL termination:
Hope that this helps. Regards, Bobby