Nginx安装:Installation Of Nginx.
Nginx应用:Application Of Nginx.
Application Of Nginx
帮助文档首页: http://nginx.org/en/docs/.
指令索引列表: http://nginx.org/en/docs/dirindex.html.
变量索引列表: http://nginx.org/en/docs/varindex.html.
1 Nginx的命令行控制
1、Nginx查看当前配置信息
nginx -T
2、重新加载Nginx配置信息
nginx -s reload
-?和-h : 显示帮助信息
-v : 打印版本号信息并退出
-V : 打印版本号信息和配置信息并退出
-t : 测试nginx的配置文件语法是否正确并退出
-T : 测试nginx的配置文件语法是否正确并列出用到的配置文件信息然后退出
-q : 在配置测试期间禁止显示非错误消息
-s : signal信号,后面可以跟 :
stop [快速关闭,类似于TERM/INT信号的作用]
quit [优雅的关闭,类似于QUIT信号的作用]
reopen [重新打开日志文件类似于USR1信号的作用]
reload [类似于HUP信号的作用]
-p : prefix,指定Nginx的prefix路径,(默认为: /usr/local/nginx/)
-c : filename,指定Nginx的配置文件路径,(默认为: /etc/conf/nginx.conf)
-g : 用来补充Nginx配置文件,向Nginx服务指定启动时应用全局的配置
2 Nginx默认配置
2.1 查看nginx当前使用配置文件
1、查看nginx运行的PID。
ps -ef | grep nginx
netstat -lntup | grep nginx
netstat -lntup | grep 9100
2、通过相应的进程PID【如:964】查询当前运行的nginx路径
ll /proc/964/exe
3、获取到nginx的执行路径后,使用-t
参数即可获取该进程对应的配置文件路径
/usr/local/nginx/sbin/nginx -t
记得举一反三哦!!!
2.2 Nginx配置环境变量
- 创建配置文件
vim /etc/profile.d/nginx.sh
#内容如下
NGINX_HOME=/usr/local/nginx/sbin
PATH=$NGINX_HOME/sbin:$PATH
export PATH
- 重载环境变量
source /etc/profile
2.3 nginx提供的默认配置文件信息
nginx为我们提供了默认的80端口http服务:
worker_processes 1;
events {
worker_connections 1024;
}
# 提供http服务
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
# 每个server提供一个http服务;多个server存在相同配置,则可将相同配置写在http块中。
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;
}
}
#包含其他子配置文件
#include /usr/local/nginx/conf.d/*.conf;
}
root alias index
在sever块中添加如下配置指令:
location /goods {
root html/goods; # 文档根目录:若路径为/goods/test.jpg则查找的文件为 html/goods/goods/test.jpg。
index index.html index.htm; # 若请求路径为/goods/且为目录则响应html/goods/goods/index.html 或 html/goods/goods/index.htm。
}
location /order {
alias html/asserts;# 匹配路径替换:若路径为/order/test.jpg则查找的文件为 html/asserts/test.jpg
index index.html index.htm;
}
root帮助文档: http://nginx.org/en/docs/http/ngx_http_core_module.html#root.
Syntax: root path;
Default:
root html;
Context: http, server, location, if in location
alias帮助文档: http://nginx.org/en/docs/http/ngx_http_core_module.html#alias.
Syntax: alias path;
Default: —
Context: location
访问日志、错误日志
error_log 错误日志
access_log 访问日志
在http块中配置访问日志
log_format myformat '$remote_addr - $http_user_agent';
access_log logs/access.log myformat;
$remote_addr:客户端的IP地址(普通情况下)。
$remote_user:当nginx开启了用户认证功能后,此变量记录了客户端使用了哪个用户进行了认证。
$time_local:当前日志条目的时间。
$request:当前http请求的method、url和http协议版本。
$status:当前http请求的响应状态码,如200、404、500等。
$body_bytes_sent:nginx响应客户端请求时发送给客户端的字节数,不包含响应头的大小。
$http_referer:当前请求是从哪个页面请求过来的。
$http_user_agent:客户端的软件信息,如浏览器的名称和版本号等。
注:“变量"和"配置指令"都是隶属于某个"模块"的,因此使用"变量"和"配置指令”,则必须先确保当前nginx中包含了对应的"模块",不是所有"模块"都提供了可用的"变量"和"配置指令"。
变量帮助文档:http://nginx.org/en/docs/varindex.html.
进程
[root@centos6 logs]# ps -ef | grep nginx
root 1451 1 0 Oct25 ? 00:00:00 nginx: master process /opt/nginx/sbin/nginx
nobody 1676 1451 0 00:53 ? 00:00:00 nginx: worker process
root 1697 1408 0 01:06 pts/0 00:00:00 grep nginx
一个master进程
:负责管理"worker"进程的进程,除了管理"worker"进程, "master"进程还负责读取配置文件、判断配置文件语法的工作,“master进程"也叫"主进程”, 在nginx中,"master"进程只能有一个。
一个worker进程
:真正负责处理请求的进程;可通过配置指令"worker_processes"配置。
配置worker_processes
worker_processes的配置服务器cpu核心有关,尽量让每个worker进程都有一个cpu可以使用,尽量避免了多个worker进程抢占同一个cpu的情况。
worker_processes可以如下配置数字:
worker_processes 1;
worker_processes可以如下配置auto:
值为auto时,nginx会自动检测当前主机的cpu核心数量,并启动对应数量的worker进程。
worker_processes auto;
worker进程与cpu核心绑定
为了避免进程在cpu间切换时产生性能损耗,可以将worker进程与cpu核心进行"绑定",此时worker进程可以更好的使用某个cpu核心上的缓存,从而减少worker进程在cpu间切换而带来的缓存失效,此时需要配置指令"worker_cpu_affinity"。
"cpu掩码"若当前cpu有4个核,则表示为0000
第一个核心:0001
第二个核心:0010
第三个核心:0100
第四个核心:1000
启动2个worker进程,分别绑定到"01"核"与"10"核上:
worker_processes 2;
worker_cpu_affinity 01 10;
nginx会自动将worker进程平均的绑定到可用的核心上:
worker_cpu_affinity auto;
worker进程运行身份:
指定worker进程以nginx用户的身份运行:
user nginx;
"cache manager"和"cache loader"进程
nginx服务除了master进程和worker进程,还有"cache manager"进程和"cache loader"进程,这两个进程也受master进程管理。
当nginx作为反向代理服务并且开启缓存功能时,我们才能看到这两个进程,"cache manager"进程和"cache loader"进程负责管理和加载缓存,缓存被多个worker进程共享使用。
日志滚动
每天凌晨1点产生一个日志文件,如access-20201025.log。
方法一:编写脚本完成日志滚动。
nginx -s reopen
方法二:使用日志滚动软件"logrotate"完成日志滚动。
kill -USR1 `cat /opt/nginx/logs/nginx.pid`
logrotate是一个日志管理工具。
TERM或者INT:表示立即停止nginx:stop
QUIT:表示优雅的停止nginx:quit
HUP:重载配置文件:reload
USR1:重新打开日志文件:re