Skip to content

Instantly share code, notes, and snippets.

@craftyc0der
Last active September 19, 2021 14:50
Show Gist options
  • Save craftyc0der/6492702e7b7f9fef90c3efafee029b75 to your computer and use it in GitHub Desktop.
Save craftyc0der/6492702e7b7f9fef90c3efafee029b75 to your computer and use it in GitHub Desktop.
Simple Reverse Proxy w/ NGINX

Setup Simple Reverse Proxy w/ NGINX

This example assumes you have Minikube installed with a docker repo server on it hosted at port 5000. See this gist for more details on that.

This example will launch a pod with 3 containers that will send the request to the correct container based on the path.

There are many files so open this gist up for all the information.

./run.sh
FROM nginx:alpine
COPY nginx.conf /etc/nginx/nginx.conf
RUN apk update && apk add bash
FROM nginx:alpine
COPY nginx-slave1.conf /etc/nginx/nginx.conf
COPY index1.html /www/data/index.html
RUN apk update && apk add bash
FROM nginx:alpine
COPY nginx-slave2.conf /etc/nginx/nginx.conf
COPY index2.html /www/data/index.html
RUN apk update && apk add bash
<!doctype html>
<html>
<head>
<title>Hello nginx</title>
<meta charset="utf-8" />
</head>
<body>
<h1>
Hello World 1!
</h1>
</body>
</html>
<!doctype html>
<html>
<head>
<title>Hello nginx</title>
<meta charset="utf-8" />
</head>
<body>
<h1>
Hello World 2!
</h1>
</body>
</html>
worker_processes 1;
events { worker_connections 1024; }
http {
server {
listen 8081;
root /www/data;
location / {
}
}
}
worker_processes 1;
events { worker_connections 1024; }
http {
server {
listen 8082;
root /www/data;
location / {
}
}
}
worker_processes 1;
events { worker_connections 1024; }
http {
server {
listen 8080;
location / {
proxy_pass http://localhost:8081/;
}
location /1 {
proxy_pass http://localhost:8081/;
}
location /2 {
proxy_pass http://localhost:8082/;
}
}
}
docker build -t localhost:5000/reverseproxy:latest -f Dockerfile .
docker build -t localhost:5000/reverseproxy:slave1 -f Dockerfile1 .
docker build -t localhost:5000/reverseproxy:slave2 -f Dockerfile2 .
docker push localhost:5000/reverseproxy:latest
docker push localhost:5000/reverseproxy:slave1
docker push localhost:5000/reverseproxy:slave2
kubectl delete -f ./test.yaml
kubectl apply -f ./test.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: reverseproxy
image: localhost:5000/reverseproxy:latest
imagePullPolicy: Always
ports:
- containerPort: 8080
- name: nginx1
image: localhost:5000/reverseproxy:slave1
imagePullPolicy: Always
ports:
- containerPort: 8081
- name: nginx2
image: localhost:5000/reverseproxy:slave2
imagePullPolicy: Always
ports:
- containerPort: 8082
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment