add security headers in NGINX while using fastcgi caching

纵然是瞬间 提交于 2021-02-11 14:11:04

问题


I am using nginx with fastcgi cache. I want to use security headers on my site. I have already added add header field in my virtual host configurations but I can not get any headers unless I disable add_header X-fastcgi cache $upstream cache status in my fastcgi_main.conf file. virualhost file :

    }
   include /etc/nginx/bots.d/blockbots.conf;
   include /etc/nginx/bots.d/ddos.conf;
   include /etc/nginx/skip_cache.conf ;
   include /etc/nginx/purge_location.conf ;
   include /etc/nginx/gzip_location.conf ;
   include /etc/nginx/security_wp.conf;
        add_header Referrer-Policy 'origin';
        add_header "X-Frame-Options: sameorigin" always;
    location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass  unix:/var/run/php/php7.3-fpm.sock;
    include "/etc/nginx/customfastcgi" ;
    proxy_connect_timeout 300;
    proxy_send_timeout 300;
    proxy_read_timeout 300;
    send_timeout 300;
#    underscores_in_headers on;
client_max_body_size 256M;
    include /etc/nginx/fastcgi_main.conf ;
    }

}

FASTCGI_main.conf

        fastcgi_no_cache $skip_cache;
        fastcgi_cache phpcache;
        fastcgi_cache_valid 200 1m;
        fastcgi_cache_valid 301 1m;
        fastcgi_cache_valid 302 1m;
        fastcgi_cache_valid 307 1m;
        fastcgi_cache_valid 404 1m;
        fastcgi_cache_use_stale error timeout invalid_header http_500 http_503;
        fastcgi_cache_min_uses 1;
        fastcgi_cache_methods GET HEAD;
        fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
       add_header X-FastCGI-Cache $upstream_cache_status;
    ```

RESULT:
curl -I https://dev-kuhicbury.$domain
HTTP/1.1 200 OK
Server: nginx/1.10.3 (Ubuntu)
Date: Fri, 09 Oct 2020 11:39:35 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
Vary: Accept-Encoding
rel="https://api.w.org/"
X-FastCGI-Cache: HIT

回答1:


You've stepped onto a very common configuration pitfall of the add_header directive. Similar to all other array-like directives in NGINX, it is only inherited, if there is no other add_header in the current context.

The typical solution is to copy-paste (through inevitable duplication), the desired headers in a specific location:

In FASTCGI_main.conf:

        fastcgi_no_cache $skip_cache;
        fastcgi_cache phpcache;
        fastcgi_cache_valid 200 1m;
        fastcgi_cache_valid 301 1m;
        fastcgi_cache_valid 302 1m;
        fastcgi_cache_valid 307 1m;
        fastcgi_cache_valid 404 1m;
        fastcgi_cache_use_stale error timeout invalid_header http_500 http_503;
        fastcgi_cache_min_uses 1;
        fastcgi_cache_methods GET HEAD;
        fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
        add_header X-FastCGI-Cache $upstream_cache_status;
        add_header Referrer-Policy 'origin';
        add_header "X-Frame-Options: sameorigin" always;

This unintuitive behavior of NGINX has been a source of trouble for many.

Here are some modules of interest, which address the same issue (as in, "better add_header"):

  • ngx_headers_more
  • ngx_security_headers, more applicable to your case


来源:https://stackoverflow.com/questions/64279365/add-security-headers-in-nginx-while-using-fastcgi-caching

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!