django nginx setup issues - django

In the following i am trying to configure from http://uwsgi-docs.readthedocs.org/en/latest/tutorials/Django_and_nginx.html
The following is my conf file below placed in my project directory and have linked the same in the sites-enabled of the nginx directory.
Now when i access my http://127.0.0.1:8000/app1/files?path=/ i end up with 502 Bad Gateway and the nginx error log says
2015/09/21 14:07:41 [error] 8023#0: *7 connect() failed (111: Connection refused) while connecting to upstream, client: 127.0.0.1, server: 127.0.0.1, request: "GET / HTTP/1.1", upstream: "uwsgi://127.0.0.1:8001", host: "127.0.0.1:8000"
but i am able to access this link http://127.0.0.1:8000/media/a.jpg what am i doing wrong here and how can access my django application
# mysite_nginx.conf
# the upstream component nginx needs to connect to
upstream django {
# server unix:///path/to/your/mysite/mysite.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 8000;
# the domain name it will serve for
server_name 127.0.0.1; # 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/rajeev/django-test/mysite/media; # your Django project's media files - amend as required
}
location /static {
#alias /path/to/your/mysite/static; # your Django project's static files - amend as required
alias /home/rajeev/django-test/mysite/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/rajeev/django-test/mysite/conf/uwsgi_params; # the uwsgi_params file you installed
}
}
Edit1: uwsgi_params file
uwsgi_param QUERY_STRING $query_string;
uwsgi_param REQUEST_METHOD $request_method;
uwsgi_param CONTENT_TYPE $content_type;
uwsgi_param CONTENT_LENGTH $content_length;
uwsgi_param REQUEST_URI $request_uri;
uwsgi_param PATH_INFO $document_uri;
uwsgi_param DOCUMENT_ROOT $document_root;
uwsgi_param SERVER_PROTOCOL $server_protocol;
uwsgi_param REQUEST_SCHEME $scheme;
uwsgi_param HTTPS $https if_not_empty;
uwsgi_param REMOTE_ADDR $remote_addr;
uwsgi_param REMOTE_PORT $remote_port;
uwsgi_param SERVER_PORT $server_port;
uwsgi_param SERVER_NAME $server_name;

You've missed one point in this tutorial. Configuration of uWSGI for nginx is different than configuration for checking in browser if uWSGI is working not only by used port, but also by protocol.
When deploying uWSGI for nginx, --http should be replaced by --socket. By that change, nginx will understand properly output from your uWSGI server, but your browser won't.
Configuration for browser (to check if uWSGI is working) can work for nginx also, but you must use proxy_pass instead of uwsgi_pass in nginx, and using it is not recommended, uwsgi protocol is faster and better for communication between web server and uWSGI server.

Related

Running django with uwsgi & nginx error: connect() to unix:/tmp/kb.sock failed

