从 nginx 中的两个位置之一提取静态文件

从 nginx 中的两个位置之一提取静态文件

我有一个小型 Web 应用程序,可将文件上传到服务器,稍后显示在 Web 上。目前我的服务器硬盘几乎已满,我想添加第二个。目前应用程序和所有现有文件都已打开,drive1我将添加drive2。是否可以使用类似方法try_files检查多个位置以查看文件是否存在?

例如,如果有人请求,mysite.com/images/2349879.jpgnginx 会首先查找/drive2/images/2349879.jpg,如果不存在,它会检查drive1/images/2349879.jpg,如果不存在,则会提供 404?


这是我当前的 nginx.conf

server {
    listen 80 default_server;
    listen 443 ssl;
    server_name www.MYSITE.com _;

    ssl_certificate     /srv/apps/MYSITE-ssl/certs/MYSITE.com.pem;
    ssl_certificate_key /srv/apps/MYSITE-ssl/private/MYSITE.com.key;

    ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers RC4:HIGH:!aNULL:!MD5:!kEDH;
    ssl_prefer_server_ciphers on;

    set $https off;
    if ( $scheme = 'https' ) { set $https on; }

    add_header Strict-Transport-Security "max-age=315360000; includeSubdomains";

    keepalive_timeout   70;

    return 301 $scheme://MYSITE.com$request_uri;
}

server {
    listen      80;
    listen  443 ssl;
    server_name  MYSITE.com;

    ssl_certificate     /srv/apps/MYSITE-ssl/certs/MYSITE.com.pem;
    ssl_certificate_key /srv/apps/MYSITE-ssl/private/MYSITE.com.key;

    ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers RC4:HIGH:!aNULL:!MD5:!kEDH;
    ssl_prefer_server_ciphers on;

    set $https off;
    if ( $scheme = 'https' ) { set $https on; }

    add_header Strict-Transport-Security "max-age=315360000; includeSubdomains";

    keepalive_timeout   70;

    root /var/www/MYSITE.com;

    charset utf-8;

    access_log  /srv/apps/logs/MYSITE.access.log  main;

    location /images {
        internal;
    }

    location /images/ {
        internal;
    }

    location ~ ^/images/(.*)$ {
        alias /srv/apps/MYSITE/i/$1;
        include /srv/apps/MYSITE.hosts;
        expires max;
        add_header Pragma "public";
        add_header Cache-Control "public, must-revalidate, proxy-revalidate";
        log_not_found off;
    }

    location ~* \.(?:png|jpe?g|gif|ico|bmp|xml)$ {
        include /srv/apps/MYSITE.hosts;
        expires max;
        add_header Pragma "public";
        add_header Cache-Control "public, must-revalidate, proxy-revalidate";
        log_not_found off;
    }

    location /i/ {
        internal;
        try_files $uri /srv/imagestorage$uri /srv/apps/MYSITE/i$uri;
    }

    location /i {
        internal;
        try_files $uri /srv/imagestorage$uri /srv/apps/MYSITE/i$uri;
    }

    location / {
        index index.php
        try_files $uri $uri/ /index.php?$args;
    }

    error_page  404             /info.php?act=404;
    error_page  500             /info.php?act=500;

    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }

    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

    location ~* \.php$ {
        try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(.*)$;
        include /etc/nginx/fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param HTTPS $https;
        fastcgi_pass php;
    }

    location ~* \.(?:js|css)$ {
        expires max;
        log_not_found off;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one

    location ~ /\. {
        deny  all;
    }

}

这里发生了一些奇怪的事情,所以我会尝试解释一下:文件位于/srv/apps/MYSITE/i,URLmysite.com/i/file.jpg将从该位置返回文件。对 的请求mysite.com/images/file.jpg将返回相同的文件。

我尝试的代码位于try_files $uri /srv/imagestorage$uri /srv/apps/MYSITE/i$uri;上面的两个位置,但是它似乎根本不起作用(我仍然可以看到文件,/i但它根本没有检查新驱动器(/srv/imagestorage)。也许我添加了try_files错误的位置?

答案1

你可以简单地这样做:

server {

[...]

    root /;

    location /images {
        try_files /drive1$uri /drive2$uri;
    }

}

然后http(s)://mysite.com/images/1.png将使用本地文件系统路径提供服务/drive1/images/1.png,或者/drive2/images/1.png如果文件在第一个路径中缺失或返回 404。

显然,这是一个例子,您永远不应该在任何 Web 服务器配置中设置根文件系统路径。

相关内容