nginx fails on OpenShift with mkdir() client_temp failed (13: Permission denied)
nginx: [emerg] mkdir() "/var/cache/nginx/client_temp" failed (13: Permission denied)Short answer
OpenShift starts the container under a random user ID that belongs to the root group, whatever USER your image sets. nginx then cannot create its temp directories in paths owned by another user. Give group 0 read and write access to those paths, move them, and listen on a port above 1024.
Fix it fast
1Find out which cause you have
Find which user ID the pod really runs as, and read the owner and mode of the directory in the error.
oc rsh my-pod id oc rsh my-pod ls -ld /var/cache/nginx2Match what you see, then jump to its fix
- 1
id shows a uid you did not choose and gid=0(root), and ls shows another owner without group write.The directory is not writable by group 0Fix - 2
The error names a temp directory under /var/cache/nginx, and you prefer to keep that tree read-only.nginx still tries to write to a path you can moveFix - 3
After the permission error is gone, nginx fails to bind or the pod restarts.nginx listens on port 80, which needs rootFix
- 1
What does the error look like?
nginx: [emerg] mkdir() "/var/cache/nginx/client_temp" failed (13: Permission denied)
Cause 1 of 3
The directory is not writable by group 0
1Check
OpenShift assigns an arbitrary UID from the project's range and puts it in the root group (GID 0). That UID can write only to paths that group 0 may write.
oc rsh my-pod ls -ld /var/cache/nginx /var/run2Fix
In the Dockerfile, give group 0 the same access as the owner on every path nginx writes to.
RUN chgrp -R 0 /var/cache/nginx /var/run && \
chmod -R g=u /var/cache/nginx /var/run3Verify
Rebuild, redeploy and read the pod log.
oc logs my-podConfirmed by docs.docker.com
Cause 2 of 3
nginx still tries to write to a path you can move
1Check
client_body_temp_path sets the directory that holds temporary files for client request bodies; its default is client_body_temp relative to the nginx prefix.
grep -rn temp_path /etc/nginx/2Fix
Point the temp path at a directory that group 0 can write, such as /tmp, in nginx.conf.
http {
client_body_temp_path /tmp/client_temp;
}3Verify
Test the config inside the image, then redeploy.
nginx -tConfirmed by nginx.org
Cause 3 of 3
nginx listens on port 80, which needs root
1Check
Ports lower than 1024 require root privileges, and OpenShift does not run the container as root.
grep -rn "listen" /etc/nginx/conf.d/2Fix
Listen on 8080 inside the container and let the Service map port 80 to it.
server {
listen 8080;
}3Verify
The pod becomes Ready.
oc get podsConfirmed by docs.docker.com
How do I stop it coming back?
- Write the chgrp -R 0 and chmod -R g=u lines into every image that is meant for OpenShift.
- Do not depend on the USER directive; the platform can replace it with an arbitrary user ID.
- Keep writable paths in as few places as possible, such as /tmp, so the Dockerfile has less to fix.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- Dispatch ProA real-time logistics platform: Flutter apps, Spring Boot services and a Kafka event pipeline on AWS.
- stegnor.comMy company site: 804 pages from a Python and Jinja generator, with 15 free browser tools.
Related fixes
Servers
Angular routes return 404 from nginx when you refresh or open a link
The home page loads, but refreshing /some/route shows an nginx 404
Angular routes like /users/42 are not files, so nginx looks for them on disk and returns 404.
3 causes, each with a fixServers
Docker php image has no php.ini and my edits do not stay
A setting changed in php.ini inside the container is gone after the container is recreated
The official php image ships php.ini-development and php.ini-production but no active php.ini.
2 causes, each with a fixServers
The MinIO Console will not work behind an nginx proxy: no login, 403 on the WebSocket, or repeated disconnects
The Console UI shows repeated disconnects
Behind a proxy the MinIO Console needs three things.
4 causes, each with a fixServers
nginx returns 403 and logs directory index is forbidden
directory index of "/usr/share/nginx/html/" is forbidden
nginx got a request for a folder, found none of the index files it was told to look for, and listing the folder is off by default.
3 causes, each with a fixServers
nginx in a container returns 502 with 'connection refused' to another container
502 Bad Gateway
Inside a container, localhost is the container itself.
4 causes, each with a fixServers
The nginx container serves the default page or 404 because /usr/share/nginx/html has no app files
requests for your pages return 404 Not Found
The official nginx image serves whatever sits in /usr/share/nginx/html.
3 causes, each with a fix