nginx reverse proxy config: example and generator
Send visitors from port 80 to an app running on port 3000.
An nginx reverse proxy sits in front of your app and passes visitors from port 80 to the port your app listens on, such as 3000. Fill in your domain and port below to get a ready config, then follow the steps to turn it on.
Create your config file
Type your own values. The file below updates as you type, and nothing you type leaves your browser.
server {
listen 80;
server_name example.com;
client_max_body_size 10m;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}How this setup works
Step-by-step setup
- Start your app so it listens on 127.0.0.1:3000.
- Save the config above as /etc/nginx/conf.d/myapp.conf.
- Test it: sudo nginx -t.
- Apply it: sudo systemctl reload nginx.
- Add HTTPS: sudo certbot --nginx -d example.com.
What the important lines mean
- proxy_pass sends traffic to 127.0.0.1:3000.
- Host and X-Forwarded-Proto tell your app the original address and protocol.
- The Upgrade and Connection lines let WebSockets work.
Common errors and how to fix them
- 502 Bad Gateway
- nginx cannot reach your app. Check the app is running on port 3000.
- bind() to 0.0.0.0:80 failed
- Another program uses port 80. Find it with sudo ss -tlnp | grep :80.
- nginx -t says unknown directive
- A typo or missing semicolon. The error shows the file and line number.
Frequently asked questions
- What is a reverse proxy in nginx?
- It is a server that receives requests from browsers and forwards them to another server, usually your app on the same machine. Visitors see only nginx, while your app stays on a private port.
- How do I fix 502 Bad Gateway in nginx?
- A 502 means nginx cannot reach your app. Check that the app is running, that the port in proxy_pass matches the port it listens on, and read /var/log/nginx/error.log for the exact reason.
- How do I add HTTPS to this config?
- Run sudo certbot --nginx -d your-domain after the config works on port 80. Certbot adds the certificate and the redirect for you.
Free tools that help
- Common ports listSearch a list of common TCP and UDP port numbers: SSH 22, HTTP 80, HTTPS 443, MySQL 3306, PostgreSQL 5432 and more, with dev server ports.
Fix a problem with this setup
- nginx 502 Bad GatewayFix nginx 502 Bad Gateway with Node, PHP-FPM or any proxy: read the error log, check the app is running and the port or socket matches.
- CORS error on localhostFix "blocked by CORS policy" when your frontend on localhost calls an API: add the right headers on the server, or use a dev proxy.
- Node EADDRINUSEFix EADDRINUSE in Node, Express, Next.js and Vite: find the program using the port, stop it, or start on another port.
- Port already in useFind which program holds a port on Windows, macOS or Linux, then stop it or use another port.
More server configs
- nginx PHP-FPM configRun a PHP site (WordPress, Laravel or plain PHP) on nginx with PHP-FPM.
- Caddyfile reverse proxySend visitors to an app on port 3000 and get HTTPS certificates automatically.
- crontab examplesRun a script every minute, hour, day or week.
- systemd service fileRun your app in the background and start it automatically at boot.
- Apache virtual hostHost a website from a folder with Apache, with or without .htaccess.
- logrotate configKeep log files from filling your disk: rotate, compress and delete old logs on a schedule.