我想通过 nginx 运行几个虚拟主机,每个虚拟主机都通过 fcgi 为不同的 django 应用程序提供服务。这可能吗?如果是这样,是否有人有关于从哪里/如何开始的良好资源?nginx 文档似乎主要是示例,但我正在尝试的特定配置都没有......
2 回答
最近有两篇关于设置 nginx 的不错的博客文章,但都使用 nginx 来提供静态(或缓存)内容,并使用 apache + mod_wsgi 来服务 Django。我自己更喜欢这个,因为 mod_wsgi 使流程管理比 fcgi 容易得多(还有其他优点)。
无论如何,这里是帖子:
编辑:好吧,在我切换到 apache + mod_wsgi 之前,我用我的 nginx + django + 虚拟主机配置文件挖出了一个旧的 tar 文件。这是一台开发机器,因此您需要调整工人数、连接数等。
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 的另一个原因...卓越的支持和文档 :)
因为有人问了这个问题,所以有人创建了一个 pip 可安装的 django 包,它将从你的 settings.py 为你生成一个 apache 或 nginx vhost 文件
pip install django-vhost