uWsgi nginx integration error - django

I am using uWsgi to deploy my django site here is my uWsgi.ini:
[uwsgi]
socket=/var/run/uwsgi.sock
virtualenv=/root/edupalm/env/
chdir=/root/edupalm/edupalm
master=True
workers=8
pidfile=/var/run/uwsgi-master.pid
max-requests=5000
module=edupalm.wsgi:application
and using nginx, here is my configuration:
server {
listen 9000;
server_name 162.243.146.127;
access_log /var/log/nginx/edupalm_access.log;
error_log /var/log/nginx/edupalm_error.log;
location /static/ {
alias /root/edupalm/edupalm/static/;
}
location / {
uwsgi_pass unix:///var/run/uwsgi.sock;
}
}
but I am having 502 Bad Gateway
here is the logs:
nginx:
2013/11/26 08:31:09 [error] 1758#0: *57 upstream prematurely closed connection while reading response header from upstream, client: 197.160.112.183, server: 162.243.146.127, request: "GET /admin HTTP/1.1", upstream: "uwsgi://unix:///var/run/uwsgi.sock:", host: "162.243.146.127:9000"
uwsgi:
-- unavailable modifier requested: 0 --
nginx is running on user www-data and uwsgi is running as root

It's advisable to use new user for your project, not root
The problem is in configuration, you should to add
plugin=python
for permissions it's better to use www-data user/group:
uid = www-data
gid = www-data
chmod-socket = 777
chown-socket = www-data

It looks like you are using a distribution package instead of official uWSGI sources. Just load (after having installed it) the python plugin with plugin = python in your config
http://uwsgi-docs.readthedocs.org/en/latest/WSGIquickstart.html

location / {
uwsgi_pass unix:///var/run/uwsgi.sock;
include uwsgi_params;
uwsgi_param SCRIPT_NAME '';
}

I similarly had this problem for a combination of Django, uWSGI and nginx running behind CloudFront. It turned out for me that the routing table in CloudFront didn't behave as expected, so some of the callbacks weren't received.
Specifically, route "/" stole traffic from "*".
There was another issue where my Django server was running unexpected code; as a user logging in caused their User model to be changed, which I hadn't predicted for some reason. So yeah, don't rule out that your Django server might be legitimately busy, causing a timeout of the socket.

Related

Nginx 502 bad gateway for deploying Django Project even it works with uwsgi

the nginx web server is successfully installed and working. But I get 502 bad gateway error.
When I check the socket file path I can't see any file.
/home/bilal/uwsgi/sites/testproject.ini ->
[uwsgi]
home = /home/bilal/my-first-project/venv
chdir = /home/bilal/my-first-project
wsgi-file = /home/bilal/my-first-project/projectname/wsgi.py
socket = /home/bilal/uwsgi/testproject.sock
vacuum = true
chown-socket = bilal:www-data
chmod-socket = 660
/etc/nginx/sites-available/testproject ->
server {
listen 80;
server_name <domain> <www.domain>;
location / {
include uwsgi_params;
uwsgi_pass unix:/home/bilal/uwsgi/testproject.sock;
}
}
When I try to connect to server with ip address I get welcome to nginx page because ip isn't written for server_name. But when I try to connect to server with domain I get this error:
502 Bad Gateway
nginx/1.14.0 (Ubuntu)
I think this problem is about the .sock file but I don't know how can I handle it.

Nginx: 403 Forbidden nginx/1.12.1 (Ubuntu)

I've never before configured any production server, I'm trying to configure nginx and keep getting the 403 Forbidden error. I can't figure out the reason why it's happening.
Here is a complete error report:
[crit] 25145#25145: *1 connect() to unix:/home/albert/deploy_test/django_env
/run/gunicorn.sock failed (13: Permission denied) while connecting to
upstream, client: 192.168.1.118, server: 192.168.1.118, request: "GET /
HTTP/1.1", upstream: "http://unix:/home/albert/deploy_test/django_env
/run/gunicorn.sock:/", host: "192.168.1.118"
Here is my /etc/nginx/sites-available/deployproject.conf:
(I removed the default config and created a symlink as follows: sudo ln -s /etc/nginx/sites-available/deployproject.conf /etc/nginx/sites-enabled/deployproject.conf)
upstream sample_project_server {
# fail_timeout=0 means we always retry an upstream even if it failed
# to return a good HTTP response (in case the Unicorn master nukes a
# single worker for timing out).
server unix:/home/albert/deploy_test/django_env/run/gunicorn.sock fail_timeout=0;
}
server {
listen 80;
server_name 192.168.1.118;
client_max_body_size 4G;
access_log /home/albert/logs/nginx-access.log;
error_log /home/albert/logs/nginx-error.log;
location /static/ {
alias /home/albert/static/;
}
location /media/ {
alias /home/albert/media/;
}
location / {
# an HTTP header important enough to have its own Wikipedia entry:
# http://en.wikipedia.org/wiki/X-Forwarded-For
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# enable this if and only if you use HTTPS, this helps Rack
# set the proper protocol for doing redirects:
# proxy_set_header X-Forwarded-Proto https;
# pass the Host: header from the client right along so redirects
# can be set properly within the Rack application
proxy_set_header Host $http_host;
# we don't want nginx trying to do something clever with
# redirects, we set the Host: header above already.
proxy_redirect off;
# set "proxy_buffering off" *only* for Rainbows! when doing
# Comet/long-poll stuff. It's also safe to set if you're
# using only serving fast clients with Unicorn + nginx.
# Otherwise you _want_ nginx to buffer responses to slow
# clients, really.
# proxy_buffering off;
# Try to serve static files from nginx, no point in making an
# *application* server like Unicorn/Rainbows! serve static files.
if (!-f $request_filename) {
proxy_pass http://sample_project_server;
break;
}
}
# Error pages
error_page 500 502 503 504 /500.html;
location = /500.html {
root /home/albert/static/;
}
}
Here is the complete tutorial I'm using to deploy my app. Here I'm just trying to deploy the most primitive,default django app but in my real app I'm using django as a serverside, so there seems to be no need for nginx to serve static and all that.
File Permissions. Incorrect file permissions are another cause of the "403 Forbidden" error. The standard setting of 755 for directories and 644 for files is recommended for use with NGINX. The NGINX user also needs to be the owner of the files
Try to change the permissions on your web dir
sudo chown -R albert:www-data /webdirectory
sudo chmod -R 0755 /webdirectory
Move all your sites inside the webdirectory do not leave the dir and files in your root home.
Have you taken a look at the gunicorn docs here which has example of how to configure nginx
http://docs.gunicorn.org/en/stable/deploy.html
Can you try running gunicorn via TCP instead of unix socket, in your upstream sample_project_server replace server with:
server 192.168.0.7:8000 fail_timeout=0;
What are the settings in gunicorn? You can bind to localhost via TCP with the following, to check that it isn't a problem with your unix socket:
--bind 127.0.0.1:8000

django nginx uwsgi not working

i ve created a django application not using virtual environment. I ve installed nginx and trying to integrate them via uwsgi application.
Here my configurations files.
[uwsgi]
chdir = /home/elastic/workspace/ES_Brevetti
wsgi-file = ES_Brevetti/wsgi.py
master = true
processes = 5
uid = nginx
gid = nginx
socket = unix:///socket/uwsgi.sock
chmod-socket = 666
vacuum = true
i've created the file /sockect/uwsgi.sock with permission 777
chown nginx:nginx -R /sockect/uwsgi.sock
and below nginx conf file:
upstream django {
server unix:///socket/uwsgi.sock; # for a file socket
#server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}
server {
listen 80;
server_name 10.184.2.231;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
charset utf-8;
location /static/ {
alias /home/elastic/workspace/ES_Brevetti;
}
location / {
include uwsgi_params;
uwsgi_pass unix:///socket/uwsgi.sock;
}
}
When i launch "systemctl start nginx" nginx is started with error:
connect() to unix:///socket/uwsgi.sock failed (111: Connection refused) while connecting to upstream,
When i run uwsgi --ini /etc/uwsgi/sites/ES_Brevetti.ini it doesnt run with error:
.....
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
bind(): Permission denied [core/socket.c line 230]
What i am doing wrong? On google i can only see configurations with VENV while i am not using virtual environment.
The docs (which I'm guessing you were following) suggest that the socket configuration option in your uwsgi ini file should take a path to the socket, not a URL. Could you try changing:
[uwsgi]
...
socket = unix:///socket/uwsgi.sock
to
[uwsgi]
...
socket = /socket/uwsgi.sock
The solution was change the socket = unix:///socket/uwsgi.sock to socket = /socket/uwsgi.sock in the uwsgi.ini file. I dont know why in some doc it appears the double slash for escaping.

uwsgi not creating a socket file

I have the following configuration:
[uwsgi]
vhost = true
plugins = python,logfile
socket = /tmp/mysite.com.sock
master = true
enable-threads = true
processes = 4
wsgi-file = /home/ubuntu/myappwebsite/myapp/myapp/wsgi.py
chdir = /home/ubuntu/myappwebsite/myapp
touch-reload = /home/ubuntu/myappwebsite/myapp/myapp/reload
logger=file:/tmp/uwsgi-error.log
vacuum = true
and for nginx:
server {
listen 80;
server_name mysite.com www.mysite.com;
access_log /var/log/nginx/mysite.com_access.log;
error_log /var/log/nginx/mysite.com_error.log;
client_max_body_size 75M;
location / {
uwsgi_pass unix:///tmp/mysite.com.sock;
include uwsgi_params;
}
}
When I look at the nginx logs i see the following:
2016/11/14 01:36:43 [error] 10779#10779: *9 connect() to unix:///tmp/mysite.com.sock failed (111: Connection refused) while connecting to upstream, client: 10.13.142.61, server: mysite.com, request: "GET / HTTP/1.1", upstream: "uwsgi://unix:///tmp/mysite.c
I'm getting the following in my uwsgi log file:
Mon Nov 14 02:08:22 2016 - unable to find logger file
I'm not able to find any issues in the uwsgi logs for some strange reason. Is there anything else i'm missing here? I'm using an EC2 with Ubuntu 16.04 if that helps. I have googled and reconfigured this from scratch again and still to no avail. Please, any help appreciated!
make sure the owner of the wsgi file is the same as nginx user.
chown www-data:www-data wsgi.py

Permission denied Accessing static files Nginx +uwsgi +Django

I have setup Django project on CentOS 6.5 with Nginx and uwsgi.
I am Getting error while accessing static content as below (/var/log/nginx/error.log)-
2015/11/02 19:05:37 [error] 29701#0: *52 open() "/home/amar/workspace/myproj/config/static/rest_framework/js/default.js" failed (13: Permission denied), client: 172.29.100.104, server: myapi.dev, request: "GET /static/rest_framework/js/default.js HTTP/1.1", host: "myapi.dev", referrer: "http://myapi.dev/api/v1/datasets/"
My /etc/nginx/conf.d/virtual.conf is as shown below -
# 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
#
#API
#
server {
# the port your site will be served on
listen 80;
# the domain name it will serve for
server_name myapi.dev; # substitute your machine's IP address or FQDN
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
location /static {
autoindex on;
alias /home/amar/workspace/myproj/config/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 /etc/nginx/uwsgi_params; # the uwsgi_params file you installed
}
}
Here is my uwsgi.ini file :
[uwsgi]
chdir = /home/amar/workspace/myproj
#home = %(base)/.virtualenvs/myproj
module = config.wsgi:application
home = /home/amar/.virtualenvs/myproj
master = true
processes = 3
socket = /tmp/uwsgi.sock
chmod-socket = 777
vacuum = true
Could someone point me in the right direction?
It took time but I've fixed the problem myself. Changed the user from amar to root and set static folder permission to 666. Hope it helps someone in future.
Probably related to SELinux. You will need to allow HTTPD scripts and modules to connect to the network.
setsebool httpd_can_network_connect on -P