25 Nginx全局块的其他配置指令

25 Nginx全局块的其他配置指令

25.1 daemon 指令

daemon:设置Nginx是否以守护进程的方式启动

守护进程是linux后台执行的一种服务进程,特点是独立于控制终端,不会随着终端关闭而停止(默认为 on )

语法
daemon on|off;
默认值
daemon on;
位置
全局块
 
 
 
 
[root@nginx-100 ~]# ps -ef|grep nginx
root       1488      1  0 19:03 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
www        1489   1488  0 19:03 ?        00:00:00 nginx: worker process
www        1490   1488  0 19:03 ?        00:00:00 nginx: worker process
root       1494   1461  0 19:05 pts/0    00:00:00 grep --color=auto nginx
[root@nginx-100 ~]# cat /usr/local/nginx/conf/nginx.conf
..........
user  www;
master_process on;
worker_processes  2;
daemon off;
..........
# 与上一节的 master_process 一样,需要先 stop 再 start,才会启用配置
[root@nginx-100 ~]# /usr/local/nginx/sbin/nginx -t && /usr/local/nginx/sbin/nginx -s reload
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@nginx-100 ~]# ps -ef|grep nginx
root       1488      1  0 19:03 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
www        1549   1488  0 19:06 ?        00:00:00 nginx: worker process
www        1550   1488  0 19:06 ?        00:00:00 nginx: worker process
root       1552   1461  0 19:06 pts/0    00:00:00 grep --color=auto nginx
[root@nginx-100 ~]# /usr/local/nginx/sbin/nginx -s stop

左侧启动 nginx 后,堵塞当前终端,右侧新开一个终端,查看进程可以查看到当前启动的nginx进程

image

浏览器访问:http://10.0.0.120/ 可以展示页面

image

当按住 Ctrl + c 后,nginx 进程也中断了

image

浏览器访问:http://10.0.0.120/ 显示无法访问

image

25.2 pid 指令

pid:用来配置 Nginx 当前 master进程的进程ID号存储的文件路径

语法 pid file;
默认值 /usr/local/nginx/logs/nginx.pid
位置 全局块

 

 

 

 

该属性可以通过:./configure --pid-path=PATH 参数指定

注意:1:./configure 与 nginx.conf 同时指定,最终以 nginx.conf 配置为准

   2:修改pid路径后,无法使用 nginx -s stop 停止nginx进程

25.3 error_log 指令

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性能

注意:测试使用 debug 时,不止要将 日志级别 改为 debug,还需额外配置

25.4 include 指令

include:用来引入其他配置文件,使 Nginx配置更加灵活

语法 include file;
默认值
位置 任何位置 any

 

 

 

 

# 将一些配置注释掉,重栽nginx,仍然能加载到默认配置文件
[root@nginx-100 ~]# cat /usr/local/nginx/conf/nginx.conf
#user  nobody;
#user  www;
#master_process on;
#worker_processes  2;
#daemon on;
..........
[root@nginx-100 ~]# /usr/local/nginx/sbin/nginx -t && /usr/local/nginx/sbin/nginx -s reload
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@nginx-100 ~]# ps -ef|grep nginx
root       1700      1  0 19:54 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody     1706   1700  0 19:54 ?        00:00:00 nginx: worker process
root       1708   1461  0 19:54 pts/0    00:00:00 grep --color=auto nginx

# 新建配置文件,可以被引入到nginx.conf 配置中
[root@nginx-100 ~]# cat /usr/local/nginx/conf/main.conf
user  www;
worker_processes  2;
[root@nginx-100 ~]# cat /usr/local/nginx/conf/nginx.conf
include /usr/local/nginx/conf/main.conf;
#user  nobody;
#user  www;
..........
[root@nginx-100 ~]# /usr/local/nginx/sbin/nginx -t && /usr/local/nginx/sbin/nginx -s reload
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@nginx-100 ~]# ps -ef|grep nginx
root       1700      1  0 19:54 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
www        1824   1700  0 20:03 ?        00:00:00 nginx: worker process
www        1825   1700  0 20:03 ?        00:00:00 nginx: worker process
root       1827   1461  0 20:03 pts/0    00:00:00 grep --color=auto nginx

———————————————————————————————————————————————————————————————————————————

                                                                                                                         无敌小马爱学习

posted on 2026-03-30 16:28  马俊南  阅读(4)  评论(0)    收藏  举报