• Nginx配置文件详解


    Nginx配置文件

    Nginx的核心配置文件默认是放在/usr/local/nginx/conf/nginx.conf

    worker_processes  1;
    
    events {
        worker_connections  1024;
    }
    
    http {
        include       mime.types;
        default_type  application/octet-stream;
        sendfile        on;
        keepalive_timeout  65;
    	
        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;
            }
        }
    }
    
    
    • 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

    nginx.conf配置文件中默认有三大块:全局块、events块、http
    http块中可以配置多个server块,每个server块又可以配置多个location块。

    全局配置

    主要设置Nginx服务器整体运行的配置指令

    1. user指令

    用于配置运行Nginx服务器的worker进程的用户和用户组。使用user指令可以指定启动运行工作进程的用户及用户组,这样对于系统的权限访问控制的更加精细,也更加安全

    语法user user [group]
    默认值nobody
    位置全局块

    该属性也可以在编译的时候指定,语法如下./configure --user=user --group=group,如果两个地方都进行了设置,最终生效的是配置文件中的配置。

    • 设置一个用户信息"songhongwei"
      在这里插入图片描述

    此时,worker进程的用户是songhongwei,如果访问当前用户没有权限的资源,nginx会报403错误,例如当前配置,访问nginx的80端口,访问的是html目录下的index.html,但是该文件属于root用户,因此页面会报403

    • index.html对于非root用户没有读权限
      在这里插入图片描述

    • 访问结果为403
      在这里插入图片描述

    • worker进程是songhongwei用户启动的
      在这里插入图片描述

    1. work process指令

    master_process:用来指定是否开启工作进程。

    语法master_process on|off;
    默认值master_process on;
    位置全局块

    如果设置成off,nginx将不启动master进程,也没有worker进程。
    worker_processes:用于配置Nginx生成工作进程的数量,这个是Nginx服务器实现并发处理服务的关键所在。理论上来说workder process的值越大,可以支持的并发处理量也越多,但事实上这个值的设定是需要受到来自服务器自身的限制,建议将该值和服务器CPU的内核数保存一致。

    语法worker_processes num/auto;
    默认值1
    位置全局块

    如果将worker_processes设置成2,则会看到如下内容:有两个worker进程
    在这里插入图片描述

    1. 其他指令

    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,翻译过来为试|信息|通知|警告|错误|临界|警报|紧急。
    include:用来引入其他配置文件,使Nginx的配置更加灵活

    语法include file;
    默认值
    位置any

    定义一个nginx配置文件test.conf,然后在nginx.conf主配置文件中include定义的test.conf
    test.conf监听8080端口的所有请求,返回200 页面显示songhongwei

     server {
        listen       8080;
        server_name  localhost;
        location / {
            default_type text/html;
            return 200 "

    songhongwei

    "; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    nginx.conf主配置文件include定义的test.conf

    user songhongwei;
    master_process on;
    worker_processes  2;
    
    events {
        worker_connections  1024;
    }
    http {
        include       mime.types;
        default_type  application/octet-stream;
        sendfile        on;
        keepalive_timeout  65;
        # 引入自定义的配置
        include test.conf;
        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;
            }
        }
    }
    
    
    • 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

    events配置

    主要设置Nginx服务器与用户的网络连接,这一部分对Nginx服务器的性能影响较大

    1. accept_mutex:用来设置Nginx网络连接序列化
      | 语法 | accept_mutex on|off; |
      | — | — |
      | 默认值 | accept_mutex on; |
      | 位置 | events |

    这个配置主要可以用来解决"惊群"问题。意思是在某一个时刻,客户端发来一个请求连接,Nginx后台是以多进程的工作模式,也就是说有多个worker进程会被同时唤醒,但是最终只会有一个进程可以获取到连接,如果每次唤醒的进程数目太多,就会影响Nginx的整体性能。如果将上述值设置为on(开启状态),将会对多个Nginx进程接收连接进行序列号,一个个来唤醒接收,就防止了多个进程对连接的争抢。

    1. multi_accept:用来设置是否允许同时接收多个网络连接
      | 语法 | multi_accept on|off; |
      | — | — |
      | 默认值 | multi_accept off; |
      | 位置 | events |

    如果multi_accept被禁止了,nginx一个工作进程只能同时接受一个新的连接。否则,一个工作进程可以同时接受所有的新连接

    1. worker_connections:用来配置单个worker进程最大的连接数
      | 语法 | worker_connections number; |
      | — | — |
      | 默认值 | worker_commections 512; |
      | 位置 | events |

    这里的连接数不仅仅包括和前端用户建立的连接数,而是包括所有可能的连接数。另外,number值不能大于操作系统支持打开的最大文件句柄数量。

    1. use:用来设置Nginx服务器选择哪种事件驱动来处理网络消息。
      | 语法 | use method; |
      | — | — |
      | 默认值 | 根据操作系统定 |
      | 位置 | events |

    注意:此处所选择事件处理模型是Nginx优化部分的一个重要内容,method的可选值有select/poll/epoll/kqueue
    另外这些值的选择,我们也可以在编译的时候使用
    --with-select_module--without-select_module
    --with-poll_module--without-poll_module来设置是否需要将对应的事件驱动模块编译到Nginx的内核。

    http配置

    是Nginx服务器配置中的重要部分,代理、缓存、日志记录、第三方模块配置等

    1. default_type:用来配置Nginx响应前端请求默认的MIME类型。在Nginx的配置文件中,默认有两行配置,在default_type之前还有一句include mime.types某些接口的时候需要返回指定的文本字符串或者json字符。

    include mime.types;
    default_type application/octet-stream``;

    语法default_type mime-type;
    默认值default_type text/plain;
    位置http、server、location

    案例:配置/json 返回json类型,/html返回html类型

    server {
        listen       8081;
        server_name  localhost;
        location /json {
        #返回json格式
            default_type application/json;
            return 200 "{id:1,name:songhongwei}";
        }
        location /html {
        # 返回html格式
            default_type text/html;
            root html;
            index index.html;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 自定义服务日志

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

    语法access_log path[format[buffer=size]]
    默认值access_log logs/access.log combined;
    位置http
    , server
    , location

    log_format:用来指定日志的输出格式。
    日志输出的nginx内置变量可参考http://nginx.org/en/docs/varindex.html官网,例如

    变量名变量含义
    request_uri完整的原始请求 URI (带参数)
    request_method请求方法,通常是GET
    或者POST
    remote_addr客户端ip
    remote_port客户端端口
    语法log_format name [escape=default|json|none] string…;
    默认值log_format combined “…”;
    位置http

    案例:

    http {
        include       mime.types;
        default_type  application/octet-stream;
        sendfile        on;
      	# 定义日志格式
        log_format  main  'songhongwei 好帅====>$remote_addr - $remote_user [$time_local] "$request" '
                          '$status "$http_referer" '
                          ' "$http_x_forwarded_for"';
       # 引用日志格式 
        access_log  logs/access.log  main;
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    日志输出:
    在这里插入图片描述

    • 其他配置指令
    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
    1. keepalive_requests:用来设置一个keep-alive连接使用的次数。
      | 语法 | keepalive_requests number; |
      | — | — |
      | 默认值 | keepalive_requests 100; |
      | 位置 | http、server、location |

    server配置

    是Nginx配置和虚拟主机相关的内容

    1. listen指令

    listen:用来配置监听端口。

    | 语法 | listen address[:port] [default_server]…;
    listen port [default_server]…; |
    | — | — |
    | 默认值 | listen *:80 | *:8000 |
    | 位置 | server |

    #listen localhost:8000 监听指定的IP和端口
    listen 127.0.0.1:8000; 
    #监听指定IP的所有端口
    listen 127.0.0.1;	
    #监听指定端口上的连接
    listen 8000;	
    #监听指定端口上的连接
    listen *:8000;	
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    default_server属性是标识符,用来将此虚拟主机设置成默认主机。所谓的默认主机指的是匹配到了listen但是没有匹配到server_name(用来处理没有成功匹配server_name的请求
    ),则会默认执行的。如果不指定默认使用的是第一个server。

    server{
    	listen 8080;
    	server_name 127.0.0.1;
    	location /{
    		root html;
    		index index.html;
    	}
    }
    server{
    	listen 8080 default_server;
    	server_name localhost;
    	default_type text/plain;
    	return 444 'This is a error request';
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    1. server_name指令

    server_name:用来设置虚拟主机服务名称。

    | 语法 | server_name name …;
    name可以提供多个中间用空格分隔 |
    | — | — |
    | 默认值 | server_name “”; |
    | 位置 | server |

    server_name的配置方式有三种,精确匹配、通配符匹配、正则表达式匹配

    • 精确匹配

    因为是域名访问,测试是的虚拟域名,需要在/etc/hosts文件里修改host解析

    server {
        listen       8090;
        server_name www.song.com www.hongwei.com;
        location /json {
            default_type application/json;
            return 200 "{'id':1,'name':'songhongwei'}";
        }
    
       location /html {
            default_type text/html;
            root html;
            index index.html;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    在这里插入图片描述

    • 使用通配符配置

    server_name中支持通配符"*",但需要注意的是通配符不能出现在域名的中间,只能出现在首段或尾段,如:

    在这里插入图片描述

    
    server {
        listen       8091;
      	# www.song.cn abc.song.cn www.hongwei.cn www.hongwei.com
    
        server_name *.song.cn www.hongwei.*;
        location /json {
            default_type application/json;
            return 200 "{id:1,name:songhongwei}";
        }
    
       location /html {
            default_type text/html;
            root html;
            index index.html;
        }
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 使用正则表达式配置

    server_name中可以使用正则表达式,并且使用~作为正则表达式字符串的开始标记
    常见的正则表达式:

    代码说明
    ^匹配搜索字符串开始位置
    $匹配搜索字符串结束位置
    .匹配除换行符\n之外的任何单个字符
    \转义字符,将下一个字符标记为特殊字符
    [xyz]字符集,与任意一个指定字符匹配
    [a-z]字符范围,匹配指定范围内的任何字符
    \w与以下任意字符匹配 A-Z a-z 0-9 和下划线,等效于[A-Za-z0-9_]
    \d数字字符匹配,等效于[0-9]
    {n}正好匹配n次
    {n,}至少匹配n次
    {n,m}匹配至少n次至多m次
    *零次或多次,等效于{0,}
    +一次或多次,等效于{1,}
    ?零次或一次,等效于{0,1}

    配置案例:

    server{
            listen 8092;
            server_name ~^www\.(\w+)\.com$;
            default_type text/plain;
            return 200 $1  $2 ;
    }
    # 注意 ~后面不能加空格,括号可以取值 $1 表示第一个匹配的值,$2表示第二个匹配的值,本案例中没有匹配的第二个值
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 匹配执行顺序

    由于server_name指令支持通配符和正则表达式,因此在包含多个虚拟主机的配置文件中,可能会出现一个名称被多个虚拟主机的server_name匹配成功,Nginx的匹配顺序为:

    1. 准确匹配server_name
    2. 配符在开始时匹配server_name成功
    3. 通配符在结束时匹配server_name成功
    4. 正则表达式匹配server_name成功
    5. 被默认的default_server处理,如果没有指定默认找第一个server
    
    • 1
    • 2
    • 3
    • 4
    • 5

    案例:

    server{
            listen 8093;
            server_name ~^www\.\w+\.com$;
            default_type text/plain;
            return 200 'regex_success';
    }
    
    server{
            listen 8093;
            server_name www.song.*;
            default_type text/plain;
            return 200 'wildcard_after_success';
    }
    
    server{
            listen 8093;
            server_name *.song.com;
            default_type text/plain;
            return 200 'wildcard_before_success';
    }
    
    server{
            listen 8093;
            server_name www.song.com;
            default_type text/plain;
            return 200 'exact_success';
    }
    
    server{
            listen 8093 default_server;
            server_name _;
            default_type text/plain;
            return 444 'default_server not found server';
    }
    # /etc/hosts配置
    127.0.0.1 www.song.com
    127.0.0.1 abc.song.com
    127.0.0.1 www.song.cn
    127.0.0.1 www.hongwei.com
    
    127.0.0.1 www.hongwei.cn
    
    # 访问 www.song.com 返回 exact_success
    
    # 访问 abc.song.com 返回 wildcard_before_success
    
    # 访问 www.song.cn 返回 wildcard_after_success
     
    # 访问 www.hongwei.com 返回 regex_success
    
    # 访问 www.hongwei.cn 返回 default_server not found server
    
    
    • 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
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52

    location 配置

    基于Nginx服务器接收请求字符串与location后面的值进行匹配,对特定请求进行处理

    • location指令

    location:用来设置请求的URI

    语法location [ = | ~ | ~* | ^~ |@ ] uri{…}
    默认值
    位置server,location

    uri变量是待匹配的请求字符串,可以不包含正则表达式,也可以包含正则表达式,nginx服务器在搜索匹配location的时候,先使用不包含正则表达式进行匹配,找到一个匹配度最高的一个,然后在通过包含正则表达式的进行匹配,如果能匹配到直接访问,匹配不到,就使用刚才匹配度最高的那个location来处理请求。
    属性介绍:
    不带符号,要求必须以指定模式开始,但是正则表达式规则和长的块规则将被优先和查询匹配

    server {
        listen 80;
        server_name 127.0.0.1;
        location /abc {
            default_type text/plain;
            return 200 "access success";
        }
    }
    # 以下访问都是正确的
    http://172.41.100.15/abc
    http://172.41.100.15/abc?p1=TOM
    http://172.41.100.15/abc/
    http://172.41.100.15/abcdef
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    1. = : 用于不包含正则表达式的uri前,必须与指定的模式精确匹配
    server {
        listen 80;
        server_name 127.0.0.1;
        location =/abc{
            default_type text/plain;
            return 200 "access success";
        }
    }
    # 可以匹配到
    http://172.41.100.15/abc
    http://172.41.100.15/abc?p1=TOM
    # 匹配不到
    http://172.41.100.15/abc/
    http://172.41.100.15/abcdef
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    1. ~ : 用于表示当前uri中包含了正则表达式,**并且区分大小写 **

    ~* : 用于表示当前uri中包含了正则表达式,并且不区分大小写

    server {
        listen 80;
        server_name 127.0.0.1;
        location ~^/abc\w*$ {
            default_type text/plain;
            return 200 "no ignore";
        }
    }
    server {
        listen 80;
        server_name 127.0.0.1;
        location ~*^/abc\w*$ {
            default_type text/plain;
            return 200 "ignore";
        }
    }
    # 访问 http://172.41.100.15/abc 返回 no ignore
    
    # 访问 http://172.41.100.15/Abc 返回  ignore
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    1. ^~: 用于不包含正则表达式的uri前,功能和不加符号的一致,主要区别在下面:

    Nginx匹配是先使用不包含正则表达式进行匹配,找到一个匹配度最高的一个,然后在通过包含正则表达式的进行匹配,如果能匹配到直接访问,匹配不到,就使用刚才匹配度最高的那个location来处理请求。但是如果使用^~ ,则匹配到直接使用,不在进行正则匹配,例如

    server {
        listen 80;
        server_name 127.0.0.1;
       location /images {
            default_type text/plain;
            return 200 "images access success";
        }
       location ~^/images {
            default_type text/plain;
            return 200 "express access success";
        }
    }
    
    # 访问 http://172.41.100.15/images 返回的是 express access success,因为使用了正则匹配
    server {
      listen 81;
      server_name localhost;
      location ~^/images{
        default_type text/plain;
        return 200 "express access success";
      }
      location ^~/images {
        default_type text/plain;
        return 200 "access success";
      }
    }
    # 访问 http://172.41.100.15:81/images 返回的是 access success,因为中断了正则匹配
    
    
    • 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
    1. @ : 定义一个命名的 location,使用在内部定向
    server{
    	error_page 404 @jump_to_error;
    	location @jump_to_error {
    		default_type text/plain;
    		return 404 'Not Found Page...';
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
  • 相关阅读:
    ARM DAY3
    注释之背后:代码的解释者与保护者
    美创再次荣登《2024杭州独角兽&准独角兽企业榜单》
    校园导航系统C++
    深度学习入门
    一键打包,随时运行,Python3项目虚拟环境一键整合包的制作(Venv)
    谭浩强【C语言程序设计】第一章习题详解
    PAT乙级1042 字符统计
    【BOOST C++ 20 设计模式】(2)库Boost.Signals2
    C# 基础(四)
  • 原文地址:https://blog.csdn.net/u010859650/article/details/126749965