如何在Nginx和uWSGI上运行多个Django站点?
是否可以使用Nginx和uWSGI在同一台服务器上运行多个Django站点 我认为有必要运行多个uWSGI实例(每个站点一个)。我将/etc/init.d/uwsgi复制到uwsgi2并更改了端口号。但是,我得到了以下错误:如何在Nginx和uWSGI上运行多个Django站点?,django,nginx,uwsgi,Django,Nginx,Uwsgi,是否可以使用Nginx和uWSGI在同一台服务器上运行多个Django站点 我认为有必要运行多个uWSGI实例(每个站点一个)。我将/etc/init.d/uwsgi复制到uwsgi2并更改了端口号。但是,我得到了以下错误: # /etc/init.d/uwsgi2 start Starting uwsgi: /usr/bin/uwsgi already running. 如何运行多个uWSGI实例 谢谢您可以创建多个虚拟主机,允许您独立地托管多个站点。更多信息请点击此处: 这里还有一些关于如
# /etc/init.d/uwsgi2 start
Starting uwsgi: /usr/bin/uwsgi already running.
如何运行多个uWSGI实例
谢谢您可以创建多个虚拟主机,允许您独立地托管多个站点。更多信息请点击此处:
这里还有一些关于如何设置虚拟主机的详细信息。您可以使用运行多个uwsgi实例 这将处理新工作实例的创建。这些例子的名字都非常精彩有趣。每个附庸只需要一个配置文件,该文件通常放在
/etc/uwsgi/vassals
对于nginx,您需要为希望服务的每个主机创建一个服务器块。只需为您要服务的每个主机更改server\u name
指令。下面是一个例子:
#Simple HTTP server
server {
listen 80;
root /usr/share/nginx/www;
server_name host1.example.com;
}
#Django server
server {
listen 80;
server_name host2.example.com;
#...upstream config...
}
重要提示:确保已在
/etc/hosts
中指定主机名。我发现,如果不这样做,我的django站点也在默认服务器IP上提供服务,尽管指定它只应在我的nginx配置中的特定主机名上提供服务 我看到了很多建议,比如@donturner的答案。i、 e.在nginx配置文件中设置两个或多个不同的服务器
。但问题是,每台服务器都需要一个唯一的服务器名称
不同的域名或子域名。这种情况如何:我想像这样为两个不同的Django项目提供服务器:
www.ourlab.cn/site1/ # first Django project
www.ourlab.cn/site2/ # second Django project
这样,我们就可以在一台服务器上配置所有设置
这是我在/etc/nginx/nginx.conf
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
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 /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
# /etc/nginx/conf.d/self_configure.conf
server {
listen 80;
server_name www.ourlab.cn;
# note that these lines are originally from the "location /" block
root /mnt/data/www/ourlab;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# Django media
location /media {
# your Django project's media files - amend as required
alias /mnt/data/www/metCCS/metCCS/static/media;
}
location /static {
# your Django project's static files - amend as required
# first project's static files path
alias /mnt/data/www/metCCS/metCCS/static/static_dirs;
}
location /static_lip {
# second project's static files path
alias /mnt/data/www/lipidCCS/lipidCCS/static/static_dirs;
}
# match www.ourlab.cn/metccs/*
location ~* ^/metccs {
include uwsgi_params;
uwsgi_pass unix:/run/uwsgi/metCCS.sock;
}
# match www.ourlab.cn/lipidccs/*
location ~* ^/lipidccs {
include uwsgi_params;
uwsgi_pass unix:/run/uwsgi/lipidCCS.sock;
}
}
这是我在/etc/nginx/conf.d/self_configure.conf
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
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 /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
# /etc/nginx/conf.d/self_configure.conf
server {
listen 80;
server_name www.ourlab.cn;
# note that these lines are originally from the "location /" block
root /mnt/data/www/ourlab;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# Django media
location /media {
# your Django project's media files - amend as required
alias /mnt/data/www/metCCS/metCCS/static/media;
}
location /static {
# your Django project's static files - amend as required
# first project's static files path
alias /mnt/data/www/metCCS/metCCS/static/static_dirs;
}
location /static_lip {
# second project's static files path
alias /mnt/data/www/lipidCCS/lipidCCS/static/static_dirs;
}
# match www.ourlab.cn/metccs/*
location ~* ^/metccs {
include uwsgi_params;
uwsgi_pass unix:/run/uwsgi/metCCS.sock;
}
# match www.ourlab.cn/lipidccs/*
location ~* ^/lipidccs {
include uwsgi_params;
uwsgi_pass unix:/run/uwsgi/lipidCCS.sock;
}
}
您还需要将Django项目的一个settings.py
文件更改为STATIC\u URL='/STATIC\u lip/'
,以便两个项目可以分别使用它们的静态文件
一个新的发现是
nginx
可以自己为静态文件提供服务器。即使我们关闭了uwsgi和Django,我们也可以通过浏览器使用这些文件。运行2个uwsgi实例的解决方案在第二个链接中:uwsgi--uid 1001-w customer1app--limit as 128-p 3-M-s 127.0.0.1:3031如果在帝王模式下运行uwsgi,它将处理多个实例的创建(也称为vassals)。对于每个django项目,您只需要一个uwsgi配置文件。更多信息:这两个链接都过时了。我想这是同一个问题,