$ systemctl list-units --type=service | grep nginx
nginx.service loaded active running A high performance web server and a reverse proxy server
$ systemctl status nginx.service
● nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: active (running) since 六 2021-11-13 16:01:15 CST; 6min ago
Main PID: 25575 (nginx)
CGroup: /system.slice/nginx.service
├─25575 nginx: master process /usr/sbin/nginx -g daemon on; master_process on
├─25576 nginx: worker process
└─25577 nginx: worker process
11月 13 16:01:15 ubuntu systemd[1]: Starting A high performance web server and a reverse proxy server...
11月 13 16:01:15 ubuntu systemd[1]: Started A high performance web server and a reverse proxy server.
$ cat /lib/systemd/system/nginx.service
# Stop dance for nginx
# =======================
#
# ExecStop sends SIGSTOP (graceful stop) to the nginx process.
# If, after 5s (--retry QUIT/5) nginx is still running, systemd takes control
# and sends SIGTERM (fast shutdown) to the main process.
# After another 5s (TimeoutStopSec=5), and if nginx is alive, systemd sends
# SIGKILL to all the remaining processes in the process group (KillMode=mixed).
#
# nginx signals reference doc:
# http://nginx.org/en/docs/control.html
#
[Unit]
Description=A high performance web server and a reverse proxy server
After=network.target
[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t -q -g 'daemon on; master_process on;'
ExecStart=/usr/sbin/nginx -g 'daemon on; master_process on;'
ExecReload=/usr/sbin/nginx -g 'daemon on; master_process on;' -s reload
ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid
TimeoutStopSec=5
KillMode=mixed
[Install]
WantedBy=multi-user.target
# root @ ubuntu in /etc/systemd/system [16:11:17]
$ /usr/sbin/nginx -T | grep include
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
include /etc/nginx/mime.types;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
# include snippets/snakeoil.conf;
# include snippets/fastcgi-php.conf;
内容摘录自:https://www.qycn.com/xzx/article/9798.html
... #全局指令块
events {
... # events指令快
}
http {
... # http指令块
server {
.... # server指令块
location {
... # location指令快
}
}
}
./conf/nginx.conf 即直接给命令的配置文件
* user
指定nginx的工作进程的用户及用户组,默认是nobody用户。
* worker_processes
指定工作进程的个数,默认是1个。
具体可以根据服务器cpu数量进行设置,比如cpu有4个,可以设置为4。
如果不知道cpu的数量,可以设置为auto。
nginx会自动判断服务器的cpu个数,并设置相应的进程数。
* error_log
设置nginx的错误日志路径,并设置相应的输出级别。
如果编译时没有指定编译调试模块,那么 info就是最详细的输出模式了。
如果有编译debug模块,那么debug时最为详细的输出模式。这里设置为默认就好了。
* pid
指定nginx进程pid的文件路径。
* events
这个指令块用来设置工作进程的工作模式以及每个进程的连接上限。
* use
用来指定nginx的工作模式,通常选择epoll,除了epoll,还有select,poll。
* worker_connections
定义每个工作进程的最大连接数,默认是1024。
客户端最大连接数,就要考虑有几个工作进程了,两者相乘就是的。
当nginx作反向代理时,需要除以2.
* ps
进程的最大连接数受linux系统进程的最大打开文件数限制。
比如ulimit -n 默认时1024,如果不改变这个值,
即使修改工作进程为2048,也无法处理这么多连接。
* include
主要是可以引用指定的配置文件。
* default_type
设定默认类型为二进制流,也就是当文件类型未定义时使用这种方式,
例如在没有配置php环境时,nginx是不予解析的,
此时,用浏览器访问php文件就会出现下载窗口。
* log_format
定义日志文件格式,并默认取名为main,可以自定义该名字。
也可以通过添加,删除变量来自定义日志文件的格式。
* access_log
定义访问日志的存放路径,并且通过引用log_format所定义的main名称设置其输出格式。
* sendfile
用于开启高效文件传输模式。将tcp_nopush和tcp_nodelay两个指令设置为on用于防止网络阻塞;
* keepalive_timeout
设置客户端连接保持活动的超时时间。在超过这个时间之后,服务器会关闭该连接。
* gzipon
开启压缩功能,减少文件传输大小,节省带宽。
* client_max_body_size
用来设置允许客户端请求的最大的单个文件字节数
* client_header_buffer_size
用于指定来自客户端请求头的header_buffer大小。
对于大多数请求,1k的缓冲区大小已经足够,
如果自定义了消息头或有更大的cookie,可以增加缓冲区大小。这里设置为32k
* large_client_header_buffers
用来指定客户端请求中较大的消息头的缓存最大数量和大小,
“4”为个数,“128k”为大小,最大缓存量为4个128k
* client_header_timeout
设置客户端请求头读取超时时间。如果超过这个时间,
客户端还没有发送任何数据,nginx将返回“request time out(408)”错误
* client_body_timeout
设置客户端请求主体读取超时时间。
如果超过这个时间,客户端还没有发送任何数据,nginx将返回“request time out(408)”错误,默认值是60
* send_timeout
指定响应客户端的超时时间。这个超时仅限于两个连接活动之间的时间,
如果超过这个时间,客户端没有任何活动,nginx将会关闭连接。
* server
用来定义虚拟主机。
* listen
设置监听端口,默认为80端口
* server_name
域名,多个域名通过逗号隔开
* charset
设置网页的默认编码格式
* access_log
指定该虚拟主机的独立访问日志,会覆盖前面的全局配置。
* index
设置默认的索引文件
* location
定义请求匹配规则。
* error_page
定义访问错误返回的页面,凡是状态码是500 502 503 504 都会返回这个页面。
* location
匹配到这条规则
* root
文件存放的目录
* fastcgi_pass
指定php-fpm进程管理的ip端口或者unix套接字
* fastcgi_index
指定php脚本目录下的索引文件
* fastcgi_param
指定传递给fastcgi服务器的参数