Nginx as a reverse proxy: exposing your services on the internet
What is a reverse proxy?
A reverse proxy is a server that is placed between the internet and your applications. It receives all incoming requests and redirects them to the correct service based on the URL.
Without reverse proxy, each service must be exposed directly on the internet with its own port:
mydomain.com:3000for the frontendmydomain.com:8000for the backendmydomain.com:5432for the database
This is not only unattractive but above all dangerous. With a reverse proxy, a single entry point on ports 80 and 443 handles all traffic.
Why Nginx?
Nginx is one of the most used web servers in the world. It is light, efficient and can play several roles:
- Static web server
- Reverse proxy
- Load balancer
- SSL termination
In our case, we use it as a reverse proxy to expose our Docker containers on the internet.
Installation
sudo apt update && sudo apt install -y nginx
sudo systemctl start nginx
sudo systemctl enable nginx
Check that Nginx is running:
sudo systemctl status nginx
Basic configuration
Nginx configuration files are in /etc/nginx/. The structure is as follows:
/etc/nginx/nginx.conf: global configuration/etc/nginx/sites-available/: configurations of available sites/etc/nginx/sites-enabled/: symbolic links to active sites
To create a new configuration:
sudo nano /etc/nginx/sites-available/myapp
Configure the reverse proxy
server {
listen 80;
server_name mydomain.com www.mydomain.com;
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;
}
location /api/ {
proxy_pass http://localhost:8000/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
Explanations:
listen 80: Nginx listens on port 80server_name: the domain for which this configuration applieslocation /: any request to the root is redirected to the frontend on port 3000location /api/: any request to/api/is redirected to the backend on port 8000proxy_set_header: transmits the original HTTP headers to the target service
Activate configuration:
sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
nginx -t checks the configuration syntax before applying it. Never reload Nginx without this check.
Block direct IP access
Without this measure, anyone can access your application by directly typing the server IP into their browser, bypassing your domain name and potentially your SSL.
server {
listen 80 default_server;
server_name _;
return 444;
}
Code 444 is specific to Nginx. It closes the connection without sending a response. This block must be placed before the main configuration.
Hide technical information
By default, Nginx exposes its version in HTTP headers. An attacker can use this information to target known vulnerabilities of this version.
In /etc/nginx/nginx.conf, in the http {} block:
server_tokensoff;
```## Add security headers
```nginx
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;
proxy_hide_header X-Powered-By;
add_header X-Content-Type-Options nosniff;
add_header X-Frame-Options DENY;
add_header X-XSS-Protection "1; mode=block";
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
add_header Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline'; img-src 'self' data: https:; font-src 'self'; connect-src 'self' https://mydomain.com;" always;
}
proxy_hide_header X-Powered-By: hides the framework used by the backendX-Content-Type-Options nosniff: prevents the browser from guessing the MIME type of a responseX-Frame-Options DENY: prevents integration of the site into an iframe, protects against clickjackingX-XSS-Protection: activates browser XSS protectionStrict-Transport-Security: forces the browser to use HTTPS for 1 year, even if the user types HTTP.includeSubDomainsapplies the rule also to subdomainsContent-Security-Policy: defines authorized content sources.default-src 'self'only allows resources from the same domain. Exceptions forunsafe-inlineandunsafe-evalare required for Next.js
Limit the number of requests
Rate limiting protects against DDoS and brute force attacks by limiting the number of requests per IP.
In /etc/nginx/nginx.conf:
http {
limit_req_zone $binary_remote_addr zone=general:10m rate=10r/s;
limit_req_zone $binary_remote_addr zone=api:10m rate=5r/s;
}
In the site configuration:
location / {
limit_req zone=general burst=20 nodelay;
proxy_pass http://localhost:3000;
}
location /api/ {
limit_req zone=api burst=10 nodelay;
proxy_pass http://localhost:8000/;
}
zone=general:10m: 10MB zone in memory to store counters by IPrate=10r/s: maximum 10 requests per secondburst=20: authorizes a burst of 20 requests before applying rate limitingnodelay: immediately rejects requests that exceed the limit
Configure SSL with Certbot
Certbot is a tool that automates obtaining and renewing free SSL certificates through Let's Encrypt.
sudo apt install -y certbot python3-certbot-nginx
sudo certbot --nginx -d mydomain.com -d www.mydomain.com
Certbot automatically modifies the Nginx configuration to enable HTTPS and configures automatic certificate renewal.
Check auto-renewal:
sudo certbot renew --dry-run
The final configuration
After all these steps, here is the complete configuration:
server {
listen 80 default_server;
server_name _;
return 444;
}
server {
listen 80;
server_name mydomain.com www.mydomain.com;location / {
limit_req zone=general burst=20 nodelay;
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;
proxy_hide_header X-Powered-By;
add_header X-Content-Type-Options nosniff;
add_header X-Frame-Options DENY;
add_header X-XSS-Protection "1; mode=block";
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
add_header Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline'; img-src 'self' data: https:; font-src 'self'; connect-src 'self' https://mydomain.com;" always;
}
location /api/ {
limit_req zone=api burst=10 nodelay;
proxy_pass http://localhost:8000/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_hide_header X-Powered-By;
}
}
Check the headers in place
Once the configuration has been reloaded, you can check that all the headers are present:
curl -I https://mydomain.com
You should see in the response:
X-Content-Type-Options: nosniff X-Frame-Options: DENY X-XSS-Protection: 1; mode=block Strict-Transport-Security: max-age=31536000; includeSubDomains Content-Security-Policy: default-src 'self'; ...
Conclusion
Nginx as a reverse proxy centralizes all incoming traffic, secures access to services and simplifies SSL management. It is an essential component in any modern production infrastructure.
In the following article, we secure MongoDB to protect the application data.