Setting up a django site, I can run it outside the virtualenv with uwsgi --ini kb_uwsgi.ini --http :8000 but when I try to set it up live with nginx brings an error in the logs:
2017/01/30 08:00:26 [crit] 24629#24629: *1 connect() to unix:/tmp/kb.sock failed (2: No such file or directory) while connecting to upstream, client: 197.232.12.165, server: kenyabuzz.nation.news, request: "GET / HTTP/1.1", upstream: "uwsgi://unix:/tmp/kb.sock:", host: "kenyabuzz.nation.news"
Here's the nginx file
# kb.conf
# the upstream component nginx needs to connect to
upstream django {
server unix:/tmp/kb.sock; # for a file socket
#server 0.0.0.0:8000; # for a web port socket (we'll use this first)
}
# configuration of the server
server {
# the port your site will be served on
listen 0.0.0.0:80;
# the domain name it will serve for
server_name kenyabuzz.nation.news; # 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/ubuntu/webapps/kenyabuzz/kb/media; # your Django project's media files - amend as required
}
location /static {
alias /home/ubuntu/webapps/kenyabuzz/kb/static; # your Django project's static files - amend as required
}
location /favicon.ico {
alias /home/ubuntu/webapps/kenyabuzz/kb/static/kb/favicon.ico; # favicon
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include /home/ubuntu/webapps/kenyabuzz/uwsgi_params; # the uwsgi_params file you installed
}
}
uwsgi ini
# kb_uwsgi.ini file
[uwsgi]
# Django-related settings
# the base directory (full path)
chdir = /home/ubuntu/webapps/kenyabuzz
# Django's wsgi file
module = kb.wsgi
# the virtualenv (full path)
home = /home/ubuntu/webapps/djangoenv
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 10
# the socket (use the full path to be safe
socket = /tmp/kb.sock
# ... with appropriate permissions - may be needed
chmod-socket = 664
# clear environment on exit
#vacuum = true
and uwsgi_params
uwsgi_param QUERY_STRING $query_string;
uwsgi_param REQUEST_METHOD $request_method;
uwsgi_param CONTENT_TYPE $content_type;
uwsgi_param CONTENT_LENGTH $content_length;
uwsgi_param REQUEST_URI $request_uri;
uwsgi_param PATH_INFO $document_uri;
uwsgi_param DOCUMENT_ROOT $document_root;
uwsgi_param SERVER_PROTOCOL $server_protocol;
uwsgi_param REQUEST_SCHEME $scheme;
uwsgi_param HTTPS $https if_not_empty;
uwsgi_param REMOTE_ADDR $remote_addr;
uwsgi_param REMOTE_PORT $remote_port;
uwsgi_param SERVER_PORT $server_port;
uwsgi_param SERVER_NAME $server_name;
The file doesn't exist kb.sock and I've considered tried creating it with proper permissions but that leads to an access error that itself maybe diagnosis a problem I created.
I think you should use unix:// rather than unix:
And, read this post, use /var/run instead of /tmp

Django Nginx uWSGI 502 Bad Gateway always

