Putting Nginx in front of a Node.js app lets one web server handle ports 80 and 443, HTTPS and static files while your app runs on a private port. This guide shows how to set up Nginx on Windows, macOS and Linux to host a Node.js app, with a check after every step so you know it worked.
Quick answer: Install Nginx, add a server block whose location / forwards requests to your app with proxy_pass http://127.0.0.1:3000; (use your app's port), then test the config with nginx -t and reload Nginx.
- Start your Node.js app on a local port such as 3000 and check it with curl.
- Install Nginx: the zip from nginx.org on Windows,
brew install nginxon macOS,sudo apt install nginxon Ubuntu. - Add a server block with
proxy_passto your app's port inside thehttp { }block. - Run
nginx -t, then reload Nginx. - Open your domain or IP and confirm you see your app, not the Nginx welcome page.
In this guide
- Before You Start
- How Nginx and Node.js Work Together
- Step 1: Start Your Node.js App on a Local Port
- Step 2: Install Nginx on Your Operating System
- Step 3: Add a Reverse Proxy Server Block for Your App
- Step 4: Test the Config and Reload Nginx
- Step 5: Make a Local PC Reachable from the Internet
- Nginx Commands You Will Use Most
- Keep Nginx and Your App Running After a Reboot
- Common Errors and Fixes
- FAQ
- Next Steps
- Related guides on IsItDev

Before You Start
- A computer or server running Windows, macOS, or Linux, with administrator or
sudorights. - Node.js installed and an app that starts on a port. Step 1 gives you a tiny test app if you do not have one yet.
- A terminal: Command Prompt on Windows, Terminal on macOS, or an SSH session on a Linux server.
- About 20 minutes. If the machine already serves a live site through Nginx, copy the config folder first (for example
sudo cp -r /etc/nginx /etc/nginx.bakon Linux), because a bad server block can take that site down.
How Nginx and Node.js Work Together
Nginx acts as a reverse proxy: it receives every request from the browser and passes it on to your Node.js app, then sends the app's answer back. Your app listens only on 127.0.0.1 (the machine itself), so visitors never reach it directly. Nginx handles the public ports, and later HTTPS.
Step 1: Start Your Node.js App on a Local Port
If you already have an app, start it and note its port. Otherwise, save this as server.js in an empty folder. It uses only Node's built-in http module:
const http = require('http');
const PORT = process.env.PORT || 3000;
http.createServer((req, res) => res.end('Hello from Node.js'))
.listen(PORT, '127.0.0.1', () => console.log(`Listening on http://127.0.0.1:${PORT}`));Start it from that folder in your terminal, then open a second terminal window and request the page:
node server.js
curl http://127.0.0.1:3000The first command prints Listening on http://127.0.0.1:3000 and keeps running. The curl command, run in the second window, should print Hello from Node.js. If it does not, fix the app before you touch Nginx.
Step 2: Install Nginx on Your Operating System
Windows
- Visit the official NGINX website and download the Windows version (a zip file).
- Extract the downloaded file to a directory, e.g., C:\nginx, so that
nginx.exesits directly in that folder. - Open Command Prompt, change to that directory with
cd C:\nginx, and run:start nginx - Check it: open
http://localhostin a browser. You should see the "Welcome to nginx!" page.tasklist /fi "imagename eq nginx.exe"also lists the running Nginx processes.
nginx.org describes the Windows build as a beta with known limitations. It is fine for local development; for a public site, run Nginx on Linux.
macOS (Using Homebrew)
- Open Terminal.
- Install Homebrew if you haven't: /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
- Install NGINX:
brew install nginx - Start NGINX:
brew services start nginx(Homebrew's default config listens on port 8080) - Check it: open
http://localhost:8080. You should see the "Welcome to nginx!" page.
Linux (Debian/Ubuntu)
- Open Terminal, or connect to your server over SSH.
- Update package lists:
sudo apt update - Install NGINX:
sudo apt install nginx - Start NGINX:
sudo systemctl start nginx - Check it:
systemctl status nginxshould showactive (running), andhttp://your-server-ipshould show the welcome page. - If the UFW firewall is on, open the web ports with
sudo ufw allow 'Nginx Full'(ports 80 and 443).
Step 3: Add a Reverse Proxy Server Block for Your App
- Navigate to the NGINX configuration directory. Typically, it's the conf folder inside C:\nginx on Windows, /opt/homebrew/etc/nginx on Apple Silicon Macs (/usr/local/etc/nginx on Intel Macs), and /etc/nginx on Linux.
- Open nginx.conf in a text editor. On Debian and Ubuntu you can instead create a separate file such as
/etc/nginx/sites-available/myapp, which keeps your site apart from the main config (Step 4 shows how to enable it). - Add a new server block inside the existing http { } block to reverse proxy requests to your Node.js app:

server {
listen 80;
server_name your_domain_or_IP;
location / {
proxy_pass http://127.0.0.1:YOUR_NODE_APP_PORT;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
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;
}
}Replace your_domain_or_IP with your domain name or IP address and YOUR_NODE_APP_PORT with the port your Node.js app runs on.
What the lines do: proxy_pass sends each request to your app. The Upgrade and Connection headers let WebSocket connections through. Host and the X-Forwarded-* headers tell your app the original domain, the visitor's IP and whether the request came over HTTPS.
Testing on your own computer? The default Nginx site already uses port 80 on Windows and Linux and port 8080 on macOS. To avoid a clash, change the first two lines to listen 8081; and server_name localhost;, then use http://localhost:8081 in Step 4.
Step 4: Test the Config and Reload Nginx
Save and close the file, then check it with nginx -t and apply it with nginx -s reload. Where you run those two commands depends on the system:
- Windows: in Command Prompt, inside
C:\nginx, runnginx -tand thennginx -s reload. - macOS: in Terminal, run
nginx -tand thennginx -s reload. - Linux: both commands need
sudo. If you created a file insites-available, enable it first with a symlink, as below.
sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginxA good config makes nginx -t report that the syntax is ok and the test is successful. If it names a file and line number instead, fix that line and run the test again before you reload.
Now open your domain or IP (or http://localhost:8081 for a local test). You should see your app's response, such as Hello from Node.js, instead of the Nginx welcome page.
Step 5: Make a Local PC Reachable from the Internet
If you're running your Node.js app on a local PC and want it to be accessible from the internet, you have two options:
- Configure your router to forward incoming requests on port 80 to your PC (port forwarding). This exposes your home network and usually needs a static IP or dynamic DNS.
- Use services like ngrok or localtunnel to expose your local server through a tunnel, with no router changes. For a free option with a stable URL, see how to expose a local server with Cloudflare Tunnel.
Remember, exposing your local environment to the internet comes with security risks. Keep the tunnel or port open only while you need it, and never expose a database or admin panel this way.
Nginx Commands You Will Use Most
On Windows, run these from the C:\nginx folder. On Linux, put sudo in front of each one.
- Start NGINX: nginx (or start nginx on Windows)
- Stop NGINX: nginx -s stop
- Reload NGINX Configuration: nginx -s reload
- Test NGINX Configuration: nginx -t
Keep Nginx and Your App Running After a Reboot
You do not need to restart your PC after installing or reconfiguring NGINX; nginx -s reload applies config changes. To have NGINX come back after a reboot, run sudo systemctl enable nginx on Linux. On macOS, brew services start nginx already takes care of it. On Windows, run start nginx again after each restart.
Your Node.js app needs the same treatment. node server.js stops when you close the terminal, so on a server use a process manager. Here is how to keep a Node.js app running with PM2 and a startup script.
Common Errors and Fixes
- 502 Bad Gateway: Nginx is running but cannot reach your app. The app has stopped, or the port in
proxy_passdoes not match the app's port. Run thecurl http://127.0.0.1:3000check from Step 1 again. - Nginx will not start and mentions
bind() to 0.0.0.0:80 failed: another program already uses that port (often IIS on Windows or Apache on Linux), or you lack permission to use it. Stop the other service or change thelistenport. - You still see "Welcome to nginx!": the default site is answering. Check that
server_namematches exactly what you type in the browser and that you reloaded Nginx. On Ubuntu you can remove the default site withsudo rm /etc/nginx/sites-enabled/default. - Commands fail on Windows: run them from the
C:\nginxfolder, where Nginx finds itsconfandlogsfolders.
FAQ
Do I need Nginx to run a Node.js app?
No, Node.js can listen on a port by itself. Nginx earns its place when you want HTTPS, several apps on one server behind ports 80 and 443, fast static files, or a buffer that protects the app from slow clients.
Which port should my Node.js app use behind Nginx?
Any free port above 1024 works; 3000 is the usual choice. Bind the app to 127.0.0.1 so only Nginx can reach it, and make sure the port in proxy_pass matches. Give each extra app its own port.
Does this config work with WebSockets and Socket.IO?
Yes for basic use, because the Upgrade and Connection headers are already set. Long-lived connections may also need a longer proxy_read_timeout. The guide to an Nginx reverse proxy for Node.js WebSockets covers timeouts and PM2 cluster mode.
How do I add HTTPS?
On a Linux server with a real domain, use Certbot with its Nginx plugin. It requests a free Let's Encrypt certificate and adds the HTTPS settings to your server block. A local PC behind a tunnel usually gets HTTPS from the tunnel service instead.
Next Steps
Your Node.js app now runs behind Nginx. For a public site, move it to a Linux server and add a domain and HTTPS: the guide to deploy Node.js on a VPS with Nginx, DNS and SSL walks through the whole setup.


You must be logged in to post a comment.