【问题标题】:nginx add_header on specific URI to PHP app with front controller带有前端控制器的 PHP 应用程序的特定 URI 上的 nginx add_header
【发布时间】: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 ifadd_header 一起使用。

这在 apache 中很容易,但我发现的唯一远程解决方案使用第三方 lua 模块,安装文档在这方面有点薄,而且在 centos 上从源代码编译它的想法让我心悸。

【问题讨论】:

  • 在我的情况下,我特别放弃了,它就是行不通,所以我只是对这段代码gist.github.com/Erutan409/…进行了现代化处理,并在响应之前使用文件扩展名设置了mime类型

标签: php symfony nginx nginx-location


【解决方案1】:

如果内部重定向困扰我们,让我们删除内部重定向:) 你可以通过 fastcgi 配置复制轻松解决它

server {
    listen 80;

    root /opt/my/code/web;
    index app.php;
    charset utf-8;

    location / {
        try_files $uri $uri/ /app.php$is_args$args;
    }

    location ~ ^/recording {
        add_header Content-Type audio/x-wav;
        fastcgi_pass unix:/var/run/php-fpm.sock;
        include /etc/nginx/fastcgi_params;
        fastcgi_param SCRIPT_NAME /app.php;
        fastcgi_param SCRIPT_FILENAME $document_root/app.php;
    }    

    # pass the PHP scripts to php5-fpm
    location ~ ^/app\.php(/|$) {
        fastcgi_split_path_info ^(.+?\.php)(/?.*)$;
        fastcgi_pass unix:/var/run/php-fpm.sock;
        fastcgi_index app.php;
        include /etc/nginx/fastcgi_params;
        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;
    }
}

仅当您知道所有其他请求的内容类型时,第二个解决方案才有效。我们可以使用变量。 (顺便说一句,我不建议这个解决方案,因为更难支持而且不可爱:)

server {
    listen 80;

    root /opt/my/code/web;
    index app.php;
    charset utf-8;

    location / {
        try_files $uri $uri/ /app.php$is_args$args;
        set $ct "text/html";
    }

    location ~ ^/recording {
        try_files $uri $uri/ /app.php$is_args$args;
        set $ct "audio/x-wav";
    }    

    # pass the PHP scripts to php5-fpm
    location ~ ^/app\.php(/|$) {
        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;

        add_header "Content-Type $ct;

        # 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;
    }
}

【讨论】:

  • 第一个解决方案足够优雅,不是我喜欢重复,而是搞砸了。如果我在某些时候需要更多特殊处理,我还可以巧妙地使用位置匹配和堆栈位置块
  • 这几乎是 nginx 开发团队的官方观点:复制它!如果重复太多 - 使用包含或外部模板系统。可能是因为它有助于保持配置解析器更简单。我编辑了我的帖子,fastcgi_param 错误。
  • 如果它是官方的,我会说链接它,因为这是一件非常重要的事情。
  • 正如我所说,“几乎是官方的”,因为没有记录,而是基于 Igor Sysoev、Maxim Dounin 的 nginx 邮件列表中的许多答案。等等。很难链接它。
猜你喜欢
  • 2012-11-07
  • 1970-01-01
  • 1970-01-01
  • 2015-05-11
  • 2013-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-01
相关资源
最近更新 更多