03-Nginx核心配置文件结构

三、Nginx核心配置文件结构

从前面的内容学习中,我们知道Nginx的核心配置文件默认是放在/usr/local/nginx/conf/nginx.conf,这一节,我们就来学习下nginx.conf的内容和基本配置方法。
读取Nginx自带的Nginx配置文件,我们将其中的注释部分【学习一个技术点就是在Nginx的配置文件中可以使用#来注释】删除掉后,就剩下下面内容:

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;
        }
    }

}
指令名	指令值;  #全局块,主要设置Nginx服务器整体运行的配置指令

 #events块,主要设置,Nginx服务器与用户的网络连接,这一部分对Nginx服务器的性能影响较大
events {	 
    指令名	指令值;
}
#http块,是Nginx服务器配置中的重要部分,代理、缓存、日志记录、第三方模块配置...             
http {		
    指令名	指令值;
    server { #server块,是Nginx配置和虚拟主机相关的内容
        指令名	指令值;
        location / { 
        #location块,基于Nginx服务器接收请求字符串与location后面的值进行匹配,对特定请求进行处理
            指令名	指令值;
        }
    }
	...
}

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

1、全局块

1.1 user指令

(1)user:用于配置运行Nginx服务器的worker进程的用户和用户组。

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

该属性也可以在编译的时候指定,语法如下./configure --user=user --group=group,如果两个地方都进行了设置,最终生效的是配置文件中的配置。
该指令的使用步骤:
(1)设置一个用户信息"www"

user www;

[root@zqf conf]# vim nginx.conf
[root@zqf conf]# cd ../sbin/
[root@zqf sbin]# ./nginx -t
nginx: [emerg] getpwnam("www") failed in /usr/local/nginx/conf/nginx.conf:1
nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed

(2) 创建一个用户
useradd www

[root@zqf sbin]# useradd www
[root@zqf sbin]# ./nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

(3)修改user属性
user www

[root@zqf sbin]# ps -ef|grep nginx
root      39856      1  0 16:43 ?        00:00:00 nginx: master process ./nginx
nobody    40024  39856  0 16:54 ?        00:00:00 nginx: worker process
root      40176  26759  0 17:03 pts/1    00:00:00 grep --color=auto nginx
[root@zqf sbin]# ./nginx -s reload
[root@zqf sbin]# ps -ef|grep nginx
root      39856      1  0 16:43 ?        00:00:00 nginx: master process ./nginx
www       40178  39856  0 17:03 ?        00:00:00 nginx: worker process
root      40180  26759  0 17:03 pts/1    00:00:00 grep --color=auto nginx

(4)创建/root/html/index.html页面,添加如下内容

[root@zqf sbin]# mkdir /root/html/
[root@zqf sbin]# cd /root/html/
[root@zqf html]# vim index.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

location / {
	root   /root/html;
	index  index.html index.htm;
}

(6)测试启动访问

在这里插入图片描述


页面会报403拒绝访问的错误
(7)将文件创建到 /home/www/html/index.html,修改配置

cp -r /root/html/ /home/www/

修改nginx.conf配置

location / {
	root   /home/www/html;
	index  index.html index.htm;
}

