1

1.我的环境是Redhat 7,Django 2.2,nginx版本:nginx/1.15.12。我有两个静态位置,我需要在两个时间都得到服务。同一服务器上的第一个位置和第二个位置是已安装的网络驱动器,用于存储图像和所有其他 pdf。

有两个问题。1. 如何为两个静态位置提供服务。这两个位置是 a)/www/AutomationServices/static b)/storage/Investigator_Validator。在 nginx conf 我试过

 location /static/ {
        autoindex on;
        root /www/AutomationServices/static;
       try_files $uri $uri/  @secondStatic;
    }
    location /storage/ {
        root /storage/Investigator_Validator;
    }
    location @secondStatic{
        root /storage/Investigator_Validator;
        }

我在 /storage/Investigator_Validator 下有多个文件夹。在每个子文件夹中都有根据其分类分组的 pdf、图像和文本文件。当我试图为他们服务时,它不起作用。但是当我尝试 python manage.py collectstatic 时,/storage/Investigator_Validator 中的文件夹和文件被复制到这个文件夹/www/AutomationServices/static 中。它当时有效,但 /storage/Investigator_Validator 中的文件会动态变化,因此每次创建这些图像时运行 python manage.py collectstatic 并不好。

需要使用 nginx 网络服务器提供两个静态位置。如何使用 nginx 在文件夹中搜索 js、css、images 文件的子文件夹。

4

1 回答 1

1

因此 URI/static/foo/bar.css可能在/www/AutomationServices/static/foo/bar.css或中找到/storage/Investigator_Validator/foo/bar.css。后者不包括/static/作为路径名的一部分。

有很多方法可以实现这一点,一种是使用正则表达式 location来捕获 URI 的尾部。root设置为公共根目录(在本例中)/try_files用于依次测试每个路径。

例如:

location ~ ^/static/(.*)$ {
    root /;
    try_files /www/AutomationServices/static/$1 /storage/Investigator_Validator/$1 =404;
}
于 2019-07-15T19:07:39.200 回答