I have been following the http://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html
and went up to the
http://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html#running-the-django-application-with-uwsgi-and-nginx
and started the uwsgi --socket mysite.sock --module mysite.wsgi --chmod-socket=664 it is started but when i load my webpage on browser getting 502 Bad Gateway
Django version 1.10.2
error is
error] 25444#25444: *1 connect() failed (111: Connection refused)
while connecting to upstream, client: 127.0.0.1, server: mytest.com,
request: "GET / HTTP/1.1", upstream: "uwsgi://127.0.0.1:8001", host:
"127.0.0.1:8000"
What could be the issue ?
# configuration of the server
server {
# the port your site will be served on
listen 8000;
# the domain name it will serve for
server_name mytest.com; # substitute your machine's IP address or FQDN
charset utf-8;
# max upload size
client_max_body_size 175M; # adjust to taste
# Django media
location /media {
alias /home/karesh/tutorial/uwsgi-tutorial/mysite/media; # your Django project's media files - amend as required
}
location /static {
alias /home/karesh/tutorial/uwsgi-tutorial/mysite/static; # your Django project's static files - amend as required
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass unix:/home/karesh/tutorial/uwsgi-tutorial/mysite/mysite.sock;
include /home/karesh/tutorial/uwsgi-tutorial/mysite/uwsgi_params; # the uwsgi_params file you installed
}
}
uwsgi_params file inside project directory
uwsgi_param QUERY_STRING $query_string;
uwsgi_param REQUEST_METHOD $request_method;
uwsgi_param CONTENT_TYPE $content_type;
uwsgi_param CONTENT_LENGTH $content_length;
uwsgi_param REQUEST_URI $request_uri;
uwsgi_param PATH_INFO $document_uri;
uwsgi_param DOCUMENT_ROOT $document_root;
uwsgi_param SERVER_PROTOCOL $server_protocol;
uwsgi_param REQUEST_SCHEME $scheme;
uwsgi_param HTTPS $https if_not_empty;
uwsgi_param REMOTE_ADDR $remote_addr;
uwsgi_param REMOTE_PORT $remote_port;
uwsgi_param SERVER_PORT $server_port;
uwsgi_param SERVER_NAME $server_name;
A few things, that could help you:
1) Add user, that runs uWSGI, to nginx group
2) On CentOS with enabled SE the best way to put socket is a directory under /run
3) Set socket permissions to <your_user>:nginx(in other words - set permission for socket for your user and for nginx group.
These steps normally helps me to figure out with permissions error.

Receiving 411 errors with Haproxy + Nging when uploading with chunked encoding

here is my server architecture:
port 443 --> haproxy 1.6.3 --> nginx 1.1.19 --> uwsgi 2.0.13.1 --> wsgi python server (Django)
I know it's weird to use both haproxy+nginx but I have no choice because I need haproxy for another project on the same server on port 443.
here is my haproxy config:
frontend www-https
bind *:443 ssl crt /etc/ssl/private/
mode http
option httpclose
acl nginx hdr_end(host) -i example.com
backend nginx
mode http
balance leastconn
option forwardfor
option http-server-close
option forceclose
no option httpclose
server nginx-01 nginx:52654 check
and here is my nginx config
server {
listen 52654 default_server;
charset utf-8;
client_max_body_size 75M;
chunked_transfer_encoding on;
location / {
uwsgi_pass django;
proxy_buffering off;
uwsgi_param QUERY_STRING $query_string;
uwsgi_param REQUEST_METHOD $request_method;
uwsgi_param CONTENT_TYPE $content_type;
uwsgi_param CONTENT_LENGTH $content_length;
uwsgi_param REQUEST_URI $request_uri;
uwsgi_param PATH_INFO $document_uri;
uwsgi_param DOCUMENT_ROOT $document_root;
uwsgi_param SERVER_PROTOCOL $server_protocol;
uwsgi_param HTTPS $https if_not_empty;
uwsgi_param REMOTE_ADDR $remote_addr;
uwsgi_param REMOTE_PORT $remote_port;
uwsgi_param SERVER_PORT $server_port;
uwsgi_param SERVER_NAME $server_name;
}
}
Problem I have is that when I make an upload wuth a web client using chunked encoding, I get the 411 HTTP error Content-Length required.
If I bypass haproxy it's working fine. But using haproxy + nginx gives me the 411 error for chunked encoding.
Any idea?
Your nginx version is quite old. Try upgrade it.

django nginx uwsgi deploying fail

I am trying to deploy a simple Django project, but fail all the time. I am following the instruction here.
When I use python manage.py runserver 0.0.0.0:8000 I can see that my site is running:
In the virtualenv I've installed uWSGI:
pip install uwsgi
and wrote the test.py:
# test.py
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return [b"Hello World"] # python3
#return ["Hello World"] # python2
When running uwsgi --http :8000 --wsgi-file test.py I can see
I then installed en started Nginx:
sudo apt-get install nginx
sudo /etc/init.d/nginx start
I can see
and then wrote mysite_nginx.conf:
upstream django {
# server unix:///path/to/your/mysite/mysite.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 8000;
# the domain name it will serve for
server_name 10.211.55.21; # 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 /path/to/your/mysite/media; # your Django project's media files - amend as required
#}
#location /static {
# alias /path/to/your/mysite/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/parallels/books/mysite/uwsgi_params; # the uwsgi_params file you installed
}
}
because it is just a simple site I commented out the media and static paths, and uwsgi_params:
uwsgi_param QUERY_STRING $query_string;
uwsgi_param REQUEST_METHOD $request_method;
uwsgi_param CONTENT_TYPE $content_type;
uwsgi_param CONTENT_LENGTH $content_length;
uwsgi_param REQUEST_URI $request_uri;
uwsgi_param PATH_INFO $document_uri;
uwsgi_param DOCUMENT_ROOT $document_root;
uwsgi_param SERVER_PROTOCOL $server_protocol;
uwsgi_param REQUEST_SCHEME $scheme;
uwsgi_param HTTPS $https if_not_empty;
uwsgi_param REMOTE_ADDR $remote_addr;
uwsgi_param REMOTE_PORT $remote_port;
uwsgi_param SERVER_PORT $server_port;
uwsgi_param SERVER_NAME $server_name;
the directory:
and run:
sudo ln -s ~/home/parallels/books/mysite/mysite_nginx.conf
/etc/nginx/sites-enabled/
I can see mysite_nginx.conf within /etc/nginx/sites-enabled/:
and then run:
sudo /etc/init.d/nginx restart
uwsgi --socket :8001 --wsgi-file test.py
I see nothing in my webbrowser:
I do not know why, whenever I try deploying a Django site the socket part fails.
Finally I made it by using XML to configure uWSGI and deploying Django project successfully. However, this time I do not use a virtualenv.
I created the virtual host configuration /etc/nginx/sites-enabled/mysite:
server {
listen 80; ## listen for ipv4; this line is default and implied
#listen [::]:80 default ipv6only=on; ## listen for ipv6
server_name 10.211.55.21;
access_log /var/log/nginx/mysite-access.log ;
error_log /var/log/nginx/mysite-error.log ;
location / {
uwsgi_pass 127.0.0.1:8630;
include uwsgi_params;
}
}
Then created the /home/parallels/books/mysite/django.xml file, note that the name of the file can be anything but should end with *.xml:
<uwsgi>
<socket>127.0.0.1:8630</socket>
<chdir>/home/parallels/books/mysite/mysite</chdir>
<pythonpath>..</pythonpath>
<module>wsgi</module>
</uwsgi>
the <chdir>/home/parallels/books/mysite/mysite</chdir> just point to <module>wsgi</module> because there is a wsgi.py in /home/parallels/books/mysite/mysite/wsgi.py.
I then run the following commands:
$ sudo /etc/init.d/nginx restart
$ uwsgi -x django.xml
now the website is reachable:
But I fail to configure and deploying Django using uwsgi.ini.

