1. Nginx的核心配置文件默认是放在 /usr/local/nginx/conf/nginx.conf
  2. Nginx自带的Nginx配置文件。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 全局块

worker_processes  1;

# events块

events {
    worker_connections  1024;
}

# http块

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    # http块中可以配置多个server块
    # 每个server块又可以配置多个location块

    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

全局块

user指令

  1. 用于配置运行Nginx服务器的worker进程的用户和用户组。
  2. 设置了该指令,nginx只能访问相关用户和用户的资源,其他的访问不到。
—————–语法—————– 默认值 示例
user user [group] nobody user www

master_process指令

  1. 指定是否开启工作进程。
—————–语法—————– 默认值 示例
master_process on/off on master_process on

worker_processes指令

  1. 配置Nginx生成工作进程的数量,这个是Nginx服务器实现并发处理服务的关键所在。
  2. 理论上来说workder process的值越大,可以支持的并发处理量也越多,但事实上这个值的设定是需要受到来自服务器自身的限制,建议将该值和服务器CPU的内核数保存一致。
—————–语法—————– 默认值 示例
worker_processes num/auto 1 worker_processes auto

daemon指令

  1. 设定Nginx是否以守护进程的方式启动。
  2. 守护式进程是linux后台执行的一种服务进程,特点是独立于控制终端,不会随着终端关闭而停止。
—————–语法—————– 默认值 示例
daemon on/off on daemon on

pid指令

  1. 配置Nginx当前master进程的进程号ID存储的文件路径。
  2. 该属性可以通过./configure --pid-path=PATH来指定。
—————–语法—————– 默认值 示例
pid file /usr/local/nginx/logs/nginx.pid pid /home/nginx/logs/nginx.pid

error_log指令

  1. 配置Nginx的错误日志存放路径。
  2. 该属性可以通过./configure --error-log-path=PATH来指定。
  3. 日志级别的值有:debug|info|notice|warn|error|crit|alert|emerg。设置的时候不要设置成info以下的等级,因为会带来大量的磁盘I/O消耗,影响Nginx的性能。
  4. 可以出现在:全局块、http、server、location。
—————–语法—————– 默认值 示例
error_log file [日志级别] logs/error.log error error_log logs/error.log error

include指令

  1. 引入其他配置文件,使Nginx的配置更加灵活。
  2. 可以出现在任何位置。
—————–语法—————– 默认值 示例
include file include conf.d/*

events块

accept_mutex指令

  1. 设置Nginx网络连接序列化。主要用来解决常说的"惊群"问题。
  2. 置为on(开启状态),将会对多个Nginx进程接收连接进行序列号,一个个来唤醒接收,就防止了多个进程对连接的争抢。
—————–语法—————– 默认值 示例
accept_mutex on/off on accept_mutex on

multi_accept指令

  1. 设置是否允许同时接收多个网络连接。
  2. 如果multi_accept被禁止了,nginx一个工作进程只能同时接受一个新的连接。否则,一个工作进程可以同时接受所有的新连接。
—————–语法—————– 默认值 示例
multi_accept on/off off multi_accept off

worker_connections指令

  1. 配置单个worker进程最大的连接数。
  2. 这里的连接数不仅仅包括和前端用户建立的连接数,而是包括所有可能的连接数。number值不能大于操作系统支持打开的最大文件句柄数量。
—————–语法—————– 默认值 示例
worker_connections number 512 worker_commections 512

use指令

  1. 设置Nginx服务器选择哪种事件驱动来处理网络消息。
  2. method的可选值有select/poll/epoll/kqueue等。
  3. 也可以在编译的时候使用--with-select_module--without-select_module--with-poll_module--without-poll_module来设置。
—————–语法—————– 默认值 示例
use method 根据操作系统定 use epoll

http块

Mime-Type

  1. 浏览器中可以显示的内容有HTML、XML、GIF等种类繁多的文件、媒体等资源,浏览器为了区分这些资源,就需要使用MIME Type。
  2. MIME Type是网络资源的媒体类型。Nginx作为web服务器,也需要能够识别前端请求的资源类型。
  3. Nginx的配置文件中,默认有两行配置:
# 把mime.types文件中MIMT类型与相关类型文件的文件后缀名的对应关系加入到当前的配置文件中
include mime.types;

default_type application/octet-stream;

default_type指令

  1. default_type:用来配置Nginx响应前端请求默认的MIME类型。
  2. 可以在 http、server、location 中。
—————–语法—————– 默认值 示例
default_type mime-type text/plain default_type text/plain
  1. 有些时候请求某些接口的时候需要返回指定的文本字符串或者json字符串,如果逻辑非常简单或者干脆是固定的字符串,那么可以使用nginx快速实现,这样就不用编写程序响应请求了,可以减少服务器资源占用并且响应性能非常快。
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}';
}

日志

  1. Nginx中日志的类型分access.log、error.log。
    • access.log:用来记录用户所有的访问请求。
    • error.log:记录nginx本身运行时的错误信息,不会记录用户的访问请求。
  2. Nginx服务器支持对服务日志的格式、大小、输出等进行设置,需要使用到两个指令,分别是access_log和log_format指令。

access_log指令

  1. 设置用户访问日志的相关属性。
  2. 位置可以在 http, server, location
—————–语法—————– 默认值 示例
access_log path[format[buffer=size]] logs/access.log combined access_log logs/access.log combined

log_format指令

  1. 指定日志的输出格式。
—————–语法—————– 默认值 示例
log_format name [escape=default|json|none] string…. combined “…” log_format combined “…”

sendfile指令

  1. 设置Nginx服务器是否使用sendfile()传输文件,该属性可以大大提高Nginx处理静态资源的性能。
  2. 可以在 http、server、location 中。
—————–语法—————– 默认值 示例
sendfile on/off off sendfile off

keepalive_timeout指令

  1. 设置长连接的超时时间。
  2. 可以在 http、server、location 中。
—————–语法—————– 默认值 示例
keepalive_timeout time 75s keepalive_timeout 75

keepalive_requests指令

  1. 设置一个keep-alive连接使用的次数。
  2. 可以在 http、server、location 中。
—————–语法—————– 默认值 示例
keepalive_requests number 100 keepalive_requests 100