nginx配置

nginx配置

nginx默认配置文件

[root@server1 conf]# pwd
/usr/local/nginx/conf
[root@server1 conf]# ls
fastcgi.conf            nginx.conf
fastcgi.conf.default    nginx.conf.default
fastcgi_params          scgi_params
fastcgi_params.default  scgi_params.default
koi-utf                 uwsgi_params
koi-win                 uwsgi_params.default
mime.types              win-utf
mime.types.default

nginx服务控制方式

//服务控制方式,使用nginx命令
    -t  //检查配置文件语法
    -v  //输出nginx的版本
    -c  //指定配置文件的路径
    -s  //发送服务控制信号,可选值有{stop|quit|reopen|reload}

检查语法 :-t

[root@server1 ~]# 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@server1 ~]# 

nginx版本:-v

[root@server1 ~]# nginx -v
nginx version: nginx/1.20.1

指定配置文件路径: -c

[root@server1 conf]# pwd
/usr/local/nginx/conf
#把默认配置文件备份到另一个目录
[root@server1 conf]# cp nginx.conf mime.types /opt/test/
[root@server1 conf]# ls /opt/test/
mime.types  nginx.conf

#查看默认配置文件有4个worker_processes进程
[root@server1 conf]# head -5 nginx.conf

#user  nobody;
worker_processes  4;

#error_log  logs/error.log;


[root@server1 conf]# ps -ef | grep nginx
root      255627       1  0 16:06 ?        00:00:00 nginx: master process nginx
nginx     255628  255627  0 16:06 ?        00:00:00 nginx: worker process
nginx     255629  255627  0 16:06 ?        00:00:00 nginx: worker process
nginx     255630  255627  0 16:06 ?        00:00:00 nginx: worker process
nginx     255631  255627  0 16:06 ?        00:00:00 nginx: worker process
root      263190    1511  0 16:08 pts/0    00:00:00 grep --color=auto nginx



[root@server1 ~]# head -5 /opt/test/nginx.conf 

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;

[root@server1 ~]# nginx -s stop;nginx -c /opt/test/nginx.conf 
[root@server1 ~]# ps -ef | grep nginx
root      266383       1  0 16:09 ?        00:00:00 nginx: master process nginx -c /opt/test/nginx.conf
nginx     266384  266383  0 16:09 ?        00:00:00 nginx: worker process
root      266766    1511  0 16:09 pts/0    00:00:00 grep --color=auto nginx

发送服务控制信号:-s

停止服务 stop
[root@server1 ~]# nginx -s stop
[root@server1 ~]# ps -ef | grep nginx
root      333600    1511  0 16:27 pts/0    00:00:00 grep --color=auto nginx
[root@server1 ~]# ss -antl
State    Recv-Q   Send-Q       Local Address:Port                     Peer Address:Port                 
LISTEN   0        128                0.0.0.0:22                            0.0.0.0:*                    
LISTEN   0        128                   [::]:22                               [::]:*   
退出服务:quit
[root@server1 ~]# ps -ef | grep nginx
root      341196       1  0 16:30 ?        00:00:00 nginx: master process nginx
nginx     341197  341196  0 16:30 ?        00:00:00 nginx: worker process
nginx     341198  341196  0 16:30 ?        00:00:00 nginx: worker process
nginx     341199  341196  0 16:30 ?        00:00:00 nginx: worker process
nginx     341200  341196  0 16:30 ?        00:00:00 nginx: worker process
root      341923    1511  0 16:30 pts/0    00:00:00 grep --color=auto nginx

[root@server1 ~]# nginx -s quit
[root@server1 ~]# ps -ef | grep nginx
root      346758    1511  0 16:31 pts/0    00:00:00 grep --color=auto nginx

重新加载文件:reload
[root@server1 ~]# ps -ef | grep nginx
root      355450       1  0 16:33 ?        00:00:00 nginx: master process nginx
nginx     355451  355450  0 16:33 ?        00:00:00 nginx: worker process
nginx     355452  355450  0 16:33 ?        00:00:00 nginx: worker process
nginx     355453  355450  0 16:33 ?        00:00:00 nginx: worker process
nginx     355454  355450  0 16:33 ?        00:00:00 nginx: worker process
root      355606    1511  0 16:34 pts/0    00:00:00 grep --color=auto nginx
[root@server1 ~]# vim /usr/local/nginx/conf/nginx.conf
[root@server1 ~]# head -5 /usr/local/nginx/conf/nginx.conf

