# Nginx 服务安全加固

#### 博客作者：联系请[点击](https://hezhiqiang.gitbook.io/about-the-author/lian-xi-zuo-zhe)，搬运不易，希望请作者喝咖啡，可以点击[联系博客作者](https://hezhiqiang.gitbook.io/about-the-author/lian-xi-zuo-zhe)

## **一、 普通用户启动服务**

**名称：**&#x4EE5;非特权用户启动服务

**实施目的：**&#x4EE5;特定用户运行服务,不要使用系统管理员帐号启动Nginx。

**问题影响：**&#x8D8A;权使用造成非法攻击

**系统当前状态：**&#x4F7F;用系统命令或使工具查看Nginx进程是否使用管理员或使特权用户启动的。

```
User nginx;
Worker_processes 8;
```

**实施步骤：**&#x4E00;般情况下，Nginx是由Root来安装和运行的。如果Nginx：worker进程具有Root用户特权，那么它将给系统的安全构成很大的威胁，应确保Nginx进程以最可能低的权限用户来运行。

通过修改nginx.conf 文件中的下列选项，以nginx用户运行worker达到相对安全的目的。

1. 备份 `nginx.conf`文件
2. 取消nginx 用户对所有目录的的读取权限，然后添加对网站目录的读取权限

```
chmod o-r –R /
chmod o+r –R html/
```

&#x20;    3\. 取消 nginx 对于/bin/sh 的执行权限

```
chmod 776 /bin/sh
```

&#x20;     4\. 重启nginx

## **二、限定非法主机访问**

**名称：**&#x63A7;制哪些主机能够访问服务器

**实施目的：**&#x9632;止恶意攻击

**问题影响：**&#x975E;法访问

**系统当前状态：**&#x901A;过查看nginx.conf配置文件确定系统当前状态

**实施步骤：**&#x5728;nginx配置文件中强制限定允许某个网段或者某个IP接入服务器。

```
location /
{
deny 192.168.1.1;
 allow 192.168.1.0/24;
 allow 10.1.1.0/16;
 deny all;
}
```

## **三、限定非法文件访问**

**名称：**&#x9650;定非法文件访问

**实施目的：**&#x7981;止nginx访问某类型的文件。

**问题影响：**&#x975E;法访问，恶意攻击。

**实施步骤：**&#x7F16;辑nginx.conf配置文件

如，Nginx下禁止访问\*.txt文件

```
location ~* \.(txt|doc)${
root /var/InfiNET/web /test;
deny all;
}
禁止下载以点开头的文件
location ~ /\..+ {
deny all;
}
```

## **四、限定非法目录访问**

**名称：**&#x9650;定非法目录访问

**实施目的：**&#x7981;止nginx访问Web目录之外的任何文件。的IP地址等内容。

**问题影响：**&#x975E;法访问，恶意攻击。

**实施步骤：**

```
location ~ ^/(WEB-INF)/
{deny all;
}
```

**备注：**

1、判定条件

无法访问Web目录之外的文件。

2、检测操作

访问服务器上不属于Web目录的一个文件，结果应无法显示。

## **五、修改默认服务端口**

**名称：**&#x66F4;改默认端口

**实施目的：**&#x66F4;改nginx服务器默认端口，防止非法访问。

**问题影响：**&#x6076;意攻击。

**实施步骤：**

（1）修改nginx.conf配置文件，更改默认端口到6666。

```
server{
listen 6666 ;
server_namelocalhost;
```

（2）重启Nginx服务

**备注：**

1、判定条件

使用 6666 端口页面成功

2、检测操作

登陆<http://ip:6666>

## **六、隐藏服务器版本号**

**名称：**&#x9690;藏服务器版本号

**实施目的：**&#x9690;藏服务器版本号，防备Nginx的版本漏洞被扫描到并利用

**问题影响：**&#x6076;意攻击。

**实施步骤：**

（1）修改 nginx.conf 配置文件，增加&#x20;

```
server_tokens off http {
server_tokens off;
```

（2）重启 Nginx 服务

**备注：**&#x8BBF;问一个不存在的页面，错误页面404 NotFound 中不显示nginx版本号

## **八、控制缓冲区溢出攻击**

**名称：**&#x63A7;制缓冲区溢出攻击

**实施目的：**&#x63A7;制缓冲区大小，防备溢出攻击

**问题影响：**&#x6076;意攻击。

**实施步骤：**

1、参考配置操作

（1）修改 nginx.conf 配置文件，修改缓冲区大小

```
## Start: Size Limits & Buffer Overflows ## client_body_buffer_size1K; client_header_buffer_size 1k; client_max_body_size 1k; large_client_header_buffers 2 1k;

## END: Size Limits & Buffer Overflows ##
```

（2）重启 Nginx 服务

## **九、控制并发连接数**

**名称：**&#x63A7;制并发连接数

**实施目的：**&#x63A7;制并发连接数，防备Ddos攻击

**问题影响：**&#x6076;意攻击。

**实施步骤：**

（1）修改 nginx.conf 配置文件，修改单个 IP 的并发连接数大小

```
### 1m can handle 32000 sessions limit_zone slimits $binary_remote_addr 5m; limit_conn slimits 64;
```

上面表示限制每个远程 IP地址的客户端同时打开连接不能超过64个。

（2）重启 Nginx 服务

## **十、限制可用的请求方法**

**名称：**&#x9650;制可用的请求方法

**实施目的：**&#x9650;制可用的请求方法，防备利用应用漏洞攻击

**问题影响：**&#x6076;意攻击。

**实施步骤：**

（1）下面的指令将过滤只允许 GET，HEAD 和 POST 方法：

```
## Only allow these request methods ##
if($request_method !~ ^(GET|HEAD|POST)$ ) {
return444;
}
```

（2）重启 Nginx 服务

## **十一、漏洞补丁**

**名称：**&#x8865;丁修复

**实施目的：**&#x5347;级Nginx修复漏洞

**问题影响：**&#x5BB9;易引起恶意攻击。

**系统当前状态：**&#x4F7F;用`nginx -V`查看版本。

**实施步骤：**&#x5230;[Nginx 官网](https://nginx.org/en/security_advisories.html) 查看公开的Nginx安全漏洞。

**备注：**&#x5347;级补丁的风险极高，如当前版本没有漏洞不建议升级

## **十一、操作系统配置**

**名称：**&#x64CD;作系统配置

**实施目的：**&#x914D;置操作系统保护Web服务器

**问题影响：**&#x63A7;制攻击危害

**系统当前状态：**&#x67E5;看防火墙、用户权限以及Selinux配置

**实施步骤：**

1、Nginx以用户nginx运 行 ， 但是根目录（/nginx或者/usr/local/ nginx/html）不应该设置属于用户 nginx或对用户nginx可写。

2、配置操作系统防火墙限制，下面的例子中只允许 user 用户在外面使用80连接，如果某个IP在60秒尝试连接到80端口的次数超过了15， iptables将会丢掉来自它的入站连接。

```
iptables -A OUTPUT -o eth0 -m owner --uid-owner vivek -p tcp --dport 80 -m state --state NEW,ESTABLISHED-j ACCEPT
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
```

3、配置SELinux策略加固Nginx ，从[项目主页](https://sourceforge.net/projects/selinuxnginx/)下载 SELinux策略包并安装，用于强化 Linux 服务器上的 nginx 网络服务器的针对性 SELinux 策略。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://hezhiqiang.gitbook.io/about-the-author/xi-tong-an-quan-jia-gu/nginx-fu-wu-an-quan-jia-gu.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
