【问题标题】:Nginx, FPM in kubernetes pod - css are not renderedNginx,kubernetes pod中的FPM - css未呈现
【发布时间】:2021-11-25 19:51:49
【问题描述】:

我在一个 pod 中将 FPM 和 nginx 作为两个容器运行。我的应用程序正在运行,我可以访问它,但浏览器不呈现 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 没有加载。

【问题讨论】:

    标签: php nginx kubernetes fpm


    【解决方案1】:

    为了解决您的问题,请将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对你有用。

    【讨论】:

      猜你喜欢
      • 2018-09-04
      • 1970-01-01
      • 2019-06-14
      • 2019-06-03
      • 2017-08-05
      • 2021-04-15
      • 1970-01-01
      • 1970-01-01
      • 2020-07-19
      相关资源
      最近更新 更多