nginx and uWSGI gives "504 Gateway timeout"

I am following the tutorial Setting up Django and your web server with uWSGI and nginx.
uWSGI is up and running
I set up uwsgi to serve my Django project with the following line.
mydjangoproj $ uwsgi --http 0.0.0.0:8002 --module wsgi --harakiri 5
This works when I go there in a browser, to 42.42.42.42:8002.
My nginx setup
nginx is running as a daemon, and visiting it's default site, port 80, works.
I added this as a site to nginx using the following mydjangoproj_nginx.conf file:
server {
listen 8000;
server_name 42.42.42.42;
charset utf-8;
client_max_body_size 75M;
location /static {
alias /home/myuser/mydjangoproj/static;
}
location / {
uwsgi_pass 127.0.0.1:8002;
include /home/myuser/mydjangoproj/uwsgi_params;
}
}
I use the unmodified version of uwsgi_params, from the tutorial:
uwsgi_param QUERY_STRING $query_string;
uwsgi_param REQUEST_METHOD $request_method;
uwsgi_param CONTENT_TYPE $content_type;
uwsgi_param CONTENT_LENGTH $content_length;
uwsgi_param REQUEST_URI $request_uri;
uwsgi_param PATH_INFO $document_uri;
uwsgi_param DOCUMENT_ROOT $document_root;
uwsgi_param SERVER_PROTOCOL $server_protocol;
uwsgi_param HTTPS $https if_not_empty;
uwsgi_param REMOTE_ADDR $remote_addr;
uwsgi_param REMOTE_PORT $remote_port;
uwsgi_param SERVER_PORT $server_port;
uwsgi_param SERVER_NAME $server_name;
It does serve the static files perfectly.
Error
If I visit 42.42.42.42:8000 it hangs for a long time, until the nginx timeout I guess, and I get 504 Gateway Time-out.
uWSGI writes nothing in the shell. If visiting directly in browser, it does write about receiving a request.
The nginx error log writes, only after the timeout:
2014/12/11 05:31:12 [error] 28895#0: *1 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 66.66.66.66, server: 42.42.42.42, request: "GET / HTTP/1.1", upstream: "uwsgi://42.42.42.42:8002", host: "42.42.42.42:8000"
If I close the uWSGI, which is just run from a shell, I instantly get a 502 Bad Gateway.
When searching online, people just recommend setting the uWSGI timeout lower than the nginx timeout, that's why I run uWSGI with --harakiri 5.
So, what is my problem here?
I think you are running uwsgi in http mode --http 0.0.0.0:8002 but you have configured nginx as uwsgi proxy. You should change your uwsgi script to something like:
uwsgi --socket :8002 --module uwsgi --harakiri 5
Note that if you are running nginx and uwsgi on the same machine is better to use unix sockets