我想制作一些可下载的video,但我的策略似乎并不奏效
location ~* ^/(this-video.mp4)(/.*)$ { alias /some/path/this-video.mp4; add_header Content-Type 'video/mp4'; if ( $2 = "/dl" ) { add_header Content-Disposition 'attachment; filename="$1"'; } }
错误:
#nginx -t
nginx:[emerg]未知“1”variablesnginx:configuration文件/etc/nginx/nginx.conftesting失败
# nginx -v nginx version: nginx/1.6.2
任何想法我做错了什么?
编辑 :
顺便说一句,这一个通过testing:
location ~* ^/(this-video.mp4)(/.*)$ { alias /some/path/$1; add_header Content-Type 'video/mp4'; #if ( $2 = "/dl" ) { # add_header Content-Disposition 'attachment; filename="$1"'; #} } # nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
所以它必须与报价有关?
EDIT2
Nginx 文档状态:
条件可以是以下任何一种:
一个variables名称; 如果variables的值是空string或“0”,则为false;
所以如果/dl url没有提供在URL中,那么$2应该是一个空string?
问题似乎是数字捕获不能通过if块,大概是因为if条件也可以是一个新的正则expression式。 您可以通过使用命名捕获来解决问题,例如:
location ~* ^/(?<filename>.+\.mp4)(?<suffix>/.*)$ { ... }
但是, 不build议使用某些types的if块,因此可以考虑使用两个位置块:
location ~* ^/(.+\.mp4)/dl$ { alias /some/path/$1; add_header Content-Type 'video/mp4'; add_header Content-Disposition 'attachment; filename="$1"'; } location ~* ^/(.+\.mp4) { alias /some/path/$1; add_header Content-Type 'video/mp4'; }
或者更简单的第二个位置,如果适用,就像包含root指令的前缀位置。