nginx是一个轻量级的,高性能的web服务器以及反向代理和邮箱代理服务器。它运行在unix、gnu、linux、bsd、mac os x、solaris和windows各种版本。
根据调查统计数据显示,当前全球超过6%的网站使用nginx web服务器来管理web网站应用。
为了保证基于nginx的web应用安全, 我结合之前在项目中遇到的相应问题进行总结和提炼,针对nginx常用安全配置进行梳理。
一、基础安全配置
隐藏版本信息:编辑 nginx 源码中的 /http/ngx_http_header_filter_module.c 文件,找到以下两行并修改为自定义内容:
static char ngx_http_server_string[] = “server: ninja web server” ; static char ngx_http_server_full_string[] = “server: ninja web server” ;
然后重新编译安装 nginx。
限制模块加载:在编译 nginx 时,禁用不必要的模块,例如:
./configure -without-http_autoindex_module -without-http_ssi_module make make install
限制并发连接:在 nginx.conf 文件中,使用 limit_zone 和 limit_conn 指令限制每个 ip 的并发连接数:
limit_zone slimits $binary_remote_addr 5m; limit_conn slimits 5;
限制访问域名:在 nginx.conf 文件中,添加以下配置,只允许特定域名的请求:
if ($host !~ ^(guoyu.city|www.guoyu.city|images.guoyu.city)$ ) {
return 444;
}
目录访问限制:
通过 ip 地址限制访问:
location /docs/ {
deny 192.168.1.1;
allow 192.168.1.0/24;
deny all;
}
通过密码保护目录:
mkdir /usr/local/nginx/conf/.htpasswd/ htpasswd -c /usr/local/nginx/conf/.htpasswd/passwd user
- 在
nginx.conf中添加:
location ~ /(personal-images/.|delta/.) {
auth_basic “restricted”;
auth_basic_user_file /usr/local/nginx/conf/.htpasswd/passwd;
}
防止目录遍历:在 nginx.conf 文件中,设置 autoindex off 来防止目录遍历攻击:
location / {
autoindex off;
}
过滤常见xss攻击: 过滤常见的 xss 攻击字符, xss 攻击常常涉及将<script>标签或类似的特殊字符(如 <>)作为查询参数的一部分。你可以通过正则表达式来阻止这些非法字符的访问。
server {
listen 80;
server_name your-domain.com;
# 屏蔽包含 <script> 或类似的 xss 攻击字符的查询字符串
location / {
if ($query_string ~* "<script|%3cscript|%3e|%3c|alert|eval|javascript|<|>") {
return 403; # 返回 http 403 禁止访问
}
# 其他配置...
}
# 其他配置...
}
除了 <script>,你还可以过滤其他可能用于 xss 攻击的字符,比如 javascript 关键字、url 编码后的字符(如 %3c 为 <,%3e 为 >),甚至是尝试使用 eval() 或 alert() 等 javascript 函数。
server {
listen 80;
server_name your-domain.com;
# 屏蔽含有危险字符或关键字的请求
location / {
if ($query_string ~* "<script|%3cscript|%3e|%3c|alert|eval|javascript|<|>") {
return 403; # 返回 http 403 禁止访问
}
# 其他配置...
}
# 其他配置...
}
设置http头: 虽然 x-xss-protection 响应头在现代浏览器中已被弃用,但它仍然可以在一些旧版本浏览器中提供一定的保护。可在location规则下添加相应的配置信息。
add_header x-xss-protection "1; mode=block";
设置 content-type options: 阻止浏览器从执行由服务器推断出的 mime 类型,而非服务器声明的内容类型。可在location规则下添加相应的配置信息。
add_header x-content-type-options nosniff;
限制请求大小 :通过限制请求的大小,可以减少大型恶意载荷的风险。可在location规则下添加相应的配置信息。
# 限制请求大小为10kb client_max_body_size 10k;
实施内容安全策略(csp): csp 是一个额外的安全层,可以帮助检测和减轻某些类型的攻击,包括 xss 和数据注入攻击。可在location规则下添加相应的配置信息。
add_header content-security-policy "default-src 'self'; script-src 'self' https://apis.guoyu.city";
二、ssl/tls 配置
生成 ssl 证书:
cd /usr/local/nginx/conf openssl genrsa -des3 -out server.key 1024 openssl req -new -key server.key -out server.csr cp server.key server.key.org openssl rsa -in server.key.org -out server.key openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
配置 ssl:在 nginx.conf 文件中,添加以下配置:
server {
server_name project.guoyu.city;
listen 443;
ssl on;
ssl_certificate /usr/local/nginx/conf/server.crt;
ssl_certificate_key /usr/local/nginx/conf/server.key;
access_log /usr/local/nginx/logs/ssl.access.log;
error_log /usr/local/nginx/logs/ssl.error.log;
}
配置 https 重定向:在 nginx.conf 文件中,添加以下配置,将所有 http 请求重定向到 https:
server {
listen 80;
server_name example.com;
return 301 https://$host$request_uri;
}
配置 ssl 参数:在 nginx.conf 文件中,添加以下配置,提高 ssl 安全性:
ssl_session_cache shared:ssl:1m; ssl_session_timeout 5m; ssl_ciphers ecdhe-rsa-aes128-gcm-sha256:ecdhe:ecdh:aes:high:!null:!anull:!md5:!adh:!rc4; ssl_protocols tlsv1.1 tlsv1.2 tlsv1.3; ssl_prefer_server_ciphers on;
使用 let’s encrypt 自动续期证书:
安装 certbot 和 nginx 插件:
sudo dnf install certbot python3-certbot-nginx -y
使用 certbot 申请 ssl 证书并自动配置 nginx:
sudo certbot --nginx -d your_domain.com -d www.your_domain.com
配置定时任务,每天凌晨 2 点自动检查并续期证书:
sudo crontab -e 0 2 * * * /usr/bin/certbot renew --quiet --post-hook "systemctl reload nginx"
三、其他安全配置
安装 selinux 策略:安装和编译 selinux 策略以强化 nginx web 服务器:
yum -y install selinux-policy-targeted selinux-policy-devel cd /opt wget http://downloads.sourceforge.net/project/selinuxnginx/se-ngix_1_0_10.tar.gz?use_mirror=nchc&rsquo tar -zxvf se-ngix_1_0_10.tar.gz cd se-ngix_1_0_10/nginx make /usr/sbin/semodule -i nginx.pp
限制 nginx 连接传出:使用 iptables 限制 nginx 用户的传出连接:
/sbin/iptables -a output -o eth0 -m owner -uid-owner nginx -p tcp -dport 80 -m state -state new,established -j accept
配置操作系统保护:正确设置 /nginx 文档根目录的权限,确保 nginx 以非 root 用户运行:
find /nginx -user nginx find /usr/local/nginx/html -user nginx ls -l /usr/local/nginx/html/
限制每个 ip 的连接数:在防火墙级别限制每个 ip 的连接数:
/sbin/iptables -a input -p tcp -dport 80 -i eth0 -m state -state new -m recent -set /sbin/iptables -a input -p tcp -dport 80 -i eth0 -m state -state new -m recent -update -seconds 60 -hitcount 15 -j drop service iptables save
通过以上的安全配置,可以大大提高 nginx 服务器的安全性,减少网站的安全威胁。
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论