如何配置Kubernetes中Nginx前端以实现多路径路由转发
Got it, let's walk through exactly how to set up an Nginx reverse proxy in Kubernetes that routes requests to your app1 and app2 services via /app1 and /app2 paths. I'll break this down into actionable steps with concrete examples.
First, you'll need a custom Nginx config that maps each path to the corresponding backend service. Create a file named nginx.conf with this content:
events {} http { server { listen 80; server_name localhost; # Route /app1 requests to app1 service location /app1/ { # Rewrite the path to strip /app1/ so the backend gets the root path rewrite ^/app1/(.*)$ /$1 break; proxy_pass http://app1:80/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } # Route /app2 requests to app2 service location /app2/ { rewrite ^/app2/(.*)$ /$1 break; proxy_pass http://app2:80/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } # Optional: Default route if someone hits the root location / { return 200 "Welcome to the Nginx Proxy!\nUse /app1 or /app2 to access services.\n"; } } }
What's happening here?
rewrite ^/app1/(.*)$ /$1 break: Strips the/app1/prefix from the request URL before sending it to the backend. This is crucial if yourapp1service expects requests at its root (/) instead of/app1/.proxy_pass http://app1:80/: Points to your Kubernetes service namedapp1(assuming it's listening on port 80). In Kubernetes, services in the same namespace are resolvable via their name directly.- The
proxy_set_headerlines ensure the backend gets proper client and host information.
Next, you need to package this config into an Nginx image. Create a Dockerfile:
FROM nginx:alpine COPY nginx.conf /etc/nginx/nginx.conf
Then build and push the image to a container registry (like Docker Hub, or your private registry):
# Replace <your-registry> and <image-name> with your details docker build -t <your-registry>/nginx-multipath-proxy:v1 . docker push <your-registry>/nginx-multipath-proxy:v1
Now deploy this custom Nginx proxy to Kubernetes. Create a file named nginx-proxy.yaml:
apiVersion: apps/v1 kind: Deployment metadata: name: nginx-proxy spec: replicas: 2 selector: matchLabels: app: nginx-proxy template: metadata: labels: app: nginx-proxy spec: containers: - name: nginx-proxy image: <your-registry>/nginx-multipath-proxy:v1 ports: - containerPort: 80 --- apiVersion: v1 kind: Service metadata: name: nginx-proxy-service spec: type: NodePort # Use LoadBalancer if you're on a cloud provider with external IP support selector: app: nginx-proxy ports: - protocol: TCP port: 80 targetPort: 80 nodePort: 30080 # Optional: Specify a port if needed
Apply this to your cluster:
kubectl apply -f nginx-proxy.yaml
- Make sure your
app1andapp2services are running in the same namespace as the Nginx proxy. If they're in a different namespace, update theproxy_passURLs to use the full service DNS name, e.g.,http://app1.other-namespace.svc.cluster.local:80/. - Test the routing: If using NodePort, access
http://<your-node-ip>:30080/app1andhttp://<your-node-ip>:30080/app2to confirm they reach the right services. - If your backend services don't need the path stripped (e.g., they're already configured to handle
/app1), you can remove therewriteline and adjustproxy_passtohttp://app1:80(without the trailing slash).
内容的提问来源于stack exchange,提问作者Valentin

