Reverse proxy to your Raspberry Pi
host-from-home cloud raspberry-pi nginx reverse-proxyNow that we have a network set up, let’s create a reverse proxy to send traffic from the cloud server to a web server running on the pi.
Web server on the pi #
We can run a simple web server using nginx on the pi to serve static content. Let’s assume we have some static files in a /public
directory and a simple nginx config like this in default.conf
:
server {
listen 80;
server_name localhost;
root /usr/share/nginx/html;
location / {
index index.html;
}
}
We can start the docker container like this:
docker run \
--rm \
-v $(pwd)/public:/usr/share/nginx/html:ro \
-v $(pwd)/default.conf:/etc/nginx/conf.d/default.conf:ro \
-p 8080:80 \
—name origin-server \
-d \
nginx:latest
You should be able to point your browser directly at your pi now and see your static content at http://interwebs.local:8080
Reverse proxy in the cloud #
On the cloud server we will run another instance of nginx but set it up as a reverse proxy to our pi. Using the tin network we set up in the previous post, the pi should be accessible at 10.0.0.2
. Create a nginx config file that sets up the reverse proxy:
server {
listen 80;
server_name _;
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_pass http://10.0.0.2:8080;
}
}
Start the docker container like this:
docker run \
--rm \
-v $(pwd)/default.conf:/etc/nginx/conf.d/default.conf:ro \
-p 80:80 \
—name reverse-proxy \
-d \
nginx:latest
Add a firewall rule #
By default, the firewall will block access to port 80, but we can open that up using ufw-docker
to forward that traffic to our docker container:
sudo ufw-docker allow reverse-proxy
At this point you should be able to navigate to the ip address of your cloud server in a browser and have that traffic proxied to the web server running on your raspberry pi.