【发布时间】:2016-12-31 23:00:40
【问题描述】:
我有一个非常标准的设置,带有一个带有前端控制器的类似 symfony2 的应用程序,在 nginx 1.10 和 Centos7 上运行。这一切都按预期工作,在预期的地方阻塞等等。
server {
listen 80;
root /opt/my/code/web;
index app.php;
charset utf-8;
location / {
try_files $uri $uri/ /app.php$is_args$args;
}
# pass the PHP scripts to php5-fpm
location ~ ^/app\.php(/|$) {
# problem here
location ~ ^/recording {
add_header Content-Type audio/x-wav;
}
fastcgi_split_path_info ^(.+?\.php)(/?.*)$;
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_index app.php;
include /etc/nginx/fastcgi_params;
fastcgi_param DOCUMENT_ROOT $realpath_root;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# Prevents URIs that include the front controller. This will 404:
internal;
}
# return 404 for all other php files not matching the front controller
location ~ \.php$ {
return 404;
}
}
我有一些问题,但主要是我想要对匹配 /recording 的 URI 进行特殊处理,但它仍然必须通过前端控制器。 (这个没有争议,如果URI匹配/recording,它必须通过前端控制器并修改响应头)
由于try_files 重定向到location ~ ^/app\.php(/|$) nginx 用于位置匹配的$uri 参数更新为/app.php,因此任何嵌套位置都将不起作用。
我不能在前端控制器块之外使用add_header,因为任何add_header 指令都会在内部重定向中被丢弃。
显然我也不能将location if 与add_header 一起使用。
这在 apache 中很容易,但我发现的唯一远程解决方案使用第三方 lua 模块,安装文档在这方面有点薄,而且在 centos 上从源代码编译它的想法让我心悸。
【问题讨论】:
-
在我的情况下,我特别放弃了,它就是行不通,所以我只是对这段代码gist.github.com/Erutan409/…进行了现代化处理,并在响应之前使用文件扩展名设置了mime类型
标签: php symfony nginx nginx-location