Django + nginx + flup配置成功

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.