Nginx SSL/HTTPS Not Working with Dynamic IP on Raspberry Pi and Fritzbox Router
Hosting a website on a Raspberry Pi with a dynamic IP provided by a Fritzbox router can be a challenging task. One of the main issues that users encounter is configuring Nginx to use SSL/HTTPS when the IP address is constantly changing. This article will provide a detailed explanation of the key concepts and steps required to set up Nginx with SSL/HTTPS on a Raspberry Pi with a dynamic IP.
Prerequisites
Before we start, make sure you have the following:
- A Raspberry Pi with Raspberry Pi OS (previously called Raspbian) installed.
- A Fritzbox router that supports dynamic DNS.
- A domain name registered with a domain registrar that allows you to configure dynamic DNS.
Configure Dynamic DNS on Fritzbox Router
The first step is to configure the dynamic DNS feature on your Fritzbox router. Log in to your router's web interface and navigate to the dynamic DNS settings. Input your domain name and select the dynamic DNS provider offered by your router. Save the settings and allow some time for the router to update the DNS records.
Install Nginx and Certbot on Raspberry Pi
Next, install Nginx and Certbot on your Raspberry Pi. Open a terminal session and run the following commands:
sudo apt update
sudo apt install nginx
sudo apt install certbot
Setup Nginx Virtual Host with SSL
Create a new virtual host configuration file for your website. Replace example.com with your domain name.
sudo nano /etc/nginx/sites-available/example.com
Add the following content to the file:
server {
listen 80;
server\_name example.com;
return 301 https://$server\_name$request\_uri;
}
server {
listen 443 ssl;
server\_name example.com;
ssl\_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl\_certificate\_key /etc/letsencrypt/live/example.com/privkey.pem;
location / {
proxy\_pass http://localhost:8080;
}
}Save and exit the file. Create a symbolic link to enable the virtual host and restart Nginx.
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo systemctl restart nginx
Obtain SSL Certificate with Certbot
Use Certbot to obtain a free SSL certificate for your website. Replace example.com with your domain name.
sudo certbot certonly --webroot -w /var/www/html -d example.com
Set up automatic certificate renewal using a cron job:
sudo crontab -e
Add the following line to the file:
0 0 * * * certbot renew --quiet
Configure Dynamic IP with Nginx
Up to this point, we have a standard Nginx configuration, but we still need to handle the dynamic IP. To accomplish this, use a service such as DynDNS or FreeDNS to map your domain name to your dynamic IP. Follow the instructions provided by the service to set up an account and configure your domain name. Then, install the service's client on your Raspberry Pi, and set it up to update the dynamic DNS records.
Edit the Nginx configuration file and replace the server\_name directive with the following:
server\_name example.com