Setting up Nginx as a reverse proxy and web server - complete guide
Nginx is a powerful and versatile tool that can function both as a web server and a reverse proxy. Whether you host a website, load applications, or distribute traffic between servers: Nginx offers an efficient solution. In this guide, you will discover how to optimize Nginx for both roles – with practical configuration examples, security checks, and tips for Dutch developers and system administrators.
What is Nginx and why use it as a reverse proxy?
Nginx ("engine X") is a lightweight, high-performance web server often used for:
- Serving static content (HTML, CSS, JavaScript)
- Load balancing between backend servers
- SSL/TLS termination
- Reverse proxy functionality for applications such as Node.js, Python, or Java
As a reverse proxy, Nginx acts as an intermediary between clients and backend servers. This improves:
- Performance: Caching and compression
- Security: Hides internal server architecture
- Scalability: Easily add multiple backend servers
Installing Nginx (Ubuntu/Debian)
Install Nginx via the package manager:
sudo apt update && sudo apt install nginx -y
Check the version and status:
nginx -v
systemctl status nginx
Basic configuration as a web server
By default, Nginx serves files from /var/www/html. Adjust this in /etc/nginx/sites-available/default:
server {
listen 80;
server_name jesite.nl www.jesite.nl;
root /var/www/mijnsite;
index index.html;
}
Test the configuration and reload:
nginx -t && systemctl reload nginx
Setting up Nginx as a reverse proxy
A simple reverse proxy configuration for a Node.js app on port 3000:
server {
listen 80;
server_name api.jesite.nl;
location / {
proxy_pass http://localhost:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
Advanced proxy settings
- Caching: Reduce the load on backend servers
- Load balancing: Distribute traffic across multiple servers
- WebSockets: Requires additional headers
Security and optimization
Scan your configuration for vulnerabilities with Gixy:
gixy-next /etc/nginx/nginx.conf
Essential security tips:
- Always use SSL/TLS (e.g., via Let's Encrypt)
- Set strict permissions for configuration files
- Restrict HTTP methods (disable TRACE/DEBUG)
Troubleshooting common issues
502 Bad Gateway: Usually a backend server that is not responding. Check:
curl -I http://localhost:3000Performance issues: Optimize worker processes and buffer sizes:
worker_processes auto;
events {
worker_connections 1024;
}
Conclusion
Nginx is an ideal tool to function both as a web server and a reverse proxy. With the right configuration:
- Serve static content at lightning speed
- Connect securely to backend applications
- Scale easily under heavy load
Experiment with the examples, monitor performance, and do not forget to regularly run security audits with tools such as Gixy. This way, you get the most out of your Nginx installation.