当前位置:  开发笔记 > 编程语言 > 正文

如何为django nginx虚拟服务器+ fcgi?

如何解决《如何为djangonginx虚拟服务器+fcgi?》经验,为你挑选了1个好方法。

我想通过nginx运行几个虚拟主机,每个主机通过fcgi提供不同的django应用程序.这可能吗?如果是这样,有没有人有关于在哪里/如何开始的良好资源?nginx文档似乎主要是示例,但没有我正在尝试的特定配置...



1> Van Gale..:

最近有关于设置nginx的两篇相当不错的博客文章,但两者都使用nginx来提供静态(或缓存)内容,使用apache + mod_wsgi来提供Django.我更喜欢这个,因为mod_wsgi使进程管理比fcgi更容易(除了其他优点).

无论如何,这里是帖子:

Django和Ubuntu Intrepid Almanac

django,nginx,memcached - 动态三重奏

编辑:好吧我在一年前用我的nginx + django +虚拟主机配置文件挖出一个旧的tar文件,然后我切换到apache + mod_wsgi.这是一台开发机器,所以你需要调整#工人,连接等.

nginx.conf:

user nginx nginx;
worker_processes 2;

error_log /var/log/nginx/error_log info;

events {
    worker_connections  1024;
    use epoll;
}

http {
    include         /etc/nginx/mime.types;
default_type    application/octet-stream;

log_format main
            '$remote_addr - $remote_user [$time_local] '
    '"$request" $status $bytes_sent '
            '"$http_referer" "$http_user_agent" '
    '"$gzip_ratio"';

client_header_timeout   3m;
client_body_timeout     3m;
send_timeout            3m;

connection_pool_size            256;
client_header_buffer_size       1k;
large_client_header_buffers     4 2k;
request_pool_size               4k;

gzip on;
gzip_min_length 1100;
gzip_buffers    4 32k;
gzip_types      text/plain text/html application/x-javascript text/xml text/css;

output_buffers  4 32k;
postpone_output 1460;

sendfile        on;
tcp_nopush      on;
tcp_nodelay     on;

keepalive_timeout       75 20;

ignore_invalid_headers  on;

include vhosts.d/*.conf;
}

因此,主要的nginx.conf包含vhosts.d /子目录中的每个文件.我的一个文件用于在localhost上提供PHP(可能正在运行phpMyAdmin),如下所示:

vhosts.d/00_localhost:

server {

  listen 127.0.0.1:80;
  server_name localhost;

  access_log /var/log/nginx/localhost.access_log main;
  error_log /var/log/nginx/localhost.error_log info;

  root /var/www/localhost/htdocs;
  index index.php index.html index.htm;

    location ~ .php$ {
        fastcgi_pass   127.0.0.1:8888;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /var/www/localhost/htdocs$fastcgi_script_name;
        fastcgi_param  QUERY_STRING     $query_string;
        fastcgi_param  REQUEST_METHOD   $request_method;
        fastcgi_param  CONTENT_TYPE     $content_type;
        fastcgi_param  CONTENT_LENGTH   $content_length;
  }
}

然后像这样的本地Django开发服务器:

50_django.local:

server {

  listen  80;
  server_name django.local;

  access_log /var/log/nginx/django.access.log main;
  error_log /var/log/nginx/django.error.log info;

  root /var/www/django.local;

  location ~* ^.+\.(mpg|avi|mp3|swf|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|txt|tar|mid|midi|wav|rtf|mpeg)$ {
    access_log off;
    break;
  }

  location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|bmp|js)$ {
    expires 30d;
    break;
  }

  location / {
    fastcgi_pass unix:/var/www/django.local/server.sock;
    include fastcgi.conf;
  }

  location ^~ /admin/ {
    fastcgi_pass unix:/var/www/django.local/server.sock;
    include fastcgi.conf;
    access_log off;
    auth_basic "Welcome to admin";
  }
}

最后,每个虚拟服务器都包含每个位置的fastcgi.conf.

fastcgi.conf:

fastcgi_pass_header Authorization;
fastcgi_intercept_errors off;

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_param SERVER_PORT       $server_port;
fastcgi_param SERVER_PROTOCOL   $server_protocol;
fastcgi_param SERVER_NAME       $server_name;

fastcgi_param REQUEST_URI       $request_uri;
fastcgi_param DOCUMENT_URI      $document_uri;
fastcgi_param DOCUMENT_ROOT     $document_root;
fastcgi_param SERVER_ADDR       $server_addr;
fastcgi_param REMOTE_USER       $remote_user;
fastcgi_param REMOTE_ADDR       $remote_addr;
fastcgi_param REMOTE_PORT       $remote_port;
fastcgi_param SERVER_SOFTWARE   "nginx";
fastcgi_param GATEWAY_INTERFACE "CGI/1.1";

fastcgi_param UID_SET           $uid_set;
fastcgi_param UID_GOT           $uid_got;

我不确定上述所有内容都是必需的,但这是我切换到mod_wsgi的另一个原因...优越的支持和文档:)


我从未接触过这种工作,但是男人,这是一个什么样的答案.
推荐阅读
wangtao
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有