nginx - Kubernetes 中来自 ConfigMap 的自定义 nginx.conf

标签 nginx kubernetes config configmap

我在家庭实验室中设置了 Kubernetes,并且能够从部署中运行 nginx 的普通实现。
下一步是有一个自定义的 nginx.conf 文件用于 nginx 的配置。为此,我使用了 ConfigMap。
当我这样做时,当我导航到 http://192.168.1.10:30008 时,我不再收到 nginx 索引页面。 (运行 nginx 服务器的节点的本地 IP 地址)。如果我尝试使用 ConfigMap,我会收到 nginx 404 页面/消息。
我无法看到我在这里做错了什么。任何方向将不胜感激。
nginx-deploy.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-conf
data:
  nginx.conf: |
    user nginx;
    worker_processes  1;
    events {
      worker_connections  10240;
    }
    http {
      server {
          listen       80;
          server_name  localhost;
          location / {
            root   html;
            index  index.html index.htm;
        }
      }
    }

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 1
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80
        volumeMounts:
            - name: nginx-conf
              mountPath: /etc/nginx/nginx.conf
              subPath: nginx.conf
              readOnly: true
      volumes:
      - name: nginx-conf
        configMap:
          name: nginx-conf
          items:
            - key: nginx.conf
              path: nginx.conf

---
apiVersion: v1
kind: Service
metadata:
  name: nginx
spec:
  type: NodePort
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
    nodePort: 30008
  selector:
    app: nginx 

最佳答案

没什么复杂的,就是nginx.conf中的根目录没有正确定义。
使用 kubectl logs <<podname>> -n <<namespace>> 检查日志给出了为什么 404 error正在针对特定请求发生。xxx.xxx.xxx.xxx - - [02/Oct/2020:22:26:57 +0000] "GET / HTTP/1.1" 404 153 "-" "curl/7.58.0" 2020/10/02 22:26:57 [error] 28#28: *1 "/etc/nginx/html/index.html" is not found (2: No such file or directory), client: xxx.xxx.xxx.xxx, server: localhost, request: "GET / HTTP/1.1", host: "xxx.xxx.xxx.xxx"这是因为location在您的 configmap 内以 root 身份引用错误的目录 root html .
将位置更改为具有 index.html 的目录将解决问题。这是 root /usr/share/nginx/html 的工作配置映射.但是,这可以根据需要进行操作,但我们需要确保目录中存在文件。


apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-conf
data:
  nginx.conf: |
    user nginx;
    worker_processes  1;
    events {
      worker_connections  10240;
    }
    http {
      server {
          listen       80;
          server_name  localhost;
          location / {
            root   /usr/share/nginx/html; #Change this line
            index  index.html index.htm;
        }
      }
    }

关于nginx - Kubernetes 中来自 ConfigMap 的自定义 nginx.conf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64178370/

相关文章:

nginx - 使用 RTMP 并通过 WebSocket 分发

docker - Elastic Beanstalk、带有 Docker 的 Nginx 配置

docker - 无法通过 minikube 从不安全的注册表中提取图像

kubernetes - 如何为kubernetes集群创建多个管理员用户?

perl - 如何从 Perl 访问 INI 文件?

django - 使用proxy_pass的nginx位置路径

nginx - 如何解决 Strapi 生产构建中的 CORS 策略问题?

kubernetes - minikube 转发到服务

Ruby Rack - 安装一个默认读取 index.html 的简单 Web 服务器

java - 在 Linux 2.6 系统上安装多个 Java 导致 'Command not found' 错误