如何通过nginx将多个Django项目的静态文件提供给同一个域

我想设置我的nginx conf,以便我可以为我的多个Django项目提供相关文件。 最终,我希望每个应用程序在www.example.com/app1,www.example.com/app2等都可用。它们都从位于各自项目根目录中的“静态文件”目录提供静态文件。

项目结构:

Home Ubuntu Web www.example.com ref logs app app1 app1 static bower_components templatetags app1_project templates static-files app2 app2 static templates templatetags app2_project static-files app3 tests templates static-files static app3_project app3 venv 

当我使用下面的conf时,服务我在/ static / location中指定的应用程序的静态文件没有问题。 我也可以访问在他们的位置find的不同的应用程序。 但是,我不知道如何同时为所有的应用程序提供所有的静态文件。 我已经研究了使用“try_files”命令的静态位置,但不知道如何看它是否工作。

Nginx Conf – 只为一个应用程序提供静态文件:

 server { listen 80; server_name example.com; server_name www.example.com; access_log /home/ubuntu/web/www.example.com/logs/access.log; error_log /home/ubuntu/web/www.example.com/logs/error.log; root /home/ubuntu/web/www.example.com/; location /static/ { alias /home/ubuntu/web/www.example.com/app/app1/static-files/; } location /media/ { alias /home/ubuntu/web/www.example.com/media/; } location /app1/ { include uwsgi_params; uwsgi_param SCRIPT_NAME /app1; uwsgi_modifier1 30; uwsgi_pass unix:///home/ubuntu/web/www.example.com/app1.sock; } location /app2/ { include uwsgi_params; uwsgi_param SCRIPT_NAME /app2; uwsgi_modifier1 30; uwsgi_pass unix:///home/ubuntu/web/www.example.com/app2.sock; } location /app3/ { include uwsgi_params; uwsgi_param SCRIPT_NAME /app3; uwsgi_modifier1 30; uwsgi_pass unix:///home/ubuntu/web/www.example.com/app3.sock; } # what to serve if upstream is not available or crashes error_page 400 /static/400.html; error_page 403 /static/403.html; error_page 404 /static/404.html; error_page 500 502 503 504 /static/500.html; # Compression gzip on; gzip_http_version 1.0; gzip_comp_level 5; gzip_proxied any; gzip_min_length 1100; gzip_buffers 16 8k; gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript; # Some version of IE 6 don't handle compression well on some mime-types, # so just disable for them gzip_disable "MSIE [1-6].(?!.*SV1)"; # Set a vary header so downstream proxies don't send cached gzipped # content to IE6 gzip_vary on; } 

基本上我想有一些像(我知道这不会工作)

 location /static/ { alias /home/ubuntu/web/www.example.com/app/app1/static-files/; alias /home/ubuntu/web/www.example.com/app/app2/static-files/; alias /home/ubuntu/web/www.example.com/app/app3/static-files/; } 

或者(它可以提供基于uri的静态文件)

 location /static/ { try_files $uri $uri/ =404; } 

所以基本上,如果我使用上面的try_files,是我的项目目录结构中的问题? 或者我完全脱离基于此,我需要把每个应用程序在一个子域,而不是走这条路线? 感谢您的任何build议

TLDR:我想去:
www.example.com/APP_NAME_HERE

并有nginx服务的静态位置:
/home/ubuntu/web/www.example.com/app/APP_NAME_HERE/static-files/;

简单的选项是为每个应用程序创build子域名(例如:app1.example.com)。 这样你可以在每个url上为你的静态文件目录设置一个别名。