Custom nginx.conf from ConfigMap in Kubernetes
Nothing complex, it's the root directory in the nginx.conf
is not defined correctly.
Checking the logs with kubectl logs <<podname>> -n <<namespace>>
gives why the 404 error
is happening for a particular request.
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"
It's because of location
inside your configmap
is referring to wrong directory as root root html
.
Change the location to a directory which has index.html
will fix the issue. Here is the working configmap with root /usr/share/nginx/html
. However this could be manipulated as you want, but we need to make sure files exist in the directory.
apiVersion: v1kind: ConfigMapmetadata: name: nginx-confdata: 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; } } }