You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何配置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.

1. Core Nginx Configuration for Multi-Path Routing

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 your app1 service expects requests at its root (/) instead of /app1/.
  • proxy_pass http://app1:80/: Points to your Kubernetes service named app1 (assuming it's listening on port 80). In Kubernetes, services in the same namespace are resolvable via their name directly.
  • The proxy_set_header lines ensure the backend gets proper client and host information.
2. Build a Custom Nginx Image

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
3. Kubernetes Deployment & Service

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
4. Key Things to Verify
  • Make sure your app1 and app2 services are running in the same namespace as the Nginx proxy. If they're in a different namespace, update the proxy_pass URLs 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/app1 and http://<your-node-ip>:30080/app2 to 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 the rewrite line and adjust proxy_pass to http://app1:80 (without the trailing slash).

内容的提问来源于stack exchange,提问作者Valentin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 08:04:29