Django + nginx + flup配置成功
1.安装flup
$sudo easy_install flup
2.安装nginx
3.配置nginx.conf
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /home/stephen/workspace/python/app/django/demo/;
#autoindex;
fastcgi_pass 127.0.0.1:3000;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_pass_header Authorization;
fastcgi_intercept_errors off;
}
..........................
注意这一行:fastcgi_pass 127.0.0.1:3000;
关键点就是nginx的fcgi的ip地址+端口与flup的fcgi进程相匹配
在项目文件夹下启动命令
$python manage.py runfcgi method=prefork host=127.0.0.1 port=3000
让flup的fcgi运行在127.0.0.1:3000上
可以看到与nginx.conf中fastcgi_pass 127.0.0.1:3000;地址端口都是一致的,所以他们之间形成了一个管道从而可以让nginx将请求发给python处理然后返回。
fastcgi_param PATH_INFO $fastcgi_script_name; 注意不要丢下这一行,不然会url映射会有问题。
ip+port是第一种方法,还有一种方法是用sock文件来建立管道。
location / {
root /home/stephen/workspace/python/app/django/demo/;
#autoindex;
fastcgi_pass unix:/tmp/python/python.sock;
fastcgi_param PATH_INFO $fastcgi_script_name;
然后:
#python manage.py runfcgi socket=/tmp/python/python.sock maxrequests=1
即利用/tmp/python/python.sock来搭桥
外网不能访问的问题解决:
通过/etc/init.d/iptables status命令查询是否有打开80端口,如果没有可通过两种方式处理:
1.修改vi /etc/sysconfig/iptables命令添加使防火墙开放80端口
-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 80 -j ACCEPT
2.关闭防火墙
/etc/init.d/iptables stop
#start 开启
#restart 重启
永久性关闭防火墙chkconfig –level 35 iptables off
nginx 访问静态文件:
location ^~/static {
autoindex on;
alias /home/stephen/workspace/python/app/django/demo/static/;
}
把css,js文件放在项目文件夹的static目录下,如果有403访问错误,第一行加入user root root; 给予root访问权限