Setting up Nginx as a reverse proxy and web server - Complete guide
Nginx is one of the most widely used web servers in the world, known for its speed, stability, and versatility. Whether you want to host a website, place an API behind a reverse proxy, or manage multiple services through a single server – Nginx provides the solution. In this article, we explain step by step how to set up Nginx as a reverse proxy and as a web server, including practical tips for optimal performance.
What is Nginx and why choose a reverse proxy?
Nginx is an open-source web server that also functions as a reverse proxy, load balancer, and caching tool. It is lightweight and can handle thousands of simultaneous connections, making it ideal for modern web applications.
A reverse proxy acts as an intermediary between clients and backend servers. This offers advantages such as:
- Improved security (hides backend servers)
- Load balancing (distributes traffic across multiple servers)
- Caching (speeds up load times)
- SSL termination (simplifies certificate management)
Installing Nginx on your server
Before we can use Nginx as a reverse proxy or web server, it must be installed. Below are the commands for popular Linux distributions:
Ubuntu/Debian
sudo apt update
sudo apt install nginx
CentOS/RHEL sudo yum install epel-release sudo yum install nginx
Start and enable Nginx sudo systemctl start nginx sudo systemctl enable nginx
Check if Nginx is running by opening your server's IP in a browser. You should see the welcome page.
Configuring Nginx as a web server
By default, Nginx serves files from /var/www/html. Let's set up a basic website:
- Create a directory for your site:
sudo mkdir -p /var/www/mijnsite.nl/html - Set the correct permissions:
sudo chown -R $USER:$USER /var/www/mijnsite.nl/html - Create a test
index.htmlfile - Create a server block (virtual host) in
/etc/nginx/sites-available/mijnsite.nl:server { listen 80; server_name mijnsite.nl www.mijnsite.nl; root /var/www/mijnsite.nl/html; index index.html; location / { try_files $uri $uri/ =404; } } - Activate the configuration:
sudo ln -s /etc/nginx/sites-available/mijnsite.nl /etc/nginx/sites-enabled/ - Test the configuration and reload Nginx:
sudo nginx -t sudo systemctl reload nginx
Setting up Nginx as a reverse proxy
Suppose you have a Node.js, Python, or other application running on port 3000. We can configure Nginx as a reverse proxy to route traffic to this application:
server {
listen 80;
server_name api.mijnsite.nl;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
Important proxy directives:
proxy_pass: Defines the backend serverproxy_set_header: Preserves important headersproxy_http_version: Supports HTTP/1.1 for better performance
Advanced reverse proxy configurations
Load balancing
With Nginx you can distribute traffic across multiple backend servers:
upstream backend_servers {
server backend1.mijnsite.nl:3000;
server backend2.mijnsite.nl:3000;
server backend3.mijnsite.nl:3000;
}
server { location / { proxy_pass http://backend_servers; } }
SSL termination
SSL certificates can be centrally managed at Nginx:
server {
listen 443 ssl;
server_name api.mijnsite.nl;
ssl_certificate /path/to/certificate.pem;
ssl_certificate_key /path/to/key.key;
location / {
proxy_pass http://localhost:3000;
... other proxy settings
}
}
Practical tips for Nginx optimization
- Worker processes: Set
worker_processes auto;innginx.conffor optimal CPU utilization - Keepalive: Increase
keepalive_timeoutfor long-lived HTTP connections - Gzip compression: Enable gzip to save bandwidth
- Caching: Configure caching for static files
- Logging: Limit logging for better performance (
access_log off;for static files)
Common problems and solutions
502 Bad Gateway: This usually means Nginx cannot reach the backend server. Check that your application is running and that the port in proxy_pass is correct.
Permission denied: Ensure the Nginx user (www-data or nginx) has read permissions on your website files.
Configuration errors: Always use sudo nginx -t to test your configuration before reloading Nginx.
Conclusion
Nginx is a powerful tool that can function both as a web server and as a reverse proxy. With the right configuration, you can:
- Host websites and applications efficiently
- Distribute traffic optimally across backend servers
- Improve security and performance
- Simplify complex architectures
Start with the basic configurations in this article and gradually expand to more advanced setups. Don't forget to always test changes with nginx -t before activating them.