Proxying to a port with Nginx
I keep forgetting how to do exaclty the same thing: I often install things on a VPS and want a simple Nginx setup that proxies/forwards requests to that service’s port, as well as applies LetsEncrypt encryption. So here are all the steps.
Install and setup Nginx
Run these first to install Nginx and disable the default host:
1 | sudo apt-get update |
Then write a new file /etc/nginx/sites-available/service
, for example for a redirect on /
on port 80 to port 8080:
1 | server { |
And link it to enable it:
1 | ln -s /etc/nginx/sites-available/service /etc/nginx/sites-enabled/service |
At this point, https://example.com/
should redirect to the service running on port 8080.
Apply HTTPS with Certbot
First install Certbot:
1 | sudo apt-get install software-properties-common |
Then run it for Nginx:
1 | sudo certbot --nginx |
Be sure to pick your domain for automatic host file update, and choose whether to redirect HTTP to HTTPS (HSTS).
The new host file should like the following after being rewritten by Certbot:
1 | server { |
Alternative: use nginx-le
The same thing can be accomplished using Docker with the nginx-le image. You just have to write the nginx config files and mount them using the docker-compose.yml
.