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

nginx - Security headers within location block?

· 52CE 我爱测

I have been testing the security headers ( https://securityheaders.com ) of my nginx setup and wanted to check peoples opinion with nginx suffix location blocks. Currently, I get 'A+' for http(s)://my.site however, 'B' when testi…

问题原文

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

I have been testing the security headers (https://securityheaders.com) of my nginx setup and wanted to check peoples opinion with nginx suffix location blocks.

Currently, I get 'A+' for http(s)://my.site however, 'B' when testing a suffix location ie https://my.site/location1

The warnings are for missing:-

  • Content-Security-Policy
  • Referrer-Policy
  • Feature-Policy

My server block which receives 'A+' consists of:-

    add_header 'Referrer-Policy' 'no-referrer';
    add_header Strict-Transport-Security "max-age=15552000; preload" always;
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header X-XSS-Protection "1; mode=block" always;
    add_header Feature-Policy "geolocation none;midi none;notifications none;push none;sync-xhr none;microphone none;camera none;magnetometer none;gyroscope none;;speaker self;vibrate none;fullscreen self;payment none;";
    add_header Content-Security-Policy "frame-ancestors my.site;";

an example location block that receives 'B' consists of:-

location /location1 {
    proxy_pass              http://192.168.1.1;
    proxy_set_header        X-Real-IP         $remote_addr;
    proxy_set_header        Host              $host;
    proxy_set_header        X-Forwarded-For   $proxy_add_x_forwarded_for;
    proxy_set_header        X-Forwarded-Proto $scheme;
    proxy_redirect          off;
    proxy_buffering         off;
    auth_basic              "Restricted";
    auth_basic_user_file    /etc/nginx/.htpasswd;
    error_log               /var/log/nginx/blah.error.log;

I have played around with adding the CSP within the location block, however if ths is the way - I must be getting the syntax wrong.

Are the security headers intended to cascade into location blocks? Or is the result of the scan expected? Or am I just noobing...

Cheers, Jonny

已采纳解答

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

The headers that are reported as being missing are lacking an always directive. I'm guessing that whatever is being tested against isn't returning one of the response codes that add_header wants in order to return the other headers.

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

资料来源与适用范围

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

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

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