#user  nobody;
worker_processes  2;  #原本有四个worker process进程现在改为两个

#error_log  logs/error.log;
[root@server1 ~]# nginx -s reload    #重新加载
[root@server1 ~]# ps -ef | grep nginxroot      355450       1  0 16:33 ?        00:00:00 nginx: master process nginx
nginx     358912  355450  0 16:34 ?        00:00:00 nginx: worker process
nginx     358913  355450  0 16:34 ?        00:00:00 nginx: worker process
root      359145    1511  0 16:34 pts/0    00:00:00 grep --color=auto nginx

nginx的配置文件详解

主配置文件:/usr/local/nginx/conf/nginx.conf

  • 默认启动nginx时,使用的配置文件是:安装路径/conf/nginx.conf文件
  • 可以在启动nginx时通过-c选项来指定要读取的配置文件

nginx常见的配置文件及其作用

配置文件 作用
nginx.conf nginx的基本配置文件
mime.types MIME类型关联的扩展文件
fastcgi.conf 与fastcgi相关的配置
proxy.conf 与proxy相关的配置
sites.conf 配置nginx提供的网站,包括虚拟主机

nginx.conf配置详解

nginx.conf的内容分为以下几段:

  • main配置段:全局配置段。其中main配置段中可能包含event配置段
  • event {}:定义event模型工作特性
  • http {}:定义http协议相关的配置

配置指令:要以分号结尾,语法格式如下:

derective value1 [value2 ...];

支持使用变量:

  • 内置变量:模块会提供内建变量定义
  • 自定义变量:set var_name value

用于调试、定位问题的配置参数

daemon {on|off};    //是否以守护进程方式运行nginx,调试时应设置为off
master_process {on|off};    //是否以master/worker模型来运行nginx,调试时可以设置为off
error_log 位置 级别;    //配置错误日志

error_log里的位置和级别能有以下可选项:

位置 级别
file stderr syslog:server=address[,parameter=value] memory:size debug:若要使用debug级别,需要在编译nginx时使用–with-debug选项
info
notice
warn
error
crit
alert
emerg

开启错误日志

[root@serevr1 ~]# vim   /usr/local/nginx/conf/nginx.conf
#user  nobody;
worker_processes  2;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
error_log  logs/error.log  info;

查看错误日志位置

[root@serevr1 nginx]# pwd
/var/log/nginx
[root@serevr1 nginx]# ls
access.log  cat  error.log
[root@serevr1 nginx]# cat  error.log 
2021/10/26 17:08:47 [notice] 373309#0: signal process started
2021/10/26 19:29:56 [notice] 227608#0: signal process started
2021/10/26 19:30:33 [error] 227612#0: *1 open() "/usr/local/nginx/html/favicon.ico" failed (2: No such file or directory), client: 192.168.244.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "192.168.244.133", referrer: "http://192.168.244.133/"

清空错误日志并改日志级别为debug

[root@serevr1 nginx]# > error.log 
[root@serevr1 nginx]# cat error.log 

[root@serevr1 ~]# vim   /usr/local/nginx/conf/nginx.conf
#user  nobody;
worker_processes  2;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
error_log  logs/error.log error ;
#重启服务
[root@serevr1 ~]# systemctl restart nginx
#查看错误日志
[root@serevr1 nginx]# cat error.log 
2021/10/26 20:16:18 [notice] 331720#0: signal process started
2021/10/26 20:18:21 [notice] 343000#0: signal process started

正常运行必备的配置参数

user USERNAME [GROUPNAME];    //指定运行worker进程的用户和组
pid /path/to/pid_file;    //指定nginx守护进程的pid文件
worker_rlimit_nofile number;    //设置所有worker进程最大可以打开的文件数,默认为1024
worker_rlimit_core size;    //指明所有worker进程所能够使用的总体的最大核心文件大
user 指定运行worker进程的用户和组
[root@serevr1 ~]# head -5  /usr/local/nginx/conf/nginx.conf

user  nginx nginx;  #修改为nginx用户  nginx组
worker_processes  2;

#error_log  logs/error.log;

重启nginx服务
[root@serevr1 ~]# systemctl restart nginx



[root@serevr1 ~]# ps -ef  | grep nginx
root      367359       1  0 20:22 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx     367360  367359  0 20:22 ?        00:00:00 nginx: worker process
nginx     367361  367359  0 20:22 ?        00:00:00 nginx: worker process
root      368896  300727  0 20:23 pts/2    00:00:00 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值