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存取權限