2011-10-30 75 views
9

是否有可能使用Nginx和uwsgi在同一臺服務器上運行多個django站點?如何在Nginx和uwsgi上運行多個django站點?

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

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

怎麼可能運行多個uwsgi實例?

感謝

+0

我認爲這是同一個問題在serverfault, http://serverfault.com/questions/498994/how-to-run-multiple-websites-from-one-django-project-on-nginx-and -uwsgi/549337#549337 – MrTeera

回答

9

您可以創建創建多個虛擬主機,使您可以承載多個站點,相互獨立。更多的信息在這裏:http://wiki.nginx.org/VirtualHostExample

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

+0

運行2個uwsgi實例的解決方案位於第二個鏈接中: uwsgi --uid 1001 -w customer1app --limit-as 128 -p 3 -M -s 127.0.0.1:3031 – Thomas

+0

If你在Emperor模式下運行uwsgi它會處理多個實例(又名封套)的創建。每個django項目只需要一個uwsgi配置文件。更多信息:https://uwsgi.readthedocs.org/cn/latest/tutorials/Django_and_nginx.html#emperor-mode – donturner

4

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

這處理新工作者實例的創建。這些實例出色而熱鬧地命名爲vassals。每個vassal只需要一個配置文件,它通常放在(或符號鏈接的)/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配置中的特定主機名上提供。

1

我看到很多建議,像@ 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,我們也可以通過瀏覽器使用這些文件。

相關問題