如何在Kubernetes中使用nginx作为IIS的边车容器?

xe55xuns  于 2023-03-01  发布在  Kubernetes
关注(0)|答案(1)|浏览(128)

我在一个Kubernetes pod中同时使用nginx和IIS服务器时遇到了一个奇怪的问题。这似乎是nginx. conf的问题。如果我绕过nginx直接进入IIS,我会看到标准登录页面-x1c 0d1x
但是,当我尝试通过反向代理时,我看到了以下部分结果-

以下是这些文件:
nginx.conf:

events {
  worker_connections  4096;  ## Default: 1024
}

http{
    server {
       listen 81;
       #Using variable to prevent nginx from checking hostname at startup, which leads to a container failure / restart loop, due to nginx starting faster than IIS server. 
       set $target "http://127.0.0.1:80/"; 
       location / { 
          proxy_pass $target; 
       }
     }
}

deployment.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    ...
  name: ...
spec:
  replicas: 1
  selector:
    matchLabels:
      pod: ...
  template:
    metadata:
      labels:
        pod: ...
      name: ...
    spec:
      containers:
        - image: claudiubelu/nginx:1.15-1-windows-amd64-1809
          name: nginx-reverse-proxy
          volumeMounts:
            - mountPath: "C:/usr/share/nginx/conf" 
              name: nginx-conf
          imagePullPolicy: Always
        - image: some-repo/proprietary-server-including-iis
          name: ...
          imagePullPolicy: Always
      nodeSelector:
        kubernetes.io/os: windows
      imagePullSecrets:
        - name: secret1
      volumes:
        - name: nginx-conf
          persistentVolumeClaim:
            claimName: pvc-nginx

从一个卷Mapnginx.conf文件只是快速测试不同配置的一种方便方法,新配置可以使用kubectl cp ./nginx/conf nginx-busybox-pod:/mnt/nginx/交换进来。
Busybox盒(用于访问PVC):

apiVersion: v1
kind: Pod
metadata:
  name: nginx-busybox-pod
  namespace: default
spec:
  containers:
    - image: busybox
      command:
        - sleep
        - "360000"
      imagePullPolicy: Always
      name: busybox
      volumeMounts:
       - name: nginx-conf
         mountPath: "/mnt/nginx/conf"
  restartPolicy: Always
  volumes:
    - name: nginx-conf
      persistentVolumeClaim:
        claimName: pvc-nginx
  nodeSelector:
    kubernetes.io/os: linux

最后是PVC:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc-nginx
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 100Mi
  storageClassName: azurefile

知道为什么吗?

ybzsozfc

ybzsozfc1#

经过一些测试,下面是一个可以工作的nginx.conf -

http{  
    server {  
       listen 81;  
       set $target "http://127.0.0.1:80"; 
       location / { 
          proxy_pass $target; 
          proxy_set_header Host $host;
       }
     }
}
  • 新指令-proxy_set_header Host $host;
  • 已从proxy_pass指令使用的target变量中删除尾部斜杠。
  • (特定于我的应用程序)使用$host:$server_port代替$host可以更好地访问服务器上的其他端点。这是由于应用服务器将传入请求重定向到不同的URI,在此过程中丢失了代理的端口(81)。

相关问题