Nginx常见版本有4个
nginx安装
yum search 可以搜索有什么版本的nginx
yum -y install nginx直接安装就行,默认是装的nginx.x86_64这个版本
[root@WIND system]# yum search nginx
Last metadata expiration check: 2:19:42 ago on Sat 09 Apr 2022 03:14:21 PM CST.
================================================= Name Exactly Matched: nginx =================================================
nginx.x86_64 : A high performance web server and reverse proxy server
================================================ Name & Summary Matched: nginx ================================================
collectd-nginx.x86_64 : Nginx plugin for collectd
munin-nginx.noarch : NGINX support for Munin resource monitoring
nginx-all-modules.noarch : A meta package that installs all available Nginx modules
nginx-filesystem.noarch : The basic directory layout for the Nginx server
nginx-mod-http-image-filter.x86_64 : Nginx HTTP image filter module
nginx-mod-http-perl.x86_64 : Nginx HTTP perl module
nginx-mod-http-xslt-filter.x86_64 : Nginx XSLT module
nginx-mod-mail.x86_64 : Nginx mail modules
nginx-mod-stream.x86_64 : Nginx stream modules
pagure-web-nginx.noarch : Nginx configuration for Pagure
pcp-pmda-nginx.x86_64 : Performance Co-Pilot (PCP) metrics for the Nginx Webserver
python3-certbot-nginx.noarch : The nginx plugin for certbot
[root@WIND system]#
[root@WIND system]# yum -y install nginx
安装好的nginx,配置文件默认存放在/etc/nginx这里
[root@WIND nginx]# pwd
/etc/nginx
[root@WIND nginx]#
[root@WIND nginx]# ls
conf.d fastcgi.conf.default html koi-win nginx.conf scgi_params.default win-utf
default.d fastcgi_params keys mime.types nginx.conf.default uwsgi_params
fastcgi.conf fastcgi_params.default koi-utf mime.types.default scgi_params uwsgi_params.default
[root@WIND nginx]#
nginx常用命令
nginx ##启动
nginx -s stop ###快速停止
nginx -s reload ###重新加载配置
nginx -s quit #优雅地关闭nginx,关闭之前,先把目前的连接处理完
如果用rpm或者yum命令安装的nginx,装好之后,nginx会被注册成系统服务,可以通过系统命令来操作nginx,非常方便
[root@WIND system]# pwd
/usr/lib/systemd/system
[root@WIND system]# ls | grep nginx
nginx.service
nginx.service.d
[root@WIND system]# cat nginx.service
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/run/nginx.pid
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running `nginx -t` from the cmdline.
# https://bugzilla.redhat.com/show_bug.cgi?id=1268621
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=mixed
PrivateTmp=true
[Install]
WantedBy=multi-user.target
nginx启动先读取配置文件
校验没有问题,就开启子进程,worker处理业务
主进程Master不处理业务,协调子进程去处理业务
[root@WIND ~]# ps aux | grep nginx
root 415944 0.0 0.0 118912 2344 ? Ss Apr06 0:00 nginx: master process /usr/sbin/nginx
nginx 415945 0.0 0.3 150896 12900 ? S Apr06 0:00 nginx: worker process
nginx 415946 0.0 0.2 150604 11484 ? S Apr06 0:00 nginx: worker process
root 627017 0.0 0.0 12108 968 pts/0 R+ 17:27 0:00 grep --color=auto nginx
[root@WIND ~]#
nginx最小配置
在/etc/nginx/找到nginx.conf.default文件,把带有#号的行和空行都去掉
[root@WIND ~]# cat nginx.conf.default
worker_processes 1; #设置启动多少个worker进程,可以是auto,一般是对应到服务器的物理内核的个数
events { ##nginx事件驱动模块
worker_connections 1024;
}
http {
include mime.types; ##include命令可以把其他配置文件包含进来,mime.types由服务器端来定义,返回的请求的类型给浏览器,告诉浏览器这是什么文件,而不是让浏览器根据文件后缀名来判断请求的文件类型,在mime.types里面定义好的,浏览器直接解析,没定义好的,弹框提示下载
default_type application/octet-stream;##默认传输octet-stream这种格式的数据流给客户端(也就是浏览器)
sendfile on;##数据零拷贝,减少调度和数据拷贝的过程
keepalive_timeout 65;##保持连接,超时时间
server { ##虚拟主机vhost,可以有多个,一个server代表一个主机,每个server要用不同的端口号
listen 80; ##端口号
server_name localhost; ##域名、主机名
location / { ##uri,如http://abc.com/xx/b.html, abc.com就是域名,xx/b.html就是rui
root html; ##uri目录
index index.html index.htm; ##默认文件
}
error_page 500 502 503 504 /50x.html; ##发送服务器端错误的时候,返回默认页面的内容
location = /50x.html {
root html;
}
}
}
sendfile on/off的原理
sendfile on的时候,客户端请求的文件,nginx直接告诉系统网络接口,要读什么文件发给客户端,让网络接口直接去读对应的文件
如果是off,nginx应用程序本身会先去读文件,把文件读到nginx应用程序的内存里,然后再把文件内容发给系统网络接口,系统网络接口再把数据发送给客户端(浏览器),
这个参数on的时候,减少文件的读取和拷贝步骤