nginx下配置:
Header头设置
通过以下设置可有效防止XSS攻击
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
X-Frame-Options: 响应头表示是否允许浏览器加载frame等属性,有三个配置DENY
禁止任何网页被嵌入,SAMEORIGIN
只允许本网站的嵌套,ALLOW-FROM
允许指定地址的嵌套
X-XSS-Protection: 表示启用XSS过滤(禁用过滤为X-XSS-Protection: 0
),mode=block
表示若检查到XSS攻击则停止渲染页面
X-Content-Type-Options: 响应头用来指定浏览器对未指定或错误指定Content-Type
资源真正类型的猜测行为,nosniff 表示不允许任何猜测
在通常的请求响应中,浏览器会根据Content-Type
来分辨响应的类型,但当响应类型未指定或错误指定时,浏览会尝试启用MIME-sniffing来猜测资源的响应类型,这是非常危险的
例如一个.jpg的图片文件被恶意嵌入了可执行的js代码,在开启资源类型猜测的情况下,浏览器将执行嵌入的js代码,可能会有意想不到的后果
server {
# HTTPS 默认443端口
listen 443 ssl;
# 证书文件配置,指定证书的路径,除了证书路径其他配置都默认
ssl_certificate /usr/local/nginx/ssl/server.crt;
ssl_certificate_key /usr/local/nginx/ssl/server.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5:!DH;
# host
server_name example.com www.example.com;
#设置长连接
keepalive_timeout 70;
#减少点击劫持
add_header X-Frame-Options DENY;
#禁止服务器自动解析资源类型
add_header X-Content-Type-Options nosniff;
#防XSS攻击
add_header X-XSS-Protection "1; mode=block";
# 默认index
index index.html index.htm index.php default.html default.htm default.php;
# 代码的根目录
root /home/wwwroot/example;
# 访问日志
access_log /home/wwwlogs/example.com.log main;
# 文件的规则(详见http://seanlook.com/2015/05/17/nginx-location-rewrite/index.html)
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
expires 30d;
}
location ~ .*\.(js|css)?$ {
expires 12h;
}
}
tomcat下配置:
<filter>
<filter-name>httpHeaderSecurity</filter-name>
<filter-class>org.apache.catalina.filters.HttpHeaderSecurityFilter</filter-class>
<init-param>
<param-name>antiClickJackingOption</param-name>
<param-value>SAMEORIGIN</param-value>
</init-param>
<async-supported>true</async-supported>
</filter>
<filter-mapping>
<filter-name>httpHeaderSecurity</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>