Setting up a reverse proxy with Nginx and Let's Encrypt allows you to host multiple websites securely on a single server. This setup is ideal for managing several sites efficiently while ensuring HTTPS encryption for security.
Prerequisites
- A server with a Linux operating system (Ubuntu, Debian, etc.)
- Root or sudo access to the server
- Registered domain names for each website
- Basic knowledge of command line operations
Installing Nginx and Certbot
Begin by updating your package list and installing Nginx and Certbot, which helps obtain SSL certificates from Let's Encrypt.
sudo apt update
sudo apt install nginx certbot python3-certbot-nginx
Configuring Nginx as a Reverse Proxy
Create a new server block for each site you want to host. Here is an example configuration for example.com:
sudo nano /etc/nginx/sites-available/example.com
Insert the following configuration, replacing example.com and backend_server with your domain and backend server address:
server {
listen 80;
server_name example.com www.example.com;
location / {
proxy_pass http://backend_server;
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;
}
}
Enable the site by creating a symbolic link:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
Test the Nginx configuration and restart the service:
sudo nginx -t
sudo systemctl restart nginx
Obtaining SSL Certificates with Let's Encrypt
Use Certbot to automatically obtain and install SSL certificates for your domain:
sudo certbot --nginx -d example.com -d www.example.com
Follow the prompts to complete the SSL setup. Certbot will automatically configure Nginx to redirect HTTP to HTTPS.
Repeat for Additional Sites
To add more sites, repeat the process: create a new server block, enable it, and run Certbot for that domain. This allows you to host multiple sites securely with a reverse proxy.
Conclusion
Using Nginx as a reverse proxy combined with Let's Encrypt SSL certificates provides a robust and secure way to manage multiple websites on a single server. Regularly update your certificates and configuration to maintain security and performance.