大网数据广告活动
服务器运维问答

How to override Nginx response headers in PHP

· 52CE 我爱测

I want to apply a default CSP header in nginx - basically a catch all. However, I also want to be able to override the CSP header via PHP in certain scripts. So far I've been unable to find a way to do this and the result is I ge…

问题原文

原帖链接 · Praemon · CC BY-SA 4.0

I want to apply a default CSP header in nginx - basically a catch all. However, I also want to be able to override the CSP header via PHP in certain scripts. So far I've been unable to find a way to do this and the result is I get 2 headers - the first from PHP and the second from Nginx, with the last header taking preference over the first.

In nginx I have:

add_header Content-Security-Policy "default-src 'self';";

Then there's a catch for the PHP files:

location ~ [^/]\.php(/|$) {
        fastcgi_split_path_info ^(.+?\.php)(|/.*)$;
        fastcgi_pass 127.0.0.1:9000;
        include fastcgi_params;
        fastcgi_param HTTP_PROXY "";
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param QUERY_STRING $query_string;
        fastcgi_intercept_errors on;
        fastcgi_connect_timeout         300; 
        fastcgi_send_timeout           3600; 
        fastcgi_read_timeout           3600;
        fastcgi_buffer_size 128k;
        fastcgi_buffers 4 256k;
        fastcgi_busy_buffers_size 256k;
        fastcgi_temp_file_write_size 256k;
    }

I've tried playing around with fastcgi_param to see if I can get the header back from PHP and only add_header if not set, as well as trying:

    if ($http_content_security_policy = "") {
        add_header Content-Security-Policy "default-src 'self';";
    }

But so far not winning. I assume it's a timing or access issue in Nginx in terms of the PHP header values? Any help is appreciated.

Note: I'm using Azure web apps, so I don't think I can add any modules that aren't default for Nginx

已采纳解答

原帖链接 · Richard Smith · CC BY-SA 4.0

The add_header directive will not generate a header if the value provided is an empty string.

You can provide various values to the add_header statement by using a map block. See map directive documentation.

The header returned by the PHP script is available as $upstream_http_content_security_policy.

So the solution is to test the value returned by the PHP script using a map block, and set the default value only if the script returns no header.

For example:

map $upstream_http_content_security_policy $csp_value {
    ""  "default-src 'self';";
}
add_header Content-Security-Policy $csp_value;

The default header for Content-Security-Policy is only inserted if the $upstream_http_content_security_policy variable is empty (meaning that the script did not provide an alternative value).

Note that the map block must be placed outside of the server block. The add_header statement can remain where you currently define it.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2023-01-13)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。