Nginx、kubernetes pod 中的 FPM - css 未呈现

Nginx, FPM in kubernetes pod - css are not rendered

我是 运行 FPM 和 nginx 作为一个 pod 中的两个容器。我的应用程序正在运行,我可以访问它,但浏览器不呈现 CSS 文件。控制台中没有错误。 我的部署文件:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: test
  labels:
    app: test
spec:
  replicas: 1
  selector:
    matchLabels:
      app: test
  template:
    metadata:
      labels:
        app: test
    spec:
      volumes:
        - name: shared-files
          emptyDir: {}
    
        - name: nginx-config-volume
          configMap:
            name: test
      containers:
        - image: test-php
          name: app
          ports:
            - containerPort: 9000
              protocol: TCP
          volumeMounts:
            - name: shared-files
              mountPath: /var/appfiles
          lifecycle:
            postStart:
              exec:
                command: ['sh', '-c', 'cp -r /var/www/* /var/appfiles']
        
        - image: nginx
          name: nginx
          ports:
            - containerPort: 80
              protocol: TCP
          volumeMounts:
            - name: shared-files
              mountPath: /var/appfiles
            - name: nginx-config-volume
              mountPath: /etc/nginx/nginx.conf
              subPath: nginx.conf

Nginx 配置:

events {
}
http {
    server {
            listen       80;
            root   /var/appfiles/;
            index index.php index.html index.htm;
   

            # Logs
            access_log /var/log/nginx/tcc-webapp-access.log;
            error_log /var/log/nginx/tcc-webapp-error.log;

            location / {
                # try_files $uri $uri/ =404;
                # try_files $uri $uri/ /index.php?q=$uri&$args;
                    try_files $uri $uri/ /index.php?$query_string;
            }


            location ~ \.php$ {
                    fastcgi_split_path_info ^(.+\.php)(/.+)$;
                    fastcgi_pass 127.0.0.1:9000;
                    fastcgi_index index.php;
                    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                    include fastcgi_params;
            }
    }
}

我可以在浏览器中打开页面,我可以看到所有组件、链接、按钮等,但页面未呈现,看起来 css 未加载。

为了解决您的问题,请将 configMap 更改为 nginx-configmap 的确切名称,并且在您的 configMap nginx 配置文件中可以作为以下:

apiVersion: v1
kind: ConfigMap
metadata:
  name: "nginx-configmap"
data:
  nginx.conf: |
    server {
        listen 80;
        server_name _;
        charset utf-8;
        root  /var/appfiles/;

        access_log /var/log/nginx/tcc-webapp-access.log;
        error_log /var/log/nginx/tcc-webapp-error.log;

        location / {
            index index.php;
            try_files $uri $uri/ /index.php?$query_string;
        }

        location ~ \.php$ {
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
    }

您会发现 the medium article 对您有用。