这是我的nginx conf:
upstream django {
server 127.0.0.1:8001;
}
server {
listen 80;
server_name 127.0.0.1;
charset utf-8;
#security
add_header X-Content-Type-Options "nosniff";
add_header X-Frame-Options "DENY";
add_header X-XSS-Protection "1; mode=block";
location /static {
alias /usr/share/nginx/mysite/staticfiles;
}
location / {
uwsgi_pass django;
include /etc/nginx/uwsgi_params;
}
}
我的settings.py:
SECURE_BROWSER_XSS_FILTER = True
SECURE_CONTENT_TYPE_NOSNIFF = True
SESSION_COOKIE_SECURE = True
SECURE_FRAME_DENY = True
但它仍然存在静态文件的安全问题,我还应该设置什么?
更新
我发现@LinnTroll说,似乎nginx 403页面没有安全保护。我该怎么设置?
答案 0 :(得分:0)
这不是django问题。 Nginx返回错误代码(403)以直接访问类似/ static /的路径,而指令add_header不适用于具有错误代码的响应。如果您使用nginx版本> = 1.7.5,请将第三个参数'always'添加到add_header指令:
add_header X-Content-Type-Options "nosniff" always;
如果您的nginx版本低于1.7.5,我知道以下简单的方法来解决您的问题:
autoindex on;
。 (警告:所有静态文件都可供任何人使用,包括搜索引擎!)在您/静态位置之前添加以下位置:
location ~* ^/static.*/$ { add_header X-Content-Type-Options "nosniff"; # ... you other headers ... add_header 'Content-Type' 'text/html; charset=UTF-8'; return 200; }