Minimal local nginx setup using Docker
If you have not installed Docker, see our guide at How to install docker and docker-compose on Ubuntu in 30 seconds
- Create your
nginxconfig file (my-nginx.conf). This is a template that reverse proxys TechOverflow:
my-nginx.conf
server {
listen 80 default_server;
listen [::]:80 default_server;
location / {
proxy_pass https://techoverflow.net;
proxy_http_version 1.1;
}
}- Start
nginxusingdocker:
run_nginx_docker.sh
docker run -it -p 80:80 --rm -v $(pwd)/my-nginx.conf:/etc/nginx/conf.d/default.conf nginx:latest- Go to http://localhost and see the result!
Explanation of the docker command:
docker run -it: Create a new docker container and run it in interactive mode (i.e. it will not run in the background, once you kill the command, nginx will exit)-p 80:80: Makes port 80 of thenginxserver (the standard HTTP port) available on the host’s port80. The first80is the host port whereas the second port80is the container’s port.--rm: Once the container is stopped, delete it!-v $(pwd)/my-nginx.conf:/etc/nginx/conf.d/default.conf: Map my-nginx.conf in the current directory ($(pwd)) to /etc/nginx/conf.d/default.conf on the container.nginx:latest: In the container run the official nginx image from DockerHub in thelatestversion.
Explanation of the nginx config file:
server { ... }: Everything inside this blog will belong together. You canlisten 80 default_server;Listen on port 80 (the standard HTTP port) and make this the default server, i.e. respond to any domain name that does not have any other server configured.listen [::]:80 default_server;Same as the previous line, but for IPv6.[::]means: Listen on all IPv6 addresses.location / { ... }: Everything inside this block is valid for any URL starting with/i.e. any URL at all. In clauses likelocation /app { ... }the content of the clause would be valid for URLs starting with/apponly, e.g.http://localhost/app/orhttp://localhost/app/dashboard.proxy_pass https://techoverflow.net;Redirect requests to the current location (/) to the serverhttps://techoverflow.netusing a reverse proxy.proxy_http_version 1.1;This sets the HTTP version that nginx uses to make the requests tohttps://techoverflow.net. This is not always neccessary but might increase compatibility.
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow