|
|
|
|
# Nginx核心配置文件结构
|
|
|
|
|
- 配置文件一般目录 `/usr/local/nginx/conf/nginx.conf`
|
|
|
|
|
- Nginx的配置文件中可以使用`#`来注释
|
|
|
|
|
|
|
|
|
|
```editorconfig
|
|
|
|
|
# 全局块,主要设置Nginx服务器整体运行的配置指令
|
|
|
|
|
指令名 指令值;
|
|
|
|
|
|
|
|
|
|
#events块,主要设置,Nginx服务器与用户的网络连接,这一部分对Nginx服务器的性能影响较大
|
|
|
|
|
events {
|
|
|
|
|
指令名 指令值;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#http块,是Nginx服务器配置中的重要部分,代理、缓存、日志记录、第三方模块配置...
|
|
|
|
|
http {
|
|
|
|
|
指令名 指令值;
|
|
|
|
|
# server块,是Nginx配置和虚拟主机相关的内容
|
|
|
|
|
server {
|
|
|
|
|
指令名 指令值;
|
|
|
|
|
# location块,基于Nginx服务器接收请求字符串与location后面的值进行匹配,对特定请求进行处理
|
|
|
|
|
location / {
|
|
|
|
|
指令名 指令值;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
...
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
- nginx.conf配置文件中默认有三大块:**全局块、events块、http块**
|
|
|
|
|
- http块中可以配置多个server块,每个server块又可以配置多个location块。
|
|
|
|
|
|
|
|
|
|
## 1. 全局块
|
|
|
|
|
|
|
|
|
|
### 1.1 user指令
|
|
|
|
|
- user:**用于配置运行Nginx服务器的worker进程的用户和用户组**。
|
|
|
|
|
|
|
|
|
|
| 语法 | user user [group] |
|
|
|
|
|
|:---:|:-----------------:|
|
|
|
|
|
| 默认值 | nobody |
|
|
|
|
|
| 位置 | 全局块 |
|
|
|
|
|
- 该属性也可以在编译的时候指定,语法如下`./configure --user=user --group=group`,如果两个地方都进行了设置,最终生效的是配置文件中的配置。
|
|
|
|
|
- 该指令的使用步骤:
|
|
|
|
|
- (1)设置一个用户信息"www"
|
|
|
|
|
```editorconfig
|
|
|
|
|
user test;
|
|
|
|
|
```
|
|
|
|
|
- (2) 创建一个用户
|
|
|
|
|
```editorconfig
|
|
|
|
|
useradd test
|
|
|
|
|
```
|
|
|
|
|
- (3)修改user属性
|
|
|
|
|
```editorconfig
|
|
|
|
|
user www
|
|
|
|
|
```
|
|
|
|
|
- (4)创建`/root/html/index.html`页面,添加如下内容
|
|
|
|
|
```html
|
|
|
|
|
<!DOCTYPE html>
|
|
|
|
|
<html>
|
|
|
|
|
<head>
|
|
|
|
|
<title>Welcome to nginx!</title>
|
|
|
|
|
<style>
|
|
|
|
|
body {
|
|
|
|
|
width: 35em;
|
|
|
|
|
margin: 0 auto;
|
|
|
|
|
font-family: Tahoma, Verdana, Arial, sans-serif;
|
|
|
|
|
}
|
|
|
|
|
</style>
|
|
|
|
|
</head>
|
|
|
|
|
<body>
|
|
|
|
|
<h1>Welcome to nginx!</h1>
|
|
|
|
|
<p>If you see this page, the nginx web server is successfully installed and
|
|
|
|
|
working. Further configuration is required.</p>
|
|
|
|
|
|
|
|
|
|
<p>For online documentation and support please refer to
|
|
|
|
|
<a href="http://nginx.org/">nginx.org</a>.<br/>
|
|
|
|
|
Commercial support is available at
|
|
|
|
|
<a href="http://nginx.com/">nginx.com</a>.</p>
|
|
|
|
|
|
|
|
|
|
<p><em>Thank you for using nginx.</em></p>
|
|
|
|
|
<p><em>I am WWW</em></p>
|
|
|
|
|
</body>
|
|
|
|
|
</html>
|
|
|
|
|
```
|
|
|
|
|
- (5)修改nginx.conf
|
|
|
|
|
```editorconfig
|
|
|
|
|
location / {
|
|
|
|
|
root /root/html;
|
|
|
|
|
index index.html index.htm;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
- (5)测试启动访问
|
|
|
|
|
- 页面会报403拒绝访问的错误
|
|
|
|
|
- (6)分析原因
|
|
|
|
|
- 因为当前用户没有访问/root/html目录的权限
|
|
|
|
|
- (7)将文件创建到 `/home/www/html/index.html`,修改配置
|
|
|
|
|
```editorconfig
|
|
|
|
|
location / {
|
|
|
|
|
root /home/www/html;
|
|
|
|
|
index index.html index.htm;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
- (8)再次测试启动访问,访问正常
|
|
|
|
|
- **使用user指令可以指定启动运行工作进程的用户及用户组,这样对于系统的权限访问控制的更加精细,也更加安全**。
|
|
|
|
|
|
|
|
|
|
### 1.2 work process指令
|
|
|
|
|
- **master_process:用来指定是否开启工作进程**。
|
|
|
|
|
|
|
|
|
|
| 语法 | master_process on\off; |
|
|
|
|
|
|:---:|:----------------------:|
|
|
|
|
|
| 默认值 | master_process on; |
|
|
|
|
|
| 位置 | 全局块 |
|
|
|
|
|
|
|
|
|
|
- **worker_processes:用于配置Nginx生成工作进程的数量,这个是Nginx服务器实现并发处理服务的关键所在**。
|
|
|
|
|
- 理论上来说workder process的值越大,可以支持的并发处理量也越多,但事实上这个值的设定是需要受到来自服务器自身的限制,
|
|
|
|
|
建议将该值和服务器CPU的内核数保存一致。
|
|
|
|
|
|
|
|
|
|
| 语法 | worker_processes num/auto; |
|
|
|
|
|
|-----|--------------------------------|
|
|
|
|
|
| 默认值 | 1 |
|
|
|
|
|
| 位置 | 全局块 |
|
|
|
|
|
|
|
|
|
|
- 如果将worker_processes设置成2,则会看到如下内容:
|
|
|
|
|
- ![orker_processes-2](pic/orker_processes-2.png)
|
|
|
|
|
|
|
|
|
|
### 1.3 其他指令
|
|
|
|
|
- **daemon:设定Nginx是否以守护进程的方式启动**。
|
|
|
|
|
- 守护式进程是linux后台执行的一种服务进程,特点是独立于控制终端,不会随着终端关闭而停止。
|
|
|
|
|
|
|
|
|
|
| 语法 | daemon on\off; |
|
|
|
|
|
|-----|----------------|
|
|
|
|
|
| 默认值 | daemon on; |
|
|
|
|
|
| 位置 | 全局块 |
|
|
|
|
|
|
|
|
|
|
- **pid:用来配置Nginx当前master进程的进程号ID存储的文件路径**。
|
|
|
|
|
|
|
|
|
|
| 语法 | pid file; |
|
|
|
|
|
|-----|-------------------------------------|
|
|
|
|
|
| 默认值 | 默认为:/usr/local/nginx/logs/nginx.pid |
|
|
|
|
|
| 位置 | 全局块 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- 该属性可以通过`./configure --pid-path=PATH`来指定
|
|
|
|
|
- error_log:用来配置Nginx的错误日志存放路径
|
|
|
|
|
|
|
|
|
|
| 语法 | error_log file [日志级别]; |
|
|
|
|
|
|-----|---------------------------------|
|
|
|
|
|
| 默认值 | error_log logs/error.log error; |
|
|
|
|
|
| 位置 | 全局块、http、server、location |
|
|
|
|
|
|
|
|
|
|
- 该属性可以通过`./configure --error-log-path=PATH`来指定
|
|
|
|
|
- 其中日志级别的值有:debug|info|notice|warn|error|crit|alert|emerg,翻译过来为试|信息|通知|警告|错误|临界|警报|紧急,
|
|
|
|
|
**这块建议大家设置的时候不要设置成info以下的等级,因为会带来大量的磁盘I/O消耗,影响Nginx的性能**。
|
|
|
|
|
|
|
|
|
|
- **include:用来引入其他配置文件,使Nginx的配置更加灵活**
|
|
|
|
|
|
|
|
|
|
| 语法 | include file; |
|
|
|
|
|
|-----|---------------|
|
|
|
|
|
| 默认值 | 无 |
|
|
|
|
|
| 位置 | any |
|
|
|
|
|
|
|
|
|
|
### 1.4 events块
|
|
|
|
|
- (1)accept_mutex:用来设置Nginx网络连接序列化
|
|
|
|
|
|
|
|
|
|
| 语法 | accept_mutex on\off; |
|
|
|
|
|
|-----|----------------------|
|
|
|
|
|
| 默认值 | accept_mutex on; |
|
|
|
|
|
| 位置 | events |
|
|
|
|
|
|
|
|
|
|
- 这个配置主要可以用来解决常说的"惊群"问题。大致意思是在某一个时刻,客户端发来一个请求连接,Nginx后台是以多进程的工作模式,
|
|
|
|
|
也就是说有多个worker进程会被同时唤醒,但是最终只会有一个进程可以获取到连接,如果每次唤醒的进程数目太多,就会影响Nginx的整体性能。
|
|
|
|
|
如果将上述值设置为on(开启状态),将会对多个Nginx进程接收连接进行序列号,一个个来唤醒接收,就防止了多个进程对连接的争抢。
|
|
|
|
|
|
|
|
|
|
(2)multi_accept:用来设置是否允许同时接收多个网络连接
|
|
|
|
|
|
|
|
|
|
| 语法 | multi_accept on\off; |
|
|
|
|
|
|-----|----------------------|
|
|
|
|
|
| 默认值 | multi_accept off; |
|
|
|
|
|
| 位置 | events |
|
|
|
|
|
|
|
|
|
|
- 如果multi_accept被禁止了,nginx一个工作进程只能同时接受一个新的连接。否则,一个工作进程可以同时接受所有的新连接
|
|
|
|
|
|
|
|
|
|
- (3)worker_connections:用来配置单个worker进程最大的连接数
|
|
|
|
|
|
|
|
|
|
| 语法 | worker_connections number; |
|
|
|
|
|
|-----|----------------------------|
|
|
|
|
|
| 默认值 | worker_commections 512; |
|
|
|
|
|
| 位置 | events |
|
|
|
|
|
|
|
|
|
|
- 这里的连接数不仅仅包括和前端用户建立的连接数,而是包括所有可能的连接数。另外,**number值不能大于操作系统支持打开的最大文件句柄数量**。
|
|
|
|
|
|
|
|
|
|
- (4)use:用来设置Nginx服务器选择哪种事件驱动来处理网络消息。
|
|
|
|
|
|
|
|
|
|
| 语法 | use method; |
|
|
|
|
|
|-----|--------------|
|
|
|
|
|
| 默认值 | 根据操作系统定 |
|
|
|
|
|
| 位置 | events |
|
|
|
|
|
|
|
|
|
|
- 注意:此处所选择事件处理模型是Nginx优化部分的一个重要内容,method的可选值有select/poll/epoll/kqueue等,之前在准备centos环境的时候,我们强调过要使用linux内核在2.6以上,就是为了能使用epoll函数来优化Nginx。
|
|
|
|
|
- 另外这些值的选择,我们也可以在编译的时候使用
|
|
|
|
|
- `--with-select_module`、`--without-select_module`、
|
|
|
|
|
- ` --with-poll_module`、` --without-poll_module`来设置是否需要将对应的事件驱动模块编译到Nginx的内核。
|
|
|
|
|
|
|
|
|
|
- events指令配置实例
|
|
|
|
|
```editorconfig
|
|
|
|
|
events{
|
|
|
|
|
accept_mutex on;
|
|
|
|
|
multi_accept on;
|
|
|
|
|
worker_commections 1024;
|
|
|
|
|
use epoll;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
- 启动测试
|
|
|
|
|
```shell
|
|
|
|
|
./nginx -t
|
|
|
|
|
./nginx -s reload
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### 1.5 http块
|
|
|
|
|
- 定义MIME-Type
|
|
|
|
|
- 我们都知道浏览器中可以显示的内容有HTML、XML、GIF等种类繁多的文件、媒体等资源,浏览器为了区分这些资源,就需要使用MIME Type。所以说MIME Type是网络资源的媒体类型。Nginx作为web服务器,也需要能够识别前端请求的资源类型。
|
|
|
|
|
|
|
|
|
|
- 在Nginx的配置文件中,默认有两行配置
|
|
|
|
|
```editorconfig
|
|
|
|
|
include mime.types;
|
|
|
|
|
default_type application/octet-stream;
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
- (1)default_type:用来配置Nginx响应前端请求默认的MIME类型。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 语法 | default_type mime-type; |
|
|
|
|
|
|-----|--------------------------|
|
|
|
|
|
| 默认值 | default_type text/plain; |
|
|
|
|
|
| 位置 | http、server、location |
|
|
|
|
|
|
|
|
|
|
- 举例来说明:
|
|
|
|
|
- 有些时候请求某些接口的时候需要返回指定的文本字符串或者json字符串,如果逻辑非常简单或者干脆是固定的字符串,
|
|
|
|
|
那么可以使用nginx快速实现,这样就不用编写程序响应请求了,可以减少服务器资源占用并且响应性能非常快。
|
|
|
|
|
```editorconfig
|
|
|
|
|
|
|
|
|
|
location /get_text {
|
|
|
|
|
#这里也可以设置成text/plain
|
|
|
|
|
default_type text/html;
|
|
|
|
|
return 200 "This is nginx's text";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
location /get_json{
|
|
|
|
|
default_type application/json;
|
|
|
|
|
return 200 '{"name":"TOM","age":18}';
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
- 自定义服务日志
|
|
|
|
|
- Nginx中日志的类型分access.log、error.log。
|
|
|
|
|
- access.log:用来记录用户所有的访问请求。
|
|
|
|
|
- error.log:记录nginx本身运行时的错误信息,不会记录用户的访问请求。
|
|
|
|
|
- Nginx服务器支持对服务日志的格式、大小、输出等进行设置,需要使用到两个指令,分别是**access_log和log_format指令**。
|
|
|
|
|
- (1)access_log:用来设置用户访问日志的相关属性。
|
|
|
|
|
|
|
|
|
|
| 语法 | access_log path[format[buffer=size]] |
|
|
|
|
|
|-----|--------------------------------------|
|
|
|
|
|
| 默认值 | access_log logs/access.log combined; |
|
|
|
|
|
| 位置 | `http`, `server`, `location` |
|
|
|
|
|
|
|
|
|
|
- (2)log_format:用来指定日志的输出格式。
|
|
|
|
|
|
|
|
|
|
| 语法 | log_format name [escape=default\json\none] string....; |
|
|
|
|
|
|-----|--------------------------------------------------------|
|
|
|
|
|
| 默认值 | log_format combined "..."; |
|
|
|
|
|
| 位置 | http |
|
|
|
|
|
|
|
|
|
|
- **其他配置指令**
|
|
|
|
|
- (1)sendfile:用来设置Nginx服务器是否使用sendfile()传输文件,该属性可以大大提高Nginx处理静态资源的性能
|
|
|
|
|
|
|
|
|
|
| 语法 | sendfile on\off; |
|
|
|
|
|
|-----|----------------------|
|
|
|
|
|
| 默认值 | sendfile off; |
|
|
|
|
|
| 位置 | http、server、location |
|
|
|
|
|
|
|
|
|
|
- (2)keepalive_timeout:用来设置长连接的超时时间。
|
|
|
|
|
- 为什么要使用keepalive?
|
|
|
|
|
- 我们都知道HTTP是一种无状态协议,客户端向服务端发送一个TCP请求,服务端响应完毕后断开连接。
|
|
|
|
|
如何客户端向服务端发送多个请求,每个请求都需要重新创建一次连接,效率相对来说比较多,使用keepalive模式,
|
|
|
|
|
可以告诉服务器端在处理完一个请求后保持这个TCP连接的打开状态,若接收到来自这个客户端的其他请求,服务端就会利用这个未被关闭的连接,而不需要重新创建一个新连接,
|
|
|
|
|
提升效率,但是这个连接也不能一直保持,这样的话,连接如果过多,也会是服务端的性能下降,这个时候就需要我们进行设置其的超时时间。
|
|
|
|
|
|
|
|
|
|
| 语法 | keepalive_timeout time; |
|
|
|
|
|
|-----|-------------------------|
|
|
|
|
|
| 默认值 | keepalive_timeout 75s; |
|
|
|
|
|
| 位置 | http、server、location |
|
|
|
|
|
|
|
|
|
|
- (3)keepalive_requests:用来设置一个keep-alive连接使用的次数。
|
|
|
|
|
|
|
|
|
|
| 语法 | keepalive_requests number; |
|
|
|
|
|
|-----|----------------------------|
|
|
|
|
|
| 默认值 | keepalive_requests 100; |
|
|
|
|
|
| 位置 | http、server、location |
|
|
|
|
|
|
|
|
|
|
### 1.6 server块和location块
|
|
|
|
|
- 本节我们主要来认识下Nginx默认给的nginx.conf中的相关内容,以及server块与location块在使用的时候需要注意的一些内容。
|
|
|
|
|
|
|
|
|
|
```editorconfig
|
|
|
|
|
server {
|
|
|
|
|
listen 80;
|
|
|
|
|
server_name localhost;
|
|
|
|
|
location / {
|
|
|
|
|
root html;
|
|
|
|
|
index index.html index.htm;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
error_page 500 502 503 504 404 /50x.html;
|
|
|
|
|
location = /50x.html {
|
|
|
|
|
root html;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|