Well I am currently trying to get my django application served using nginx and uwsgi. I am currently using a virtual environment to which uwsgi is installed. However I am currently getting a 502 bad gateway error when attempting to access the page.
The Error I am experiencing.
2014/02/27 14:20:48 [crit] 29947#0: *20 connect() to unix:///tmp/uwsgi.sock failed (13: Permission denied) while connecting to upstream, client: 144.136.65.176, server: domainname.com.au, request: "GET /favicon.ico HTTP/1.1", upstream: "uwsgi://unix:///tmp/uwsgi.sock:", host: "www.domainname.com.au"
This is my nginx.conf
# mysite_nginx.conf
# the upstream component nginx needs to connect to
upstream django {
server unix:///tmp/uwsgi.sock; # for a file socket
#server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}
# configuration of the server
server {
# the port your site will be served on
listen 80;
# the domain name it will serve for
server_name .domainname.com.au; # substitute your machine's IP address or FQDN
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
# Django media
location /media {
alias /home/deepc/media; # your Django project's media files - amend as required
}
location /static {
alias /home/deepc/static; # your Django project's static files - amend as required
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include /home/deepc/.virtualenvs/dcwebproj/dcweb/uwsgi_params; # the uwsgi_params file you installed
}
}
Here is my uwsgi.ini file
[uwsgi]
socket=/tmp/uwsgi.sock
chmod-socket=644
uid = www-data
gid = www-data
chdir=/home/deepc/.virtualenvs/dcwebproj/dcweb
module=dcweb.wsgi:application
pidfile=/home/deepc/.virtualenvs/dcwebproj/dcweb.pid
vacuum=true
From what i have read on google its a permissions problem with the www-data group and /tmp/ directory. However I am new to this and have tried to changer the permission level of the folder to no avail. Could someone point me in the right direction? Is this a permissions problem.
Also is it ok practice to put the sock file in tmp directory?
Thanks
I grappled with this problem for a while, and found that the
uid
andgid
flags from myuwsgi.ini
file were not being applied to the.sock
fileYou can test this by running uwsgi, then checking the permissions on your
.sock
file using the linux commandls -l
.The solution for me was to run
uwsgi
with sudo:with the
.ini
file containing the flags:Then the permissions on the
.sock
file were correct, and the502 Bad Gateway
error finally vanished!Hope this helps :)
This is take me a lot of time to find the problem with permissions. And the problem is with permissions of course. Default user is nginx. What i did: in
/etc/nginx/nginx.conf
change user:Next join your user to www-data goup:
Next set uwsgi:
And then restart nginx:
And finaly restart uwsgi.
This issue made me crazy. My environment is centos7+nginx+uwsgi, using unix socket connection. The accepted answer is awesome, just add some points in there.
ROOT USER, QUICK TEST
First, turn off selinux, then change chmod-socket to 666, and finally start uwsgi using root.
Like this
OTHER USER
If you use the other user you created to start uwsgi, make sure that the permissions of the user folder under the home folder are 755, and that the owner and the group are corresponding.
For example
Another great article for CentOS users:
https://axilleas.me/en/blog/2013/selinux-policy-for-nginx-and-gitlab-unix-socket-in-fedora-19/
Although answers are useful regarding CentOS the problem lies beneath SELinux.
I followed the entire article but what solved the issue I believed where the following commands:
Please substitute user with your actual user for granting permissions. Same applies for the directory under chmod command.
I think you just need to change your socket file to 666(664 is ok with www-data), or remove it and run uwsgi server again.
In my uwsgi.ini:
Wow, this problem takes me almost a whole day!
I use
uwsgi 2.0.14, nginx 1.10.1, django 1.10
To sum up, the most important thing is to make sure both of below two users have
rwx
permission tosocket
file:nginx
;uWSGI
;So, you can check them one by one.
First you can check if the web server
nginx
has permission by refreshing the url, say http://192.168.201.210:8024/morning/, without running uwsgi. If you see/var/log/nginx/error.log
No such file or directory, like this:Just create a file named
helloworld.sock
, and refresh the url and check log file again, if you see Permission denied in log file, like this:It means web server
nginx
does not have all permission to read, write and execute. So you can grant permission to this file:sudo chmod 0777 helloworld.sock
Then, refresh the url and check log file again, if you see Connection refused in log file, like this:
This is a good sign, it means your web server
nginx
has the permission to usehelloworld.sock
file from now on.Next to run
uwsgi
and check if the user ofuwsgi
has permission to usehelloworld.sock
. Firstly, remove the filehelloworld.sock
we have created before.Run uwsgi:
uwsgi --socket /usr/share/nginx/html/test/helloworld.sock --wsgi-file wsgi.py
If you see bind(): Permission denied [core/socket.c line 230], it means
uwsgi
don't have permission to bindhelloworld.sock
. This is the problem of the directorytest
, the parent directory ofhelloworld.sock
.Now, you can run
uwsgi
successful.But maybe you still see 502 Bad Gateway, it's terrible, I have seen it all day. If you check
error.log
file again, you will see this again:What's wrong???
Check the detail of
helloworld.sock
file, you can see:uWSGI
gives this file755
permission automatically.You can change it by adding
--chmod-socket
:OK! Finally, you can see:
Take away message:
uwsgi_params
file's location is not important;nginx
user anduwsgi
user not same and even not at the same group, so I need to give777
permission tohelloworld.sock
and its parent dirtest/
;helloworld.sock
file in your home directory, you'll always get Permission denied.socket
file path, one in nginx conf file, for me it ishelloworld_nginx.conf
; one when you run uwsgi.This is my
helloworld_nginx.conf
file: