Nginx服务器安全加固tips整理
公司各业务网站大多用到Nginx,花了点时间整理了一下Nginx服务器安全加固的各类tips。
默认配置文件和Nginx端口
/usr/local/nginx/conf/-Nginx配置文件目录,/usr/local/nginx/conf/nginx.conf是主配置文件
/usr/local/nginx/html/-默认网站文件位置
/usr/local/nginx/logs/-默认日志文件位置
Nginx HTTP默认端口:TCP 80
Nginx HTTPS默认端口:TCP 443
可以使用以下命令来测试Nginx配置文件准确性。
/usr/local/nginx/sbin/nginx -t
将会输出:
the configuration file /usr/local/nginx/conf/nginx.conf syntax is OK
configuration file /usr/local/nginx/conf/nginx.conf test is successful
执行以下命令来重新加载配置文件:
/usr/local/nginx/sbin/nginx -s reload
执行以下命令来停止服务器:
/usr/local/nginx/sbin/nginx -s stop
通过分区挂在允许最少特权
服务器上的网页/html/php文件单独分区。例如,新建一个分区/dev/sda5(第一逻辑分区),并且挂载在/nginx。确保/nginx是以noexec,nodev and nosetuid的权限挂载。
例:
LABAL=/nginx/nginx ext3 defaults,nosuid,noexec,nodev 1 2
注意:需要使用fdisk和mkfs.ext3命令创建一个新分区。
配置/etc/sysctl.conf强化Linux安全
可以通过编辑/etc/sysctl.conf来控制和配置Linux内核、网络设置。
#Avoid a smurf attack
net.ipv4.icmp_echo_ignore_broadcasts=1
#Turn on protection for bad icmp error messages
net.ipv4.icmp_ignore_bogus_error_responses=1
#Turn on syncookies for SYN flood attack protection
net.ipv4.tcp_syncookies=1
#Turn on and log spoofed,source routed,and redirect packets
net.ipv4.conf.all.log_martians=1
net.ipv4.conf.default.log_martians=1
#No source routed packets here
net.ipv4.conf.all.accept_source_route=0
net.ipv4.conf.default.accept_source_route=0
#Make sure no one can alter the routing tables
net.ipv4.conf.all.accept_redirects=0
net.ipv4.conf.default.accept_redirects=0
net.ipv4.conf.all.secure_redirects=0
net.ipv4.conf.default.secure_redirects=0
#Don't act as a router
net.ipv4.ip_forward=0
net.ipv4.conf.send_redirects=0
net.ipv4.conf.default.send_redirects=0
#Turn on execshild
kernel.exec-shield=1
kernel.randomize_va_space=1
删除所有不需要的Nginx模块
需要直接通过编译Nginx源代码使模块数量最少化。通过限制只允许web服务器访问模块把风险降到最低。
可以只配置安装nginx所需要的模块。例如,禁用SSL和autoindex模块可以执行以下命令:
./configure -without-http_autoindex_module -without-http_ssl_module
make
make install
通过以下命令来查看当编辑nginx服务器时哪个模块能开启或关闭:
./configure -help | less
禁用你用不到的nginx模块。
使用mod_security(只适合后端Apache服务器)
mod_security为Apache提供一个应用程序集的防火墙,为后续Apache Web服务器安装mod_security,这会阻止很多注入式攻击。
安装SELinux策略以强化Nginx Web服务器
默认的SELinux不会保护Nginx Web服务器,但是你可以安装和编译保护软件。
1.安装编译SELnux所需要环境支持
yum -y install selinux-policy-targeted selinux-policy-devel
2.下载SELinux策略以强化Nginx Web服务器。
cd /opt
wget http://downloads.sourseforge.net/project/selinuxnginx/se-ngix_1_0_10.tar.gz?use_mirror=nchc’
3.解压文件
tar -zxvf se-ngix_1_0_10.tar.gz
4.编译文件
cd se-ngix_1_0_10/nginx
make
rm tmp/nginx.mod.fc tmp/nginx.mod
5.安装生成的nginx.pp SELinux模块:
/usr/sbin/semodule -i nginx.pp
控制缓冲区溢出攻击
编辑nginx.conf,为所有客户端设置缓冲区的大小限制。
vi /usr/local/nginx/conf/nginx.conf
编辑和设置所有客户端缓冲区的大小限制如下:
##Start: Size Limits & Buffer Overflows ## //server上下文
client_body_buffer_size 1K;
client_header_buffer_size 1k;
client_max_body_size 1k;
large_client_header_buffers 2 1k;
##END:Size Limits & Buffer Overflows ##
控制并发连接
可以使用NginxHttpLimitZone模块来限制指定的会话或者一个IP地址的特殊情况下的并发连接。编辑nginx.conf:
### Directive describes the zone,in which the session states are stored i.e. stored in slimits. ###
### 1m can handle 32000 sessions with 32 bytes/session,set to 5m x 32000 session###
limit_zone slimits $binary_remote_addr 5m;
### Control maximum number of simultaneous connections for one session i.e.###
### restricts the amount of connections from a single ip address ###
limit_conn slimits 5;
上面标示线至每个远程IP地址的客户端同时打开连接不能超过5个。
只允许我们的域名的访问
如果机器人只是随机扫描服务器的所有域名,可以允许配置的虚拟域或反向代理请求,以拒绝这个请求。
##Only requests to our Host are allowed i.e. xxx.in,images.xxx.in and www.xxx.in
if($host !~ ^(xxx.in|www.xxx.in|images.xxx.in)$){
retrun 444;
}
##
限制可用的请求方法
GET和POST是最常用的方法。Web服务器的方法被定义在RFC 2616。如果Web服务器不要求启用所有可用的方法,它们应该被禁用。下面的指令将过滤只允许GET,HEAD和POST方法:
##Only allow these request methods##
if($request_method !~ ^(GET|HEAD|POST)$){
retrun 444;
}
##Do not accept DELETE,SEARCH and other methods##
如何拒绝一些User-Agents?
##Block download agents ##
if ($http_user_agent ~* LWP::Simple|BBBike|wget){
retrun 403;
}
##
组织Soso和有道的机器人:
##Block some robots ##
if ($http_user_agent ~* Sosospider|Yodaobot){
retrun 403;
}
目录限制
可以对指定的目录设置访问权限。所有的网站目录应该一一配置,只允许必须的目录访问权限。
可以通过IP地址来限制访问目录/admin/:
location /docs/ {
##block one workstation
deny 192.168.1.1;
## allow anyone in 192.168.1.0/24
allow 192.168.1.0/24;
##drop rest of the world
deny all;
}
通过密码保护目录
首先创建密码文件并增加“user”用户:
mkdir /usr/local/nginx/conf/.htpasswd/
htpasswd -c /usr/local/nginx/conf/.htpasswd/passwd user
编辑nginx.conf,加入需要保护的目录:
###Password Protect /personal-images/ and /delta/ directories###
location ~ /(personal-images/./delta/.){
auth_basic "Restricted";
auth_basic_user_file /usr/local/nginx/conf/.htpasswd/passwd;
}
密码文件生成后,也可以用以下的命令来增加允许访问的用户:
htpasswd -s /usr/local/nginx/conf/.htpasswd/passwd userName
Nginx SSL配置
HTTP是一个纯文本协议,它是开放的被动监测。应使用SSL来加密你的用户内容。
创建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
编辑nginx.conf并更新:
server{
server_name example.com;
listen 443;
ssl on;
ssl_certificate /user/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;
}
重启nginx:
/usr/local/nginx/sbin/nginx -s reload
在防火墙级限制每个IP的连接数
网络服务器必须监视连接和每秒连接限制。PF和Iptables都能够在进入nginx服务器之前阻止最终用户的访问。
Nginx服务器安全加固tips整理的更多相关文章
- nginx服务器入门知识汇总
IP-hash 就是根据IP进行hash计算,然后分配到对应的服务器,好处就是不用session同步,固定IP会固定访问一台服务器,缺点就是恶意攻击,会造成某台服务器压垮.提供的服务不同,面向的地区不 ...
- mac搭配Nginx服务器常见问题
推流服务器主要是使用了开源的nginx和rtmp模块,网上也有很多资料,不过对有些可能出现的问题没有很好的总结. 安装brew 使用Mac进行开发很久的老司机应该对这个工具很熟悉了.brew是一个第三 ...
- 一个小工具帮你搞定实时监控Nginx服务器
Linux运维工程师的首要职责就是保证业务7 x 24小时稳定的运行,监控Web服务器对于查看网站上发生的情况至关重要.关注最多的便是日志变动,查看实时日志文件变动大家第一反应应该是'tail -f ...
- Linux服务器安全加固
关于对公司网站服务器安全加固的一些想法及思路: 一.修改密码和ssh登录端口,并且尽可能的用密钥对登录,禁止用密码登录(主要针对Linux)二.修改/etc/hosts.allow 设置仅仅允许某几台 ...
- [转载20131024]Nginx服务器漏洞的利用和修复方法
本文主要分为两大部分,第一部分介绍了Nginx的一些常见安全漏洞的形成原因.利用方法,并给出了相应的解决办法;第二部分介绍了Nginx安全加固时需要关注的主要内容. Nginx(发音同engine x ...
- Nginx服务器性能优化与安全配置实践指南
转载自:https://www.bilibili.com/read/cv16151784?spm_id_from=333.999.0.0 1.引言 1.1 目的 为了更好的指导部署与测试艺术升系统ng ...
- Nginx 服务器 之Nginx与tomcat实现负载均衡
本文讲解我们如何使用Nginx做反向带服务器,实现nginx与tomcat服务器集群做负载均衡. 一.nginx与tomcat实现负载均衡 1.在/usr/local/ngnix/conf 创建 ...
- Nginx服务器 之反向代理与负载均衡
一.反向代理 正向代理: 客户端要获取的资源就在服务器上,客户端请求的资源路径就是最终响应资源的服务器路径,这就是正向代理.正向代理的特点:就是我们明确知道要访问哪个网站地址. 反向代理: 客户端想获 ...
- 把域名绑定到某个项目,以nginx服务器为例
一:登陆域名服务器平台,把域名解析到项目对应的IP上面. 二:配置nginx服务器 1./etc/nginx/conf.d/ 在服务器该目录下,添加.conf文件,如命名为:www.demo.com. ...
随机推荐
- .NET Core IdentityServer4实战 第Ⅳ章-集成密码登陆模式
回顾下ClientCredentials模式,在ReSourceApi中定义了我们公开服务,第三方网站想要去访问ReSourceApi则需要在身份验证服务中获取toekn,根据token的内容,硬编码 ...
- spring 5.x 系列第7篇 —— 整合Redis客户端 Jedis和Redisson (xml配置方式)
文章目录 一.说明 1.1 Redis 客户端说明 1.2 Redis可视化软件 1.3 项目结构说明 1.3 依赖说明 二.spring 整合 jedis 2.1 新建基本配置文件 2.2 单机配置 ...
- spring cloud 系列第4篇 —— feign 声明式服务调用 (F版本)
源码Gitub地址:https://github.com/heibaiying/spring-samples-for-all 一.feign 简介 在上一个用例中,我们使用ribbon+restTem ...
- 【linux杂记】Ubuntu查看端口使用情况
转载地址: https://www.linuxidc.com/Linux/2016-01/127345.htm Ubuntu查看端口使用情况,使用netstat命令: 查看已经连接的服务端口(ESTA ...
- 【ubuntu】软件安装与apt-get下载软件的存放位置
系统:Ubuntu16.04 常用的软件安装方式有两种: 第一种:apt-get(安装后略类似于windows中的安装版软件): 例:apt-get install ssh 1.下载的软件存放位置 / ...
- smarty 截取字符串
在视图中使用smarty来截取字符串的方法: {$array|truncate:15:"...":true} php:控制器中 $index['content ...
- redis 是如何做持久化的
Redis 是一个键值对数据库服务器.基于内存存储数据,它常被用做缓存数据库,用来替代 memcached.官网:https://redis.io/ 什么是持久化? 持久化,指将数据存储到可永久保存的 ...
- 提升——树形DP
这里讲提高一点的内容,所以没有树形DP基础的,先看一下基础部分: 浅说——树形DP 闲言不表,看第一题. 这道题是典型的树上最长链问题.(就是一个模板题) 给定一棵树,树上共有N个节点(N<=5 ...
- JS高级程序设计第2章--精简版
前言:这次是二刷了,想暑假做一次完整的笔记,但用本子来写笔记的话太贵了,可能哪天还丢了..所以还是博客好== 第二章:在HTML中使用JavaScript 2.1 <script>元素: ...
- HDU 5521:Meeting(最短路)
http://acm.hdu.edu.cn/showproblem.php?pid=5521 Meeting Problem Description Bessie and her friend E ...