more_set_headers 问题求解

163 views
Skip to first unread message

lua初学者

unread,
Jan 21, 2013, 2:58:52 AM1/21/13
to open...@googlegroups.com
静态pdf文件,目的是让网页直接提示用户保存

配置如下:
location ~ /sync/dl/[0-9]+/(.+)\.pdf {
more_set_headers "Content-Type:application/octet-stream";
more_set_headers "Content-Disposition: attachment;filename=$1";
more_clear_headers "Location";
alias /opt/track/data/;
}

输出确实 301 并且location 多一个 /
HTTP/1.1 301 Moved Permanently
Server: nginx/1.3.10
Date: Mon, 21 Jan 2013 07:39:21 GMT
Content-Type: application/octet-stream;
Content-Length: 185
Connection: keep-alive
Content-Disposition: attachment;filename=4201E7A296C640270C7d57g0.pdf

如果不用正则式匹配,可以网页正常提示用户保存,但是文件名只能写死

location /sync/dl {
more_set_headers "Content-Type:application/octet-stream";
more_set_headers "Content-Disposition: attachment;filename=1.pdf";
alias /opt/track/data/;
}

返回200  但是文件名只能写死
HTTP/1.1 200 OK
Server: nginx/1.3.10
Date: Mon, 21 Jan 2013 07:49:14 GMT
Content-Type: application/octet-stream
Content-Length: 814419
Last-Modified: Fri, 11 Jan 2013 08:28:59 GMT
Connection: keep-alive
ETag: "50efcd4b-c6d53"
Content-Disposition: attachment;filename=1.pdf
Accept-Ranges: bytes

kindy

unread,
Jan 21, 2013, 3:42:52 AM1/21/13
to openresty
方案1
location /sync/dl {
more_set_headers "Content-Type:application/octet-stream";
more_set_headers ": ";
        header_filter_by_lua 'ngx.header["Content-Disposition"] = "attachment;filename=" .. string.match(ngx.var.uri, "^/sync/dl/%d+/(.+)$")';
alias /opt/track/data/;
}

方案2
location /sync/dl {
set $f "";
if ($uri ~ "^/sync/dl/\\d+/(.+)$“) {
set $f $1;
}
more_set_headers "Content-Type:application/octet-stream";
more_set_headers "Content-Disposition: attachment;filename=$f";
alias /opt/track/data/;
}

方案3
类似 方案2,只是 set $f 变成 map $f {} ,具体写法请随便研究下。
随便估计,应该 map 的方案性能最好。






--
- - - - - - - - - - - -
林青(Kindy Lin)

kindy

unread,
Jan 21, 2013, 3:44:16 AM1/21/13
to openresty
另,
各方案都是现写的,未经过测试,请验证、测试后使用。


2013/1/21 kindy <kin...@gmail.com>

lua初学者

unread,
Jan 21, 2013, 3:51:36 AM1/21/13
to open...@googlegroups.com
thx 已经搞定。
location /sync/dl/ {
alias /opt/track/data/;
more_set_headers "Content-Type:application/octet-stream";
    if ($request_filename ~* ^.*/([^/]*?)$) {
      set $exits true;
      set $msi $1;
    }
    if ($exits) {
      add_header Content-Disposition: "attachment; filename=$msi";
    } 
}

在 2013年1月21日星期一UTC+8下午4时42分52秒,kindy写道:
Reply all
Reply to author
Forward
Message has been deleted
0 new messages