(8)再次测试启动访问
![image.png](https://img-blog.csdnimg.cn/img_convert/6d3d736cc04bdb34994c5efc298e1218.png#clientId=ueaf2911a-bf73-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=351&id=ua019f6f1&margin=[object Object]&name=image.png&originHeight=483&originWidth=1346&originalType=binary&ratio=1&rotation=0&showTitle=false&size=54634&status=done&style=none&taskId=ufdd9cc9b-8e3d-498c-93a3-e114291c641&title=&width=978.9090909090909)
能正常访问。
综上所述,使用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,则会看到如下内容:需要重启nginx生效

[root@zqf sbin]# ./nginx -s stop
[root@zqf sbin]# ./nginx
[root@zqf sbin]# ps -ef|grep nginx
root      40365      1  0 17:16 ?        00:00:00 nginx: master process ./nginx
www       40366  40365  0 17:16 ?        00:00:00 nginx: worker process
www       40367  40365  0 17:16 ?        00:00:00 nginx: worker process
root      40369  26759  0 17:16 pts/1    00:00:00 grep --color=auto nginx

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的性能。
(5)include:用来引入其他配置文件,使Nginx的配置更加灵活

语法include file;
默认值
位置any

2、events块

2.1 accept_mutex:用来设置Nginx网络连接序列化

语法accept_mutex on|off;
默认值accept_mutex on;
位置events

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

在这里插入图片描述

2.2 multi_accept:用来设置是否允许同时接收多个网络连接

语法multi_accept on|off;
默认值multi_accept off;
位置events

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

2.3 worker_connections:用来配置单个worker进程最大的连接数

语法worker_connections number;
默认值worker_commections 512;
位置events

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

2.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指令配置实例
打开Nginx的配置文件 nginx.conf,添加如下配置

events{
	accept_mutex on;
	multi_accept on;
	worker_commections 1024;
	use epoll;
}

启动测试

./nginx -t
./nginx -s reload

3、http块

3.1 定义MIME-Type

我们都知道浏览器中可以显示的内容有HTML、XML、GIF等种类繁多的文件、媒体等资源,浏览器为了区分这些资源,就需要使用MIME Type。所以说MIME Type是网络资源的媒体类型。Nginx作为web服务器,也需要能够识别前端请求的资源类型。
在Nginx的配置文件中,默认有两行配置

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

(1)default_type:用来配置Nginx响应前端请求默认的MIME类型。

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

在default_type之前还有一句include mime.types,include之前我们已经介绍过,相当于把mime.types文件中MIMT类型与相关类型文件的文件后缀名的对应关系加入到当前的配置文件中。
举例来说明:
有些时候请求某些接口的时候需要返回指定的文本字符串或者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}';
 }

测试:

[root@zqf sbin]# curl localhost/get_text
This is nginx's text
[root@zqf sbin]# curl localhost/get_json
{"name":"TOM","age":18}
[root@zqf sbin]# 

![image.png](https://img-blog.csdnimg.cn/img_convert/2179441f0ea38fd632477c9dacb0c978.png#clientId=ueaf2911a-bf73-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=79&id=u182a6ab1&margin=[object Object]&name=image.png&originHeight=108&originWidth=480&originalType=binary&ratio=1&rotation=0&showTitle=false&size=8804&status=done&style=none&taskId=u22da9163-7bd6-4fe3-9574-2f3a2fcc869&title=&width=349.09090909090907)
![image.png](https://img-blog.csdnimg.cn/img_convert/0106c66f22759fa63725581abb9362a1.png#clientId=ueaf2911a-bf73-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=105&id=ubee533fd&margin=[object Object]&name=image.png&originHeight=144&originWidth=538&originalType=binary&ratio=1&rotation=0&showTitle=false&size=9037&status=done&style=none&taskId=u9129cba6-a9f0-4f60-b0da-81fc15f4f12&title=&width=391.27272727272725)

3.2 自定义服务日志

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

(3)测试
修改nginx.conf配置,在http块增加

log_format myformat '==========this is my format===========';
access_log logs/my.log myformat;
[root@zqf sbin]# vim ../conf/nginx.conf
[root@zqf sbin]# ./nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@zqf sbin]# ./nginx -s reload
[root@zqf sbin]# cd ../logs/
[root@zqf logs]# ll
total 20
-rw-r--r--. 1 root root 9044 Jun  3 17:34 access.log
-rw-r--r--. 1 root root 1988 Jun  3 17:39 error.log
-rw-r--r--. 1 root root    0 Jun  3 17:39 my.log
-rw-r--r--. 1 root root    6 Jun  3 17:16 nginx.pid
[root@zqf logs]# tail -f my.log 
==========this is my format===========
==========this is my format===========

3.3 其他配置指令

(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

4、server块和location块

server块和location块都是我们要重点讲解和学习的内容,因为我们后面会对Nginx的功能进行详细讲解,所以这块内容就放到静态资源部署的地方给大家详细说明。
本节我们主要来认识下Nginx默认给的nginx.conf中的相关内容,以及server块与location块在使用的时候需要注意的一些内容。

	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;
        }
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值