Skip to content

Instantly share code, notes, and snippets.

@karakanb
Last active October 25, 2018 22:42
Show Gist options
  • Save karakanb/73f88c2799759cfb31acd444a0078656 to your computer and use it in GitHub Desktop.
Save karakanb/73f88c2799759cfb31acd444a0078656 to your computer and use it in GitHub Desktop.
How to create an Nginx proxy container using Docker

Minikube Proxy

For a project of mine, I needed a way to make my minikube cluster reachable over localhost:4567, which seems to have no easy solution with basic tools. The script here basically uses the provided nginx.conf file and proxies all the requests to your provided IP address. In my case, my minikube installation had the IP address 192.168.99.101, which means my nginx.conf file was like this:

server {
  listen 80;
  server_name localhost;
    location / {
      access_log off;
      proxy_pass http://192.168.99.101;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header Host $host;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

This approach can be used to proxy whatever you want to wherever you want.

# The port to reach your localhost proxy, default is 4567, reachable as localhost:4567
LOCAL_PORT_TO_MAP=4567
# The name of the Nginx proxy container.
CONTAINER_NAME=nginx-proxy
# Runs an `nginx:alpine` container to proxy the requests.
# It overrides the default config by the config you provide, which is responsible for proxying everything to the address you provide.
docker run --name $CONTAINER_NAME \
-p $LOCAL_PORT_TO_MAP:80 \
-v ~/path/to/nginx.conf:/etc/nginx/conf.d/default.conf \
-d nginx:alpine;
server {
listen 80;
server_name localhost;
location / {
access_log off;
proxy_pass <IP_TO_PROXY_REQUESTS>
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment