My Nginx and Node.js application is not serving my application to the internet. When I try to access http://helloworld.example.com
in my browser it cannot connecf.
Here is my helloworld.conf file:
upstream backend {
server localhost:8000;
keepalive 32;
}
server {
listen 80;
server_name helloworld.example.com;
location / {
client_max_body_size 50M;
proxy_set_header X-Real-IP $remote_addr;
proxy_http_version 1.1;
proxy_pass http://backend;
}
}
Here is my Node app:
var http = require('http');
var hostname = 'localhost';
var port = 8000;
var server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World!');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}`);
});
And heres my service file:
[Unit]
Description=Node.js Application
After=syslog.target network.target
[Service]
Type=simple
User=root
WorkingDirectory=/root
Environment=NODE_ENV=production
ExecStart=/usr/bin/node usr/bin/helloworld.js
Restart=always
[Install]
WantedBy=multi-user.target
The service file status shows active and so does the Nginx config file and the node js app run perfectly. Any help?
2
Answers
Try the following configuration, it’s from my
NGINX
server and works fineIt listens on port
80
oflocalhost
and for every request it receives, it forwards it tohttp://localhost:8000
.Not specifying anything except
proxy_pass
will work tooYou can learn more about the directives below
proxy_pass
from the docs hereTo connect the application to the internet you just have to change the
server_name
property in then config assuming that you domain points correctly to your server.Changing
server_name localhost
toserver_name helloworld.example.com
will make the application available on the internet.To expose your application to the Internet, to host it from home, you need to make changes to systems outside your local machine.
In a typical setup your local computer is not connected to the internet directly. Instead it goes via a router, which is directly connected to the internet.
To the internet you appear with the address of your router and the internet can address your router by it’s address. Normally your router "only" forwards your requests to the internet and accepts the responses. (The technical term here is NAT.) But it does not accept any incoming requests, because the router does not know how to handle the request. (Technically the router also blocks the request because it’s Firewall is configured reject request.)
To make your application available to the internet you will need to tell the router to forward request to your local machine. How this is done depends on your Router, but the keyword here is Port Forwarding.
To forward a port you need to specify the port and the protocol to forward, in case of http this is port 80 (from the
listen 80
) and protocol TCP (the protocol http uses under the hood). Most routers automatically open this port in the Firewall, but for others you have to explicitly configure it. You most likely also have to open the port 80 in the firewall of your local machine, so the forwarding request is not rejected by your local machine.If you have set up this forwarding other can now access your application via your routers (IP) address, for example
http://91.11.84.87
, but not via a domain name (yet).To get a domain name you typically need to buy/rent (register) a domain name from a domain registrar and "attach" your routers address to the domain. (The keyword being DNS.) Getting a domain name is a story of it’s own.
Typically your routers IP address changes regularly, so you will need to also update that address for your domain then! If you are in luck your registrar and router both support DynDNS: This way your router automatically updates the address with your registrar.
I hope this helps you into the right directions.