簡體   English   中英

如何在Nginx和uWSGI上運行多個Django站點?

[英]How to run multiple Django sites on Nginx and uWSGI?

是否可以使用Nginx和uWSGI在同一台服務器上運行多個Django站點?

我想有必要運行多個uWSGI實例(每個站點一個)。 我將/etc/init.d/uwsgi復制到uwsgi2並更改了端口號。 但是,我收到以下錯誤:

# /etc/init.d/uwsgi2 start
Starting uwsgi: /usr/bin/uwsgi already running.

如何運行多個uWSGI實例?

謝謝

您可以創建創建多個虛擬主機,允許您托管多個站點,彼此獨立。 更多信息: http//wiki.nginx.org/VirtualHostExample

這里有一些更詳細的信息以及如何設置虛擬主機http://projects.unbit.it/uwsgi/wiki/RunOnNginx#VirtualHosting

您可以使用Emperor Mode運行多個uwsgi實例。

這將處理新工作器實例的創建。 這些實例非常出色地被稱為附庸 每個附庸只需要一個配置文件,通常在/etc/uwsgi/vassals放置(或符號鏈接)

對於nginx,您需要為要提供服務的每個主機創建一個服務器塊。 只需為要提供的每個主機更改server_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的回答。 即在nginx配置文件中設置兩個或多個不同的server 但問題是每個服務器需要一個唯一的server_name ,不同的域名或子域名。 這種情況怎么樣:我想像這樣服務兩個不同的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設置

# /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_URL = '/static_lip/' ,這樣兩個項目就可以單獨使用它們的靜態文件。

一個新的發現是nginx可以自己服務靜態文件。 即使我們關閉uwsgi和Django,我們也可以通過瀏覽器使用這些文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM