creamidea / creamidea.github.com

冰糖火箭筒&&蜂蜜甜甜圈
https://creamidea.github.io/
4 stars 4 forks source link

Error: 400 bad request ('xxxxx') when using Uwsgi to connect Nginx and Djanog #24

Open creamidea opened 8 years ago

creamidea commented 8 years ago

This is a disgust problem!!!

One way of solving this problem is to watch the error.log

First, you should know which encode the system is using. You can use locale to look at. Second, make sure your nginx is using the same code as the system. You can put this in nginx.conf

http {
    ...
    charset utf-8;
    large_client_header_buffers 4 16k; # maybe
    ...
}

At last, make sure your Django(Python) using the same code as the nginx. You can put this code in your /your/path/to/lib/python2.7/sitecustomize.py.

# encoding=utf8
import sys

reload(sys)
sys.setdefaultencoding("utf-8")

Good luck.

PS: uwsgi ini file format

[uwsgi]
# Django-related settings
# the virtualenv (full path)
# virtualenv    = /opt/weixnpy/
# the base directory (full path)
chdir = /home/www/xxxxx/wx/weixnpy
# Django's wsgi file
module          = weixnpy.wsgi
#module          = weixnpy.wsgi:application
env             = DJANGO_SETTINGS_MODULE = weixnpy.settings
home            = /opt/weixnpy/ # your virtual environment
# py-programname          = /usr/local/bin/python2.7
# pythonpath      = /home/www/icslg/wx/weixnpy/ 

# process-related settings
# master
master          = true
# maximum number of worker processes
processes       = 4
# the socket (use the full path to be safe
# socket          = 127.0.0.1:8001
socket          = /tmp/xxxxxxx.sock
# socket          = /var/run/weixn.sock
# ... with appropriate permissions - may be needed
chmod-socket    = 664
chown-socket = www:www
# clear environment on exit
vacuum          = true
logto           = /var/log/uwsgi.log
creamidea commented 8 years ago

BTW, here is the nginx proxy inverse

server
{
    listen 80;
    server_name xxx456.tk;
    location / {
        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://192.168.10.40:80;
    }
    access_log logs/xxx456.tk_access.log;
}
creamidea commented 8 years ago

Nginx configure:

http {
  ...
  upstream django {
    # server 127.0.0.1:8001;
    server unix:///tmp/xxxxx.sock;
  }
  ...
  server {
    ...
    location /zdl {
      include     /home/wweixnpy/uwsgi_params;
      uwsgi_pass  django;
    }
    location /weixnpystatic {
      alias /home/weixnpy/static;
    }
  }
}
creamidea commented 8 years ago

Test that uwsgi process is still alive every minute. (maybe, it is fine for you to run the crontab using the same account as uwsgi.

$ crontab -e
* * * * * /bin/bash /home/wx/weixnpy/watch_dog.sh > /dev/null 2>&1

At last, make sure it is running.

$ crontab -l
$ sudo /etc/init.d/crond status # restart

XD