(laravel,apache,nginx )响应头设置 (有关iframe嵌套,xss 攻击,防止基于 MIME 类型混淆的攻击)的配置

X-frame-options响应头。赋值有如下三种:
① DENY , 不能被嵌套如任何的iframe 或者这 frame
②SAMEORIGIN,页面只能被本站页面嵌入到iframe或者frame中。
③ALLOW-FROM uri:只能被嵌入到指定域名的框架中。
XSS 攻击防护,   X-XSS-Protection 它有几种配置:
0:禁用XSS保护;
1:启用XSS保护;
1; mode=block:启用XSS保护,并在检查到XSS攻击时,停止渲染页面(例如IE8中,检查到攻击时,整个页面会被一个#替换);
X-Content-Type-Options的安全配置
如果服务器发送响应头 "X-Content-Type-Options: nosniff",则 script 和 styleSheet 元素会拒绝包含错误的 MIME 类型的响应。这是一种安全功能,有助于防止基于 MIME 类型混淆的攻击。

 

nginx 配置
server {
        listen       80;
        server_name  nestle.com ;
        root   "D:\install\PHPTutorial\WWW\skscrm\dist";
        add_header X-Frame-Options SAMEORIGIN;
        add_header X-XSS-Protection '1; mode=block';
        add_header X-Content-Type-Options nosniff;

        location / {
            index  index.html index.htm index.php;
            #autoindex  on;
        }
        location ~ \.php(.*)$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  PATH_INFO  $fastcgi_path_info;
            fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
            include        fastcgi_params;
        }
}


apache 配置
#LoadModule headers_module modules/mod_headers.so
把#注释去掉改成
LoadModule headers_module modules/mod_headers.so
然后
在对应的vhost.conf添加以下配置或  者全局设置 直接在httpd.conf 加上对应的 Header配置
<VirtualHost *:80>
    DocumentRoot "D:\install\PHPTutorial\WWW\skscrm\dist"
   ServerName nestle.com
   ProxyPass /api/ http://nestle.com:801/api/
  <Directory "D:\install\PHPTutorial\WWW\skscrm\dist">
      Options FollowSymLinks ExecCGI
      AllowOverride All
      Order allow,deny
      Allow from all
     Require all granted
     Header always append X-Frame-Options SAMEORIGIN
     Header always append X-XSS-Protection '1; mode=block'
     Header always append X-Content-Type-Options nosniff

  </Directory>


laravel 框架 代码实现
配置一个全局中间件 在Kernel 注册对应中间件
protected $middleware = [
\App\Http\Middleware\FrameHeadersMiddleware::class
]
中间件中内容如下
$response = $next($request);
        $response->header('X-Frame-Options', 'SAMEORIGIN');
        $response->header('X-XSS-Protection', '1; mode=block');
        $response->header('X-Content-Type-Options', 'nosniff');
        return $response;

以上配置返回响应头效果如下

(laravel,apache,nginx )响应头设置 (有关iframe嵌套,xss 攻击,防止基于 MIME 类型混淆的攻击)的配置