0

I will briefly tell my problem, I need to trim the first 14 characters (0-9 and -), when downloading a file, it is just about header Content-Disposition, how to achieve something like that?

I want the file to look like this:

1235467890123-FileName.txt

for such

FileName.txt

Config file:

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;
}
  • Your nginx serving as a reverse proxy? Can you update your answer with the according config fragment? – Ivan Shatsky Dec 13 '18 at 20:39
  • Yes, using proxy, answer updated. – Sebastian Sycz Dec 13 '18 at 21:13
  • I see a manually added `Content-Disposition` header at `location ~ ^/public/uploads/files/(.*)$`, but you probably not talking about it, but about an another header with `Content-Disposition: attachment; filename="..."` which might came from upstream? – Ivan Shatsky Dec 14 '18 at 01:07
  • Simply, I would like to get the following https://ddl.oxidepolska.pl/1544820059040-lootconfig.zip from the file https://ddl.oxidepolska.pl/lootconfig.zip. I mean to delete the first 14 characters. – Sebastian Sycz Dec 14 '18 at 21:02
  • And the ddl address is test, if everything goes my way it will be moved to another one. – Sebastian Sycz Dec 14 '18 at 21:03

3 Answers3

1

As I finally understand, you are trying to get rid of timestamp prefix added to files uploaded on some forum? Here is the another config, manually adding Content-Disposition header:

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

server {

    ...

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

    ...

}

When requested file is not matching the pattern \d{13}- (13 digits and "-" sign before the rest of filename), $content_disposition variable evaluated as empty string, so Content-Disposition header is not added to response.

Ivan Shatsky
  • 13,267
  • 2
  • 21
  • 37
0

Disclaimer: This answer is about modifying existing Content-Disposition header, which is not what required by 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;
    }

    ...

}

(assuming first 14 characters can be only 0-9 and '-')

Ivan Shatsky
  • 13,267
  • 2
  • 21
  • 37
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
  • It seems I slightly misunderstand your question. I thought you need to modify existing `Content-Disposition` header, but you want to add one. This is possible too, but we need to define some restrictions, I don't think it would be right to add this header on every response, especially on CSS or JS files. We can limit this header to specific file extensions by checking URI, to specific MIME types or to specific locations. – Ivan Shatsky Dec 14 '18 at 22:50