Permission Denied When Nginx Tries to Write Image File – Fix

file-permissionslinuxluanginx

I'm using Nginx for a Linux server and I have this folder /usr/local/openresty/nginx/webfolder/img where I want nginx to save my uploaded image files.

So, this is what I did:

1) I changed the owner of the directory to www-data user in www-data group:

chown -R www-data:www-data /usr/local/openresty/nginx/webfolder/img

2) Then I updated the permissions of the directory:

sudo chmod 0600 /usr/local/openresty/nginx/webfolder/img

Lua upload err part (where I log the err):

fileToSave, errMessage = io.open(savefiletarget, "w+b")
                        if not fileToSave then
                            --ngx.say("failed to open file ", savefiletarget)
                            ngx.log(ngx.NOTICE,'failed to save file : '..savefiletarget..' reason: '..errMessage);
                            ngx.say('{"filename" : "'..filenametosave..'","status" : 0 ,"message":"failed to open file"}')
                            return
                        end

Right now my error log still shows Permission Denied when I try to upload the file.

I assume www-data is the user that nginx uses right? So why I'm not able to still write the file?

Best Answer

You gave the directory the wrong permissions. 0600 will give read and write permissions to the directory. But to access a directory, you also need execute permission. Here's an example:

[jenny@temeraire sf] $ mkdir test1
[jenny@temeraire sf] $ chmod 0600 test1
[jenny@temeraire sf] $ touch test1/foo
touch: test1/foo: Permission denied
[jenny@temeraire sf] $ chmod 0700 test1
[jenny@temeraire sf] $ touch test1/foo
[jenny@temeraire sf] $ ls -ld test1/foo
-rw-r--r--  1 jenny  staff  0 Jun  5 07:46 test1/foo
Related Topic