You are not logged in.
Setting up Django with nginx/FastCGI is pretty straight forward, just:
1) Create a "Custom (Listening on Port)" app. In the remaining steps replace all occurrences of "(App Name)" and "(App Port)" with the name and port of the app that you created.
2) Connect to the server that your account is on using an SSH client and execute the following commands:
cd ~/webapps/(App Name)
mkdir -p bin lib/python2.5
# Install Django and flup.
cd ~/webapps/(App Name)
PYTHONPATH=lib/python2.5 easy_install-2.5 -d lib/python2.5 -s bin http://www.djangoproject.com/download/1.0/tarball/ flup
./bin/django-admin.py startproject myproject
# Install nginx.
cd ~/webapps/(App Name)
wget http://sysoev.ru/nginx/nginx-0.6.32.tar.gz
tar fxz nginx-0.6.32.tar.gz
cd nginx-0.6.32
./configure --prefix=~/webapps/(App Name)
make
make install
cd ..
rm -fr nginx-0.6.32
rm nginx-0.6.32.tar.gz
# Replace "example.com" with your domain name and "/" with your app's app path.
# Also, please note that the config code contains backslashes in the fastcgi parameters.
# If you copy and paste the entire 'cat ... EOF' block as shown here, the nginx.conf
# file will be created without the backslashes. If you are copying and pasting the config
# shown here into an existing nginx.conf, be sure to remove those backslashes.
cd ~/webapps/(App Name)
cat > conf/nginx.conf <<EOF
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
server {
listen (App Port);
server_name example.com;
location / {
fastcgi_pass unix:`echo $PWD`/myproject/myproject.sock;
fastcgi_param CONTENT_LENGTH \$content_length;
fastcgi_param CONTENT_TYPE \$content_type;
fastcgi_param PATH_INFO \$fastcgi_script_name;
fastcgi_param QUERY_STRING \$query_string;
fastcgi_param REQUEST_METHOD \$request_method;
fastcgi_param SERVER_NAME \$server_name;
fastcgi_param SERVER_PORT \$server_port;
fastcgi_param SERVER_PROTOCOL \$server_protocol;
}
}
}
EOF
# Start Django
cd ~/webapps/(App Name)/myproject
python2.5 manage.py runfcgi maxchildren=1 maxspare=1 method=prefork socket=$PWD/myproject.sock > /dev/null 2>&1 &
# Start nginx
cd ~/webapps/(App Name)
./sbin/nginx3) Add the app that you created in step one to a website and wait a couple of minutes for the changes to take affect.
That's it. ![]()
Last edited by IAIHMB (2008-09-19 14:49:51)
Offline
You might also want to add something like:
This rewrite rule appends a slash to the url if one is not already there. I think nginx is better to handle this than django.
if ($request_uri ~* "^[\w\-\/]+[^\/?]$") {rewrite ^(.*)$ $scheme://$host$1/ permanent;}This rewrite rule to normalizes the url from example\.com to www\.example\.com
if ($host ~* "^[\w-]+\.\w{2,3}$") {rewrite ^(.*)$ $scheme://www.$host$1 permanent;}These rewrite rules would go somewhere inside:
location / {
}Offline
I've tried the setup above and had couple of issues with the tutorial so I thought I'd share them.
1. I'm not sure why but when I do ./configure --prefix=~/webapps the tilde didn't expand and nginx ended up installed in /home/glisha/webapps/nginxapp/~/conf...
So I had to specify the full path for --prefix=/home/glisha/webapps/nginxapp/.
2. ./configure couldn't find pcre and it complained that I would't be able to use http_redirect. I had to download pcre and point the configure script to the source.
cd ~/webapps/nginxapp/ wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-7.8.tar.gz tar -xzf pcre-7.8.tra.gz cd pcre-7.8 ./configure --prefix=/home/glisha/webapps/nginxapp/pcre make make install # I'm not sure if I need to install or just make pcre.
That I run configure for nginx once again with pcre
./configure --prefix=/home/glisha/webapps/nginxapp --with-pcre=/home/glisha/webapps/nginxapp/pcre-8.4/ make make install
3. I had to add the content-type passed for the fcgi in conf/nginx.conf because my forms didn't work.
fastcgi_param CONTENT_TYPE $content_type;
---
I run pylot with 10 agents for 360 sec on the same django application with apache/mod_python and on nginx/fscgi. I managed to get 8request/second with apache/mod_python and 13request/second with nginx and the memory footprint is great.
Apache took about 50MB+15MB for memcache and nginx/fcgi only 20MB without memcache.
I don't know why doesn't my application hit memcached when I run it thru nginx, but I'll ask in another thread.
Offline
IAIHMB wrote:
Code:
./configure --prefix=~/webapps/(App Name)
This only created directories in the right place if I gave it a fully qualified prefix path:
./configure --prefix=/home/redmonkey/webapps/(App Name)
Also, I noticed that adding a "/" to the end of your path causes configure to make paths with double slashes in them, so I left off the final slash i.e
--prefix=<stuff>/webapps/unusability
instead of
--prefix=<stuff>/webapps/unusability/
Simple stuff really.
Thanks a lot IAIHMB
Offline
Hi,
I can't get this to work. I get the following error when I try to start nginx:
[emerg] 28855#0: invalid host in upstream "/home/tinodb/webapps/django/projects/dsac/dsac.sock" in /home/tinodb/webapps/django//conf/nginx.conf:15
The socket file does exist (it's created by manage.py).
Also using
unix:`echo $PWD`/myproject/myproject.sock;
in the nginx.conf file did not work. it gave the error
2009/03/13 06:38:43 [emerg] 27847#0: invalid number of arguments in "fastcgi_param" directive in /home/tinodb/webapps/django//conf/nginx.conf:17
So I used the full path instead: /home/tinodb/webapps/django/projects/dsac/dsac.sock
What can I do to fix this?
Tino
Offline
tinodb wrote:
Hi,
I can't get this to work. I get the following error when I try to start nginx:Code:
[emerg] 28855#0: invalid host in upstream "/home/tinodb/webapps/django/projects/dsac/dsac.sock" in /home/tinodb/webapps/django//conf/nginx.conf:15The socket file does exist (it's created by manage.py).
Also usingCode:
unix:`echo $PWD`/myproject/myproject.sock;in the nginx.conf file did not work. it gave the error
Code:
2009/03/13 06:38:43 [emerg] 27847#0: invalid number of arguments in "fastcgi_param" directive in /home/tinodb/webapps/django//conf/nginx.conf:17So I used the full path instead: /home/tinodb/webapps/django/projects/dsac/dsac.sock
What can I do to fix this?
Tino
This error ...
2009/03/13 06:38:43 [emerg] 27847#0: invalid number of arguments in "fastcgi_param" directive in /home/tinodb/webapps/django//conf/nginx.conf:17
.. has nothing to do with the path to the socket. The problem is that you left the arguments off of your various fastcgi_param directives, eg you have this...
fastcgi_param CONTENT_LENGTH ;
fastcgi_param PATH_INFO ;
fastcgi_param QUERY_STRING ;
fastcgi_param REQUEST_METHOD ;
fastcgi_param SERVER_NAME ;
fastcgi_param SERVER_PORT ;
fastcgi_param SERVER_PROTOCOL ;... when you should have this ...
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param SERVER_NAME $server_name;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_PROTOCOL $server_protocol;Hope that helps!
Regards,
Sean F, WebFaction Support
--
WebFaction - Agile hosting for everyone
http://webfaction.com - http://twitter.com/webfaction
Offline
Ooops, thought I used "include fastcgi_params" ...
thx, it's working now
Offline
I can't seem to get this to work. I have nginx up and running, but when I make a request, it seems as if the request gets munged before it gets do django. For example, when I go to test.natesoares.com/test/, I get:
TypeError at \/test/ 'NoneType' object is not iterable Request Method: \GET Request URL: http://test.natesoares.com%5C/test/%5C/test/?%5C Exception Type: TypeError Exception Value: 'NoneType' object is not iterable
Don't know what's going on here. My nginx.conf is just:
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
server {
listen 7322;
server_name natesoares.com;
location / {
fastcgi_pass unix:/home/soares/webapps/overviewer/overviewer/overviewer.sock;
fastcgi_param CONTENT_TYPE \$content_type;
fastcgi_param CONTENT_LENGTH \$content_length;
fastcgi_param PATH_INFO \$fastcgi_script_name;
fastcgi_param QUERY_STRING \$query_string;
fastcgi_param REQUEST_METHOD \$request_method;
fastcgi_param SERVER_NAME \$server_name;
fastcgi_param SERVER_PORT \$server_port;
fastcgi_param SERVER_PROTOCOL \$server_protocol;
}
}
}Any ideas?
Offline
There is likely an error in your python code.
If you believe that there is an error in your nginx conf then you should enable error logging. In your conf, add:
error_log /path/to/your/error.log
Offline
I don't think it's a python issue, because django isn't getting the correct request. As I said above, when I go to test.natesoares.com/test, I get the following error from django:
... Request Method: \GET Request URL: http://test.natesoares.com%5C/test/%5C/test/?%5C ...
Django hasn't been given the correct URL. So yes, the error thrown (and the one in the logs) is a django error, but the error is thrown by django because django isn't given the right url. I don't know enough about nginx to know what's going on or why test.natesoares.com would be converted to the url that django got. Any ideas?
Offline
I believe your problem may be caused by your use of backslashes in your fastcgi parameters.
fastcgi_param CONTENT_TYPE \$content_type;
Should be:
fastcgi_param CONTENT_TYPE $content_type;
Offline
If you copy and paste everything from the original post, those backslashes will be escaped when the 'cat .... EOF' stuff executes and they won't appear in the final config file.
I've added a comment to the original post to clarify the backslash issue.
Offline
That makes sense. However, I took the slashes out, re-ran manage.py runfcgi, and re-ran sbin/nginx, and the problem is still there. Is there something else I have to do to re-load the new conf file?
Offline
Re-running sbin/nginx does not restart nginx. You need to find the PID of your nginx process, eg:
ps -u your_username -o pid,command | grep nginx
Then kill the master nginx process, eg if the PID was 4321:
kill 4321
THEN you can re-start nginx with sbin/nginx to activate the new config.
Offline
Hi,
I am using this setup, with the addition of the nginx upload progress module, and it works really well (I've even mirrored the whole setup on my Ubuntu Dev server at home). However I can't work out how to get the real IP address of the client to be written in the nginx logs. Can anyone point me in the right direction?
Regards,
Richard.
Offline
frozenskys wrote:
I am using this setup, with the addition of the nginx upload progress module, and it works really well (I've even mirrored the whole setup on my Ubuntu Dev server at home). However I can't work out how to get the real IP address of the client to be written in the nginx logs. Can anyone point me in the right direction?
The real IP is contained in the 'X-Forwarded-For' header. For example, here is the Apache logfile directive we set up when you install one of our regular Django apps:
LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combinedOffline
seanf wrote:
Re-running sbin/nginx does not restart nginx. You need to find the PID of your nginx process, eg:
Code:
ps -u your_username -o pid,command | grep nginxThen kill the master nginx process, eg if the PID was 4321:
Code:
kill 4321THEN you can re-start nginx with sbin/nginx to activate the new config.
It might be better to use:
sbin/nginx -s quit sbin/nginx
Or reload the config:
sbin/nginx -s reload
![]()
Offline
Or even better, send a HUP signal.
kill -HUP `cat /var/run/nginx.pid` will reload the config, and make sure you have no interuption in serving your pages.
Here is my ningx start/stop/restart script:
NAME=nginx
BASEDIR="/home/<user>/webapps/django/sbin/"
PIDFILE="$BASEDIR/../$NAME.pid"
case "$1" in
start)
echo -n "Starting $NAME: "
$BASEDIR$NAME
echo "$NAME Started."
;;
stop)
echo -n "Stopping $NAME: "
if [ -f $PIDFILE ]; then
kill -QUIT `cat $PIDFILE`
rm -f -- $PIDFILE
fi
echo "Stopped."
;;
restart)
echo -n "Restarting $NAME: "
if [ -f $PIDFILE ]; then
kill -HUP `cat $PIDFILE`
else
$BASEDIR$NAME
fi
echo "restarted $NAME."
;;
*)
echo "Usage: $N {start|stop|restart}" >&2
exit 1
;;
esac
exit 0Offline
How would one modify nginx.conf to serve multiple Django projects?
Offline
erikvorhes wrote:
How would one modify nginx.conf to serve multiple Django projects?
Use multiple server entries in your nginx config, pointing to different sockets (and different domains if necessary) eg:
server {
listen 43210;
server_name firstdomain.com;
location / {
fastcgi_pass unix:`echo $PWD`/firstproject/firstproject.sock;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param SERVER_NAME $server_name;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_PROTOCOL $server_protocol;
}
}
server {
listen 43210;
server_name seconddomain.com;
location / {
fastcgi_pass unix:`echo $PWD`/secondproject/secondproject.sock;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param SERVER_NAME $server_name;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_PROTOCOL $server_protocol;
}
}and run multiple Django FastCGI processes:
cd ~/webapps/(App Name)/firstproject python2.5 manage.py runfcgi maxchildren=1 maxspare=1 method=prefork socket=$PWD/firstproject.sock > /dev/null 2>&1 & cd ../secondproject python2.5 manage.py runfcgi maxchildren=1 maxspare=1 method=prefork socket=$PWD/secondproject.sock > /dev/null 2>&1 &
Hope that helps!
Offline
So I've made it so far as to get a friendly "Welcome to nginx" message when I hit the subdomain I've pointed at the app, but it doesn't show my project... when I run manage, I get the following result, is that correct?
[apt9online@web67 leel]$ manage runfcgi maxchildren=1 maxspare=1 method=prefork socket=/home/apt9online/webapps/9online/leel/leel.sock > /dev/null 2>&1 & [1] 15291 [apt9online@web67 leel]$
in the access and error logs, it only shows the browser requesting the favicon and the error that it couldn't find one ![]()
Last edited by bwooceli (2009-08-22 22:30:23)
Offline
That's correct. The output of '[1] 15291' is just telling you the PID of the process it created ![]()
Offline
Still not able to get anything but "Welcome to nginx", so a couple of stupid follow-up questions.
- After running manage fastcgi, should I be able to see that process running then (because I don't)
- Should manage fastcgi create an actual .sock file in my directory (because it doesn't appear to)
Thanks for bearing with me on this.
Offline
1) Yes you should see the django fcgi process if it has started OK
2) Yes if the config is correct you should get a .sock file created.
It sounds like the django project isn't set up properly and is failing - to see why try starting it without the redirect e.g.
manage runfcgi maxchildren=1 maxspare=1 method=prefork socket=/home/apt9online/webapps/9online/leel/leel.sock
Also I would check the settings in your nginx.conf because if the django process is down you should get a "500 Server Error" page from nginx not the "welcome to niginx" page.
Regards,
Richard.
Offline
Thanks, that got me down the right troubleshooting path and I'm up and running
So can someone help enlighten me on a couple other general questions and contexts? When I look at my running processes now, I see that each sock is represented twice.
RSS PID CMD 10848 1254 python2.5 manage.py runfcgi maxchildren=1 maxspare=1 method=prefork socket=/home/apt9online/webapps/9online/motu/motu.sock 10444 1255 python2.5 manage.py runfcgi maxchildren=1 maxspare=1 method=prefork socket=/home/apt9online/webapps/9online/motu/motu.sock 932 3230 nginx: worker process 10168 11762 python2.5 manage.py runfcgi maxchildren=1 maxspare=1 method=prefork socket=/home/apt9online/webapps/9online/leel/leel.sock 15924 11763 python2.5 manage.py runfcgi maxchildren=1 maxspare=1 method=prefork socket=/home/apt9online/webapps/9online/leel/leel.sock 908 14150 ps -u apt9online -o rss,pid,cmd 840 17404 nginx: master process ./sbin/nginx 1564 31894 sshd: apt9online@pts/0 1524 31896 -bash
I'm just trying to get my general knowledge/know-how up, what does this actually mean?
Offline