0

我简单说一下我的问题,我需要修剪前14个字符(0-9和-),下载文件时,它只是标题Content-Disposition,如何实现这样的东西?

我希望文件看起来像这样:

1235467890123-文件名.txt

对于这样

文件名.txt

配置文件:

upstream oxide_io {
    ip_hash;

    server 127.0.0.1:xxx;
    server 127.0.0.1:xxx;
    server 127.0.0.1:xxx;
}

server {
    listen 443      ssl http2;
    listen [::]:443 ssl http2;

    server_name oxidepolska.pl;
    error_log /var/log/nginx/forum-error.log error;

    ssl ***

    proxy_hide_header X-Powered-By;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;

    proxy_set_header Host $http_host;
    proxy_set_header X-NginX-Proxy true;
    proxy_redirect off;

    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";

    gzip ***

    location @oxide {
        proxy_pass http://oxide_io;
    }

    location ~ ^/assets/(.*) {
        root /var/www/forum/;
        try_files /build/public/$1 /public/$1 @oxide;
    }

    location /plugins/ {
        root /var/www/forum/build/public/;
        try_files $uri @oxide;
    }

    location ~ ^/public/uploads/files/(.*)$ {
        root /var/www/forum/public/uploads/files/;
        add_header Content-Disposition 'inline; filename="$1"';
    }

    location / {
       proxy_pass http://oxide_io;
    }

    error_page 502 503 /503.html;

    location = /503.html {
    root /var/www/forum/public/;
    }
}

server {
    if ($host = oxidepolska.pl) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    listen 80;
    listen [::]:80;

    server_name oxidepolska.pl;
}
4

3 回答 3

1

我终于明白了,您正试图摆脱在某些论坛上传的文件中添加的时间戳前缀?这是另一个配置,手动添加Content-Disposition标题:

map $uri $content_disposition {
    '~/\d{13}-([^/]+)$' 'attachment; filename="$1"';
}

server {

    ...

    location <uploaded_files_location> {
        add_header Content-Disposition $content_disposition;
    }

    ...

}

当请求的文件不匹配模式 \d{13}-(13 位数字和文件名其余部分之前的“-”符号)时,$content_disposition 变量评估为空字符串,因此Content-Disposition不将标头添加到响应中。

于 2018-12-15T03:47:39.237 回答
0
➜  ~ curl -I https://ddl.oxidepolska.pl/1544820059040-lootconfig.zip
HTTP/2 200 
server: nginx
date: Fri, 14 Dec 2018 21:54:56 GMT
content-type: application/zip
content-length: 7138
last-modified: Fri, 14 Dec 2018 20:40:59 GMT
etag: "5c14155b-1be2"
accept-ranges: bytes
于 2018-12-14T21:55:32.840 回答
0

免责声明:此答案是关于修改现有Content-Disposition标头,这不是 OP 所要求的。

map $upstream_http_content_disposition $content_disposition {
    default $upstream_http_content_disposition;
    '~^(.*filename="?)[-\d]{14}(.+?)("?$)' $1$2$3;
}

server {

    ...

    location @oxide {
        proxy_pass http://oxide_io;
        proxy_hide_header Content-Disposition;
        add_header Content-Disposition $content_disposition;
    }

    ...

    location / {
        proxy_pass http://oxide_io;
        proxy_hide_header Content-Disposition;
        add_header Content-Disposition $content_disposition;
    }

    ...

}

(假设前 14 个字符只能是 0-9 和 '-')

于 2018-12-14T21:15:47.557 回答