MIME issue of Nginx on Kubernetes
nginx usually trigger error that not right mime types, such regard css as plain text
it’s falling back to the default text/html for MIME types.
The Solution:
You need to create the my-ingress-ingress-nginx-controller ConfigMap in the ingress-nginx namespace. Here’s how:
-
Create a
configmap.yamlfile:Create a file named
configmap.yaml(or any name you prefer) with the following content:apiVersion: v1 kind: ConfigMap metadata: name: my-ingress-ingress-nginx-controller namespace: ingress-nginx labels: app.kubernetes.io/name: ingress-nginx app.kubernetes.io/component: controller data: mime-types.conf: | include /etc/nginx/mime.types;Important:
- Make sure the
nameandnamespacein themetadatasection match the values expected by the Ingress Controller (my-ingress-ingress-nginx-controllerandingress-nginx, respectively). These values MUST match the command-line arguments passed to the pod. - Include the
labelsas shown. These labels are often used by the Ingress Controller to identify the ConfigMap. - Double-check the indentation is correct. YAML is sensitive to indentation.
- Make sure the
-
Apply the ConfigMap:
kubectl apply -f configmap.yaml -
Verify the ConfigMap is Created:
kubectl -n ingress-nginx get configmapsYou should now see
my-ingress-ingress-nginx-controllerin the list. -
Restart the Ingress Controller Deployment:
kubectl -n ingress-nginx rollout restart deployment my-ingress-ingress-nginx-controller -
Test!
curl -I https://yourdomain/assets/xxxx.css curl -I https://yourdomain/xxxx.jsYou should now get the correct
content-typeheaders.
Explanation:
The Ingress Controller expects the my-ingress-ingress-nginx-controller ConfigMap to exist. Because it doesn’t exist, the Ingress Controller is running with a default, minimal configuration that doesn’t include the correct MIME type mappings. Creating the ConfigMap provides the Ingress Controller with the configuration it needs.
This should finally solve the problem. Let me know if you encounter any issues!