-
静态网站服务:
Apache服务
nginx服务
-
动态网站服务:
Tomcat服务
PHP
2.nginx网站服务特点
(1)nginx具有高并发(特别是静态资源)、占用系统资源少等特性。
(2)nginx不但是一个优秀的web软件,还具有反向代理负载均衡和缓存服务功能。
(3)支持多平台部署安装
(4)nginx实现网络通讯时使用的是异步网络IO模型:epoll模型
网络模型:
-
epoll模型:epoll是Linux内核为处理大批量文件描述符而作了改进的poll,是Linux下多路复用IO接口select/poll的增强版本,它能显著提高程序在大量并发连接中只有少量活跃的情况下的系统CPU利用率。另一点原因就是获取事件的时候,它无须遍历整个被侦听的描述符集,只要遍历那些被内核IO事件异步唤醒而加入Ready队列的描述符集合就行了。epoll除了提供select/poll那种IO事件的水平触发(Level Triggered)外,还提供了边缘触发(Edge Triggered),这就使得用户空间程序有可能缓存IO状态,减少epoll_wait/epoll_pwait的调用,提高应用程序效率。
-
select模型:select需要遍历监视的文件描述符,并且这个描述符的数组还有最大的限制。随着文件描述符数量的增长,用户态和内核的地址空间的复制所引发的开销也会线性增长。即使监视的文件描述符长时间不活跃了,select还是会线性扫描
3.nginx服务部署安装
(1)yum安装
a.使用官方的yum源进行安装,安装的是最新版本,软件目录结构比价标准(推荐)
http://nginx.org/en/linux_packages.html#RHEL-CentOS
[root@web01 /server/tools]# vim /etc/yum.repos.d/nginx.repo [nginx-stable] name=nginx stable repo baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=1 (#安装有问题改成0或注释掉) enabled=1 gpgkey=https://nginx.org/keys/nginx_signing.key module_hotfixes=true [root@web01 /server/tools]# yum install nginx -y
b.使用非官方yum源进行安装,安装的不是最新版本,目录结构会发生变化
更换阿里源,或清华源
[root@web01 /server/tools]# yum install nginx -y [root@web01 /etc/yum.repos.d]# systemctl start nginx
(2)编译安装
a.下载软件
[root@web01 ~]# mkdir -p /server/tools [root@web01 /server/tools]# wget http://nginx.org/download/nginx-1.18.0.tar.gz
b.解决软件依赖
[root@web01 /server/tools]# yum install -y openssl-devel pcre-devel [root@web01 /server/tools]# ./configure --prefix=/root/nginx --user==nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module
ps:如果编译安装软件不知道需要哪些依赖包,可以先继续编译,报错的时候回提示安装依赖包
c.解压软件,进入软件目录
[root@web01 /server/tools]# tar -xf nginx-1.18.0.tar.gz [root@web01 /server/tools]# ls nginx-1.18.0 nginx-1.18.0.tar.gz [root@web01 /server/tools]# cd nginx-1.18.0/ [root@web01 /server/tools/nginx-1.18.0]# ls auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
d.进行编译安装三部曲
-
进行配置操作
[root@web01 /server/tools/nginx-1.18.0]# ./configure --help [root@web01 /server/tools/nginx-1.18.0]# ./configure --prefix=/server/tools --user=nginx --group=nginx --with-http_ssl_module
参数 含义 --prefix=PATH 指定安装目录 --user=USER 设置一个虚拟用户管理worker进程 --group=GROUP 设置一个虚拟用户组管理worker进程 --error-log-path=PATH 指定错误日志的目录位置 --with-http_ssl_module 实现https访问 --without-http_ssi_module 关闭功能 -
进行软件的编译
[root@backup ~/nginx-1.18.0]# make
-
编译安装
[root@backup ~/nginx-1.18.0]# make install [root@backup ~/nginx-1.18.0]# ll /server/tools/nginx total 0 drwxr-xr-x 2 root root 333 Sep 16 16:41 conf drwxr-xr-x 2 root root 40 Sep 16 16:41 html drwxr-xr-x 2 root root 6 Sep 16 16:41 logs drwxr-xr-x 2 root root 19 Sep 16 16:41 sbin [root@backup /server/tools/sbin]# useradd nginx -M -s /sbin/nologin [root@backup /server/tools/sbin]# id nginx uid=1004(nginx) gid=1004(nginx) groups=1004(nginx) [root@backup /server/tools/sbin]# ./nginx [root@backup /server/tools/sbin]# netstat -lntup | grep nginx tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 31391/nginx: master [root@backup /server/tools/sbin]# ./nginx -s {reload|stop| quit|reopen|} #重启,停止
4.nginx目录结构
路径信息 | 作用说明 |
---|---|
/etc/logrotate/nginx | 用于日志轮训切割 |
/etc/nginx /etc/nginx/conf.d /etc/nginx/conf.d/default.conf /etc/nginx/nginx.conf | nginx主配置文件 |
/etc/nginx/scgi_params /etc/nginx/uwsgi_params /etc/nginx/fastcgi_params | cgi、fastcgi、uwcgi配置文件 |
/etc/nginx/koi-utf /etc/nginx/koi-win /etc/nginx/win-utf | nginx编码转换映射文件 |
/etc/nginx/mime.types | http协议的content-type与扩展名 |
/usr/lib/systemd/system/nginx-debug.service /usr/lib/systemd/system/nginx.service | nginx服务守护进程管理文件 |
/etc/nginx/modules /usr/lib64/nginx /usr/lib64/nginx/modules | nginx模块目录 |
/usr/sbin/nginx /usr/sbin/nginx-debug | nginx终端管理命令 |
/usr/share/nginx /usr/share/nginx/html /usr/share/nginx/html/50x.html /usr/share/nginx/html/index.html | nginx默认站点目录 |
/var/cache/nginx | nginx缓存目录信息 |
/var/log/nginx | nginx日志信息目录 |
PS:日志切割方法
-
利用脚本实现切割
#!/bin/bash mv /var/log/nginx/access.log /var/log/nginx/access_$(date +%F).log systemctl restart nginx
-
利用logrotate进行切割
[root@web01 ~]# egrep -v "^#|^$" /etc/logrotate.conf weekly --- 定义默认日志切割的周期(月,周,日) rotate 4 --- 定义只保留几个切割后的文件 create --- 创建出一个相同的源文件 dateext --- 定义角标(扩展名称信息,按照时间) compress --- 是否对切割后的文件进行压缩处理 include /etc/logrotate.d --- 加载包含/etc/logrotate.d/目录中文件配置 /var/log/wtmp { --- 单独对某个文件进行切割配置 monthly create 0664 root utmp ——— 切割之后的文件权限,及属主属组 minsize 1M --- 最小大小为1M,小于1M不进行切割 rotate 1 } /var/log/btmp { missingok monthly create 0600 root utmp rotate 1 }
5.nginx配置文件默认参数说明
5.1主配置文件
[root@web01 ~]# cat /etc/nginx/nginx.conf #1.配置文件主区域 user nginx; --- 定义worker进程管理的用户 worker_processes 1; --- 开启几个worker进程(等于CPU核心数或核心数的2倍) error_log /var/log/nginx/error.log warn; --- 错误日志目录 pid /var/run/nginx.pid; --- pid文件位置 #2.配置文件事件区域 events { worker_connections 1024; --- worker请求接收最大请求数 } #3.配置http区域 http { include /etc/nginx/mime.types; --- 加载一个文件类型的配置文件 default_type application/octet-stream; --- 指定默认识别文件类型 log_format main '$remote_addr - $remote_user [$time_local] "$request" ' --- 定义日志格式 '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; --- 定义日志路径 sendfile on; #tcp_nopush on; keepalive_timeout 65; --- 定义超时时间 #gzip on; include /etc/nginx/conf.d/*.conf; --- 加载站点配置文件 }
5.2站点目录文件
[root@web01 ~]# egrep -v "(^$|#)" /etc/nginx/conf.d/default.conf #4.sever区域配置 server { listen 80; --- 指定监听端口 server_name localhost; --- 指定网站的域名 location / { root /usr/share/nginx/html; --- 定义站点目录的路径 index index.html index.htm; --- 定义网站首页文件 } error_page 500 502 503 504 /50x.html; --- 定义网站错误网页 location = /50x.html { root /usr/share/nginx/html; --- 定义错网页路径 } }
6.nginx服务的企业应用
(1)利用nginx搭建一个简单的网站
a.配置nginx官方yum源
[root@web01 /server/tools]# vim /etc/yum.repos.d/nginx.repo [nginx-stable] name=nginx stable repo baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=1 (#安装有问题改成0或注释掉) enabled=1 gpgkey=https://nginx.org/keys/nginx_signing.key module_hotfixes=true
b.下载安装软件
[root@web01 ~]# yum install nginx -y
c.配置站点文件
[root@web01 /www/share]# cat /etc/nginx/conf.d/www.conf server{ listen 80; server_name www.ashuai.com; location / { root /www/share; index index.html; }
d.上传网站主页到对应目录中
[root@web01 /www]# tree /www /www └── share └── index.html
f.启动nginx服务
[root@web01 ~]# systemctl start nginx [root@web01 ~]# netstat -lntup | grep nginx tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 8703/nginx: master
g.配置hosts文件
[root@web01 ~]# vim /etc/hosts
ps:如果是公网IP则在平台添加解析
h.测试
7.企业中虚拟主机访问方式
(1)基于域名的方式进行访问
server{ listen 80; server_name www.ashuai.com; location / { root /www/share; index index.html; }
(2)基于IP地址的方式进行访问(需要重启服务,才能生效restart)
server{ listen 10.0.0.7:80; server_name www.ashuai.com; location / { root /www/share; index index.html; }
(3)基于端口的方式进行访问
server{ listen 8080; server_name www.ashuai.com; location / { root /www/share; index index.html; }
PS:没有相同域名的server主机,会找满足端口要求的第一个主机
8.nginx服务常见模块的应用
模块 | 含义 | 示例 | 对应区域 |
---|---|---|---|
访问安全模块 | |||
ngx_http_access_module | 允许限制对某些客户端地址的访问。 | location / { deny 192.168.1.1; allow 192.168.1.0/24; allow 10.1.1.0/16; allow 2001:0db8::/32; deny all; } | http, server, location, limit_except |
ngx_http_auth_basic_module | 允许通过使用“HTTP基本身份验证”协议验证用户名和密码来限制对资源的访问。 | location / { auth_basic "closed site"; auth_basic_user_file conf/htpasswd; } | http, server, location, limit_except |
ngx_http_autoindex_module | 处理以斜杠字符('/')结尾的请求并生成目录列表。 | location / { autoindex on; } | http, server, location |
ngx_http_stub_status_module | 模块提供对基本状态信息的访问 | location = /basic_status { stub_status; } | server, location |
location | 根据请求URI设置配置。 | location [ = | ~ | ~* | ^~ ] uri { ... } location @name { ... } |
server, location |
ngx_http_auth_basic_module
模块说明:
location / { auth_basic "closed site"; ------->"closed site":描述信息 auth_basic_user_file password/htpasswd; ------->"conf/htpasswd":密码文件存放目录(最好使用相对路径) } ##创建密码文件目录 [root@web01 ~]# cd /etc/nginx/ [root@web01 /etc/nginx]# mkdir password ##使用htpasswd创建密码文件 [root@web01 /etc/nginx/conf.d]# yum install httpd-tools -y [root@web01 /etc/nginx]# htpasswd -bc ./password/htpasswd shuai 123 Adding password for user shuai [root@web01 /etc/nginx]# cat ./password/htpasswd shuai:$apr1$AjtERxjc$.IkOME8TUx1jzxn/MhEwb1 #修改密码文件权限 [root@web01 /etc/nginx/password]# chmod 600 htpasswd [root@web01 /etc/nginx/password]# chown nginx htpasswd
8.1htpasswd参数说明
-c Create a new file. ***** 创建一个密码文件 -n Don't update file; display results on stdout. 不会更新文件; 显示文件内容信息 -b Use the password from the command line rather than prompting for it. ***** 免交互方式输入用户密码信息 -i Read password from stdin without verification (for script usage). 读取密码采用标准输入方式,并不做检查 ??? -m Force MD5 encryption of the password (default). md5的加密算法 -B Force bcrypt encryption of the password (very secure). 使用bcrypt对密码进行加密 -C Set the computing time used for the bcrypt algorithm (higher is more secure but slower, default: 5, valid: 4 to 31). 使用bcrypt algorithm对密码进行加密 -d Force CRYPT encryption of the password (8 chars max, insecure). 密码加密方式 -s Force SHA encryption of the password (insecure). 加密方式 -p Do not encrypt the password (plaintext, insecure). 不进行加密 -D Delete the specified user. 删除指定用户
ngx_http_autoindex_module
模块说明:
[root@web01 /www/share]# mv index.html shuai.html [root@web01 /www/share]# tree . ├── shuai01 │ └── test.txt ├── shuai02 │ └── test.txt └── shuai.html [root@web01 /www/share]# vim /etc/nginx/conf.d/www.conf server{ listen 80; server_name www.ashuai.com; location / { root /www/share; index index.html; #主页文件修改名称或直接删除 auth_basic "shuai"; auth_basic_user_file password/htpasswd; autoindex on; #开启功能 charset utf-8; #目录中有中文需要设置字符集,不加则是乱码 } }
#PS:如果文件只打开不下载的话修改媒体资源类型文件。如果只想打开文件查看不下载,则将对应的文件格式添加即可。 [root@web01 /etc/nginx]# cat /etc/nginx/mime.types
ngx_http_stub_status_module
模块说明:
[root@web01 /etc/nginx/conf.d]# vim status.conf server{ listen 80; server_name status.shuai.com; location / { stub_status; } }
8.2监控参数说明
参数 | 说明 |
---|---|
Active connections | 当前活动客户端连接数(包括等待连接数) |
accepts | 接受的客户端连接总数 |
handled | 已处理的连接总数 |
requests | 客户端请求总数 |
Reading | nginx正在读取请求头的当前连接数 |
Writing | nginx将响应写回客户端的当前连接数。 |
Waiting | 当前等待请求的空闲客户端连接数。 |
location
模块说明
location = / { ------>精确匹配,优先级最高 [ configuration A ] } location / { ------>默认匹配,优先度最低,别的location都不具备匹配条件则匹配/ [ configuration B ] } location /documents/ { ------>按照目录进行匹配 ,优先级3级 [ configuration C ] } location ^~ /images/ { ------>优先匹配 ,优先级2级 [ configuration D ] } location ~* \.(gif|jpg|jpeg)$ { ------>不区分大小写进行匹配 ,优先级3级 [ configuration E ] }
说明
[root@web01 /etc/nginx/conf.d]# vim test.conf server{ listen 80; server_name test.shuai.com; location = / { return 404; } location / { return 403; } location /documents/ { return 501; } location ^~ /images/ { return 502; } location ~* \.(gif|jpg|jpeg)$ { return 500; }
(1)访问test.shuai.com
(2)访问http://test.shuai.com/documents/
(3)访问http://test.shuai.com/images/shuai.jpg
9.nginx日志功能
访问日志: /var/log/nginx/access.log
ngx_http_log_module log_format main '$remote_addr - $remote_user [$time_local] "$request" ' #定义日志格式,只能在http模块定义 '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /spool/logs/nginx-access.log main buffer=32k; #调用日志格式,可以在http, server, location模块定义,根据不同网站调用不同的日志格式
参数 | 含义 |
---|---|
$remote_addr | 客户端IP地址 |
$remote_user | 显示认证的用户名信息 |
$time_local | 访问网站时间 |
$request | 请求报文的请求行信息 |
$status | 用户访问网站状态码信息 |
$body_bytes_sent | 显示响应的数据尺寸信息 |
$http_referer | 记录调用网站资源的连接地址信息(防止用户盗链) |
$http_user_agent | 记录用户使用什么客户端软件进行访问页面的 (谷歌 火狐 IE 安卓 iphone) |
$http_x_forwarded_for | 负载均衡 |
错误日志: /var/log/nginx/error.log
Syntax: error_log file [level];
Default: error_log logs/error.log error;
Context: main, http, mail, stream, server, location
error_log /var/log/nginx/error.log warn;
级别 | 含义 |
---|---|
debug | 调试级别, 服务运行的状态信息和错误信息详细显示 |
info | 信息级别, 只显示重要的运行信息和错误信息 |
notice | 通知级别: 更加重要的信息进行通知说明 |
warn | 警告级别: 可能出现了一些错误信息,但不影响服务运行 |
error | 错误级别: 服务运行已经出现了错误,需要进行纠正 (推荐选择) |
crit | 严重级别: 必须进行修改调整 |
alert | 严重警告级别: 即警告,而且必须进行错误修改 |
emerg | 灾难级别: 服务已经不能正常运行 |
PS:日志文件信息需要提前做切割处理
10.rewrite跳转
[root@web01 /etc/nginx/conf.d]# cat www.conf server{ listen 80; server_name ashuai.com; rewrite ^/(.*) http://www.ashuai.com/$1 permanent; } server{ listen 80; server_name www.ashuai.com; location / { root /www/share; index index.html; # auth_basic "shuai"; # auth_basic_user_file password/htpasswd; autoindex on; charset utf-8; } }