nginx / unit

NGINX Unit - universal web app server - a lightweight and versatile open source server that simplifies the application stack by natively executing application code across eight different programming language runtimes.
https://unit.nginx.org
Apache License 2.0
5.27k stars 322 forks source link

does nginx unit supports mapping? #1065

Closed umlumpa closed 5 months ago

umlumpa commented 5 months ago
        map $arg_disposition $content_disposition {
          default "attachment;";
          "inline" "inline;";
          "attachment" "attachment;";
        }

        map $arg_attachment_name $attachment_name {
          default 'filename="offer.pdf"';
          "~*" 'filename="$arg_attachment_name"';
        }

        server {
          listen  80;

          error_log /dev/stdout error;
          access_log /dev/stdout;

hot it will look like in nginx unit conf?

lcrilly commented 5 months ago

Looks like these are probably used with the add_header directive? Can you show the part of the nginx.conf where the map variables are used?

The Unit approach is likely to be something like this route action:

{
    "share": "/var/www$uri",
    "response_headers": {
        "Content-Disposition": "`${args.disposition == 'inline' ? 'inline' : 'attachment'}; filename=\"${args.attachment_name === undefined  ? 'offer.pdf' : args.attachment_name}\"`"
    }
}

It might be cleaner moved into a JS function that prepares the header. That would provide the same decoupling of logic and config that you get with the map blocks.

$ curl -I 'localhost/download?disposition=foo&attachment_name=foo.pdf'
HTTP/1.1 200
Content-Disposition: attachment; filename="foo.pdf"
Server: Unit/1.31.1
Date: Tue, 16 Jan 2024 09:53:11 GMT
umlumpa commented 5 months ago

yes of course

        map $arg_disposition $content_disposition {
          default "attachment;";
          "inline" "inline;";
          "attachment" "attachment;";
        }

        map $arg_attachment_name $attachment_name {
          default 'filename="offer.pdf"';
          "~*" 'filename="$arg_attachment_name"';
        }

        server {
          listen  80;

          client_max_body_size 100M;
          error_log /dev/stdout error;
          access_log /dev/stdout;

          real_ip_header proxy_protocol;

          location / {
            client_max_body_size 100M;
            root /var/www/html/src/ApiPublic/Web;
            try_files $uri /index.php$is_args$args;
          }

          location /runtime/offer {
            alias /mnt/runtime/offer;
            add_header Cache-Control 'no-store, no-cache, must-revalidate, max-age=0';
            add_header Content-Disposition '$content_disposition $attachment_name';
          }

          location ~ ^/.+\.php(/|$) {
            fastcgi_pass localhost:9000;

            fastcgi_split_path_info ^(.+\.php)(/.*)$;
            include fastcgi_params;

            fastcgi_param SCRIPT_FILENAME /var/www/html/src/ApiPublic/Web$fastcgi_script_name;
            fastcgi_param HTTPS off;
          }
        }
lcrilly commented 5 months ago

Then my first guess was pretty good :)

{
  "match": {
      "uri": "/runtime/offer*"
  },
  "action": {
    "share": "/mnt$uri",
    "response_headers": {
      "Cache-Control": "no-store, no-cache, must-revalidate, max-age=0",
      "Content-Disposition": "`${args.disposition == 'inline' ? 'inline' : 'attachment'}; filename=\"${args.attachment_name === undefined  ? 'offer.pdf' : args.attachment_name}\"`"
    }
  }
}