php - Nginx 添加 headers PHP FPM 返回错误

标签 php nginx laravel-4 cors

我将 Laravel 4 与 Nginx 和 PHP-FPM 一起用于为应用程序提供服务。

然而,该应用程序实现了一个 API,并且我向 Nginx 添加了一些相当开放的 CORS 规则,这些规则似乎运行良好。

每当应用程序抛出错误时,Nginx 似乎不会将 header 添加为响应的一部分。有没有办法强制执行此操作而无需安装更多 header 扩展?

我的配置如下:

server {
    listen 80;
    server_name mediabase.local;
    root /home/vagrant/mediabase/public;

    index index.html index.htm index.php;

    charset utf-8;

    location / {
        try_files $uri $uri/ /index.php?$query_string;

    add_header Access-Control-Allow-Origin "*";
    add_header Access-Control-Allow-Methods "GET, OPTIONS, POST, HEAD, DELETE, PUT";
    add_header Access-Control-Allow-Headers "Authorization, X-Requested-With, Content-Type, Origin, Accept";
    add_header Access-Control-Allow-Credentials "true";
    add_header Access-Control-Max-Age: 86400;


    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    access_log off;
    error_log  /var/log/nginx/mediabase.local-error.log error;

    error_page 404 /index.php;

    sendfile off;

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;

    add_header Access-Control-Allow-Origin "*";
    add_header Access-Control-Allow-Methods "GET, OPTIONS, POST, HEAD, DELETE, PUT";
    add_header Access-Control-Allow-Headers "Authorization, X-Requested-With, Content-Type, Origin, Accept";
    add_header Access-Control-Allow-Credentials "true";
    add_header Access-Control-Max-Age: 86400;

    }

    location ~ /\.ht {
        deny all;
    }
}

最佳答案

Nginx 的 docs say那个add_header

Adds the specified field to a response header provided that the response code equals 200, 201, 204, 206, 301, 302, 303, 304, or 307.

作为 HttpHeadersMoreModule 的替代方案,您可以在 Laravel 中添加 header ,这是如何完成的:https://stackoverflow.com/a/17550224

关于php - Nginx 添加 headers PHP FPM 返回错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23945730/

相关文章:

python - 使用 gunicorn 和 nginx 部署 aiohttp.web 应用程序

laravel - 架构生成器 Laravel 4 中的大整数

php - 使用链接表值扩展模型。在cakephp3中

python - 如何在 Python 后台运行长时间运行的作业

tomcat - 请求上下文路径 grails spring security

cookies - Laravel 4 - 永远排队 cookie

php - 如何将此函数转换为laravel函数

php - 传递商店购物车数据...Ajax 还是 PHP Post?

php - 从多个表输出数据

php - 如何使用JQuery onclick将MySql中的随机内容加载到div中?