1. 常用的网站服务软件
1.1 处理静态资源服务
1.1.1 apache
1.1.2 nginx
1.2 处理动态资源服务
1.2.1 PHP
终端浏览器进行访问
1.2.2 Tomcat (java)
利用 移动端 查看网页
1.2.3 Python
开发难度较低
2. Nginx 服务软件特点
2.1 特点
-
高并发,消耗内存资源少
-
具备多种功能
- 具备 web 服务功能 -- Apache
- 具备负载均衡功能 -- LVS
- 网站缓存服务 -- Squid
-
支持多种系统平台部署
-
Nginx 实现网络通讯时使用的是 异步网络IO模型:epoll 模型 (Apache 使用 select 模型)
2.2 扩展 - epoll 模型和 select 模型区别
-
epoll
如:宿管找人,查看人员登记信息
-
select
如:宿管找人,逐个房间去找
线性轮询
-
两者区别
指标 | select | epoll |
---|---|---|
性能 | 随着连接数的增加性能急剧下降。处理成千上万并发连接数,性能很差 | 随着连接数的增加,性能基本不变。处理成千上万并发连接时,性能很好。 |
连接数 | 连接数有限制。处理的最大连接数不超过1024,如果要处理的连接数超过1024个,则需要修改 FD_SETSIZE 宏,并重新编译 | 连接数无限制 |
内存处理机制 | 线性轮询 | 回调 callback |
开发复杂性 | 低 | 中 |
3. Nginx 安装部署
3.1 部署方式
3.1.1 yum 安装
- 使用官方 yum 源进行安装,安装的是最新版本 (软件目录结构标准,推荐使用)
- 使用非官方 yum 源安装,安装的不是最新版本 (软件目录结构有变化)
3.1.2 编译安装
相比 yum 安装 更加灵活,但是要解决依赖安装包
- 源码包下载,并解决依赖
[root@web01 ~]# cd /server/tools/
[root@web01 tools]# wget http://nginx.org/download/nginx-1.16.1.tar.gz
[root@web01 tools]# ll
total 1012
-rw-r--r-- 1 root root 1032630 Aug 14 2019 nginx-1.16.1.tar.gz
软件依赖
openssl-devel
pcre-devel
-
解压并编译安装
- 配置
./configure # 查看参数 [root@web01 nginx-1.16.1]# ./configure --help --prefix=PATH -- 指定安装路径 --user=USER -- 设置一个虚拟用户管理 worker 进程(安全考虑) --group=GROUP -- 设置一个虚拟用户组管理 worker 进程(安全考虑) --with-http_ssl_module --with-…… -- 安装 时带有某些模块的功能
- 进行软件编译
make
- 安装
make install
4. Nginx 目录结构信息
路径信息 | 类型信息 | 作用说明 |
---|---|---|
/etc/logrotate.d/nginx | 配置文件 | 用于日志文件轮询切割 |
/etc/nginx /etc/nginx/conf.d /etc/nginx/conf.d/default.conf /etc/nginx/nginx.conf |
配置目录 配置文件 |
Nginx 主配置文件 |
/etc/nginx/fastcgi_params /etc/nginx/scgi_params /etc/nginx/uwsgi_params |
配置文件 | fastcgi、cgi、uwcgi 配置文件 |
/etc/nginx/koi-utf | 配置文件 | Nginx 编码转换映射文件 |
/etc/nginx/koi-win | 配置文件 | Nginx 编码转换映射文件 |
/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/modules /usr/lib64/nginx |
目录信息 | Nginx 模块目录 |
/usr/sbin/nginx /usr/sbin/nginx-debug |
命令信息 | Nginx 终端管理命令 |
/usr/share/doc/nginx-1.20.2 /usr/share/doc/nginx-1.20.2/COPYRIGHT /usr/share/man/man8/nginx.8.gz |
目录信息 | 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 日志文件目录 |
4.1 /etc/logrotate.d
功能:实现 Nginx 日志文件定时切割处理
4.1.1 扩展 - 日志切割方法
- 利用脚本切割 和 定时任务
#!/bin/bash
mv /var/log/nginx/access.log /var/log/nginx/access_$(date +%F).log
systemctl restart nginx
- 利用专用文件切割程序 - logrotate
[root@web01 ~]# cat /etc/logrotate.conf
# see "man logrotate" for details
# rotate log files weekly
weekly -- 定义默认日志切割的周期
# keep 4 weeks worth of backlogs
rotate 4 -- 定义只保留几个切割后的文件
# create new (empty) log files after rotating old ones
create -- 创建出一个相同的原文件
# use date as a suffix of the rotated file
dateext -- 定义切割后文件的角标信息(扩展名称信息)
# uncomment this if you want your log files compressed
#compress -- 是否对切割后的文件进行压缩处理
# RPM packages drop log rotation information into this directory
include /etc/logrotate.d -- 加载包含 /etc/logrotate.d 目录中文件配置
# no packages own wtmp and btmp -- we'll rotate them here
/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
}
# system-specific logs may be also be configured here.
[root@web01 ~]# cat /etc/logrotate.d/syslog
/var/log/cron
/var/log/maillog
/var/log/messages
/var/log/secure
/var/log/spooler
{
missingok
sharedscripts
postrotate
/bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true
endscript
}
[root@web01 ~]# cat /etc/logrotate.d/nginx
/var/log/nginx/*.log {
daily
missingok
rotate 52
compress
delaycompress
notifempty
create 640 nginx adm
sharedscripts
postrotate -- 切割后操作
if [ -f /var/run/nginx.pid ]; then
kill -USR1 `cat /var/run/nginx.pid` -- 切割后将 nginx 平滑重启,CnetOS7中无USR1参数
fi
endscript
}
4.2 Nginx 服务配置文件
4.2.1 Nginx 主配置文件 (/etc/nginx/nginx.conf)
[root@web01 ~]# cat /etc/nginx/nginx.conf
user nginx; -- 定义 worker 进程管理用户
worker_processes auto; -- 定义有几个 worker 进程 (不能超过CPU核数)
error_log /var/log/nginx/error.log notice; -- 定义错误日志路径信息
pid /var/run/nginx.pid; -- 定义pid文件路径信息(若应用启动,该文件存在)
events {
worker_connections 1024; -- 一个 worker 进程可以同时接收 1024 个访问请求
}
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; -- 超时时间(短链接可设置为0)
#gzip on;
include /etc/nginx/conf.d/*.conf; -- 加载配置文件
}
4.2.2 扩展配置文件 (/etc/nginx/conf.d/default.conf )
[root@web01 ~]# cat /etc/nginx/conf.d/default.conf
server {
listen 80; -- 指定监听端口
server_name localhost; -- 指定网站域名
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html; -- 定义站点目录位置
index index.html index.htm; -- 定义首页文件
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html; -- 优雅显示页面信息
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
扩展 - Nginx 服务进程
[root@web01 nginx]# ps -ef|grep nginx
root 23569 1 0 16:52 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx 23570 23569 0 16:52 ? 00:00:00 nginx: worker process
root 23572 23213 0 16:52 pts/0 00:00:00 grep --color=auto nginx
# 解释说明
master process -- 主进程,管理服务是否能正常运行
worker process -- 工作进程,处理用户的访问请求
5. Nginx 服务企业应用
5.1 利用 Nginx 服务搭建一个网站
- 编写虚拟主机配置文件
[root@web01 ~]# vim /etc/nginx/conf.d/www.conf
server {
listen 8080;
server_name www.yunxuanedu.com;
charset utf-8;
location / {
root /html/www;
index index.html;
}
}
- 获取开发人员编写的网站代码
- 重启 Nginx 服务(平滑重启)
systemctl reload nginx
nginx -s reload
# 扩展:
nginx -t --检查nginx配置文件语法问题
nginx -T --检查nginx配置文件语法问题,同时将所有配置打印到屏幕上
nginx -s [reload/stop] --重启或者停止服务
- 编写 DNS 配置
- 测试访问,通过域名
5.2 网站访问常见错误
5.2.1 404 错误
- nginx 配置文件错误 (location)
- 站点中缺少访问的资源
5.2.2 403 错误
- 站点禁止访问
- 没有首页文件
5.3 利用 Nginx 搭建多个网站
5.3.1 编写配置文件
[root@web01 conf.d]# ll
total 16
-rw-r--r-- 1 root root 133 Dec 20 17:37 bbs.conf
-rw-r--r-- 1 root root 135 Dec 20 17:37 blog.conf
-rw-r--r-- 1 root root 1072 Dec 20 12:20 default.conf
-rw-r--r-- 1 root root 133 Dec 20 17:35 www.conf
[root@web01 conf.d]# cat bbs.conf
server {
listen 80;
server_name bbs.yunxuanedu.com;
location / {
root /html/bbs;
index index.html;
}
}
[root@web01 conf.d]# cat blog.conf
server {
listen 80;
server_name blog.yunxuanedu.com;
location / {
root /html/blog;
index index.html;
}
}
[root@web01 conf.d]# cat www.conf
server {
listen 80;
server_name www.yunxuanedu.com;
location / {
root /html/www;
index index.html;
}
}
5.3.2 创建站点目录和首页文件
[root@web01 ~]# mkdir /html/{www,blog,bbs} -p
[root@web01 ~]# for name in {www,blog,bbs} ; do echo "10.1.1.7 $name.yunxuanedu.com" > /html/$name/index.html ;done
[root@web01 ~]# for name in {www,blog,bbs} ; do cat /html/$name/index.html ;done
10.1.1.7 www.yunxuanedu.com
10.1.1.7 blog.yunxuanedu.com
10.1.1.7 bbs.yunxuanedu.com
5.3.3 检查配置文件语法,重启nginx服务
[root@web01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 ~]# systemctl reload nginx
5.3.4 配置 DNS 解析
5.3.5 访问测试
5.4 企业中虚拟主机访问方式
5.4.1 基于域名的方式访问
5.4.2 基于IP地址的方式进行访问 (监听地址-指定地址)
负载均衡+高可用服务 可使用到,保障安全访问
Nginx 配置中变动 IP 地址之后,需要 restart 服务,reload无效
[root@web01 ~]# vim /etc/nginx/conf.d/www.conf
server {
listen 10.1.1.7:80;
server_name www.yunxuanedu.com;
location / {
root /html/www;
index index.html;
}
}
[root@web01 nginx]# cat nginx.conf
user www;
worker_processes auto;
……
#include /etc/nginx/conf.d/*.conf;
include /etc/nginx/conf.d/www.conf;
}
[root@web01 nginx]# systemctl restart nginx
[root@web01 nginx]# netstat -lntup|grep nginx
tcp 0 0 10.1.1.7:80 0.0.0.0:* LISTEN 25382/nginx: master
5.4.3 基于端口的方式进行访问
[root@web01 conf.d]# vim www.conf
server {
listen 8080;
server_name www.yunxuanedu.com;
location / {
root /html/www;
index index.html;
}
}
[root@web01 conf.d]# netstat -lntup|grep nginx
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 25457/nginx: master
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 25457/nginx: master
6. 网站页面访问原理
-
域名解析
-
建立TCP连接
-
根据应用层 HTTP 协议发出请求
-
如果没有相同域名的 server 虚拟主机,会找满足端口要求的第一个主机,显示其页面
7. 企业中网站的安全访问配置
7.1 根据用户访问的地址进行控制
7.1.1 案例
10.1.1.0/24 www.yunxuanedu.com/test 不能访问
172.16.1.0/24 www.yunxuanedu.com/test 能访问
nginx 访问模块:ngx_http_access_module
- Example (源地址)
location / {
deny 192.168.1.1;
allow 192.168.1.0/24;
allow 10.1.1.0/16;
allow 2001:0db8::/32;
deny all;
}
- 指令语法
Syntax: [deny|allow] address | CIDR | unix: | all;
Default: —
Context: http, server, location, limit_except
7.1.2 实现需求
- 创建站点资源
[root@web01 ~]# cd /html/www/
[root@web01 www]# mkdir test
[root@web01 www]# echo 'NB Plus!' > test/t01.html
[root@web01 www]# cat test/t01.html
NB Plus!
- 配置 Nginx 配置文件
[root@web01 www]# vim /etc/nginx/conf.d/www.conf
server {
listen 80;
server_name www.yunxuanedu.com;
location / {
root /html/www;
index index.html;
}
location /test {
root /html/www;
deny 10.1.1.0/24;
allow 172.16.1.0/24;
deny all;
}
}
[root@web01 www]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 www]# systemctl restart nginx
- 访问测试
# 01. 访问 172.16.1.0/24
[root@web01 www]# grep www /etc/hosts
172.16.1.7 web01 www.yunxuanedu.com
[root@web01 www]# ping www.yunxuanedu.com
PING web01 (172.16.1.7) 56(84) bytes of data.
64 bytes from web01 (172.16.1.7): icmp_seq=1 ttl=64 time=0.015 ms
^C
--- web01 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.015/0.015/0.015/0.000 ms
[root@web01 www]# curl www.yunxuanedu.com/test/t01.html
NB Plus!
# 02. 访问 10.1.1.0/24
[root@web01 www]# grep www /etc/hosts
10.1.1.7 web01 www.yunxuanedu.com
[root@web01 www]# ping www.yunxuanedu.com
PING web01 (10.1.1.7) 56(84) bytes of data.
64 bytes from web01 (10.1.1.7): icmp_seq=1 ttl=64 time=0.043 ms
64 bytes from web01 (10.1.1.7): icmp_seq=2 ttl=64 time=0.042 ms
^C
--- web01 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1008ms
rtt min/avg/max/mdev = 0.042/0.042/0.043/0.006 ms
[root@web01 www]# curl www.yunxuanedu.com/test/t01.html
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.20.2</center>
</body>
</html>
7.2 根据用户访问进行认证
Nginx 认证模块:ngx_http_auth_basic_module
- example
location / {
auth_basic "closed site"; -- 开启认证功能
auth_basic_user_file conf/htpasswd; -- 加载用户密码文件
}
- 指令语法
Syntax: auth_basic string | off;
Default: auth_basic off;
Context: http, server, location, limit_except
7.2.1 案例使用
- 编写配置文件
[root@web01 conf.d]# vim www.conf
server {
listen 80;
server_name www.yunxuanedu.com;
location / {
root /html/www;
index index.html;
auth_basic "yunxuanedu";
auth_basic_user_file password/htpasswd;
}
location /test {
root /html/www;
allow 10.1.1.1/32;
deny all;
}
}
- 创建密码文件(文件中密码信息必须是密文的)
htpasswd -- 创建一个密文信息的密码文件
# 安装
yum install httpd-tools -y
- htpasswd 命令参数说明
参数 | 功能 |
---|---|
-c | 创建一个密码文件 |
-n | 不更新文件,显示文件内容信息(不重要) |
-b | 免交互方式输入用户密码信息 |
-i | 采用标准输入方式读取密码信息 |
-m | MD5算法加密 |
-B | 使用bcrypt对密码进行加密 |
-C | 使用bcrypt algorithm对密码进行加密 |
-d | 指定密码加密方式 |
-s | 指定密码加密方式 |
-p | 不加密密码信息 |
-D | 删除指定用户 |
- 创建用户密码文件 - 方式一
[root@web01 ~]# cd /etc/nginx/
[root@web01 nginx]# mkdir password
[root@web01 nginx]# cd password/
[root@web01 password]# pwd
/etc/nginx/password
[root@web01 password]# htpasswd -c htpasswd yunxuan
New password:
Re-type new password:
Adding password for user yunxuan
[root@web01 password]# ll
total 4
-rw-r--r-- 1 root root 46 Dec 23 11:36 htpasswd
[root@web01 password]# cat htpasswd
yunxuan:$apr1$6JUB5x0w$gXYbGke7hWpDqz.TnohJ4/
- 创建用户密码文件 - 方式二(免交互)
[root@web01 password]# htpasswd -bc htpasswd yunxuan 123456
Adding password for user yunxuan
[root@web01 password]# ll
total 4
-rw-r--r-- 1 root root 46 Dec 23 11:39 htpasswd
[root@web01 password]# cat htpasswd
yunxuan:$apr1$AkENEDXS$ZbKLhP4ItGekVlIEPiopQ/
- 删除密码文件中用户认证信息(比如某人离职,删除信息)
[root@web01 password]# cat htpasswd
yunxuan:$apr1$AkENEDXS$ZbKLhP4ItGekVlIEPiopQ/
[root@web01 password]# htpasswd -D htpasswd yunxuan
Deleting password for user yunxuan
[root@web01 password]# cat htpasswd
- 重启 Nginx 服务,访问测试
[root@web01 ~]# systemctl reload nginx.service

- 密码文件权限优化
# 01. 当前密码文件权限
[root@web01 password]# ll
total 4
-rw-r--r-- 1 root root 46 Dec 23 11:49 htpasswd
# 02. 为了安全起见,权限修改为600
[root@web01 password]# chmod 600 htpasswd
[root@web01 password]# ll
total 4
-rw------- 1 root root 46 Dec 23 11:49 htpasswd
[root@web01 password]#
# 03. 重启服务验证
[root@web01 password]# systemctl reload nginx

- 问题分析
# 01. 普通用户 通过 浏览器访问 web 站点,用户身份全部会转为 Nginx worker 进程的管理用户,而目前 worker进程管理用户为 www。
# 02. 站点认证,输入密码之后,普通用户转换为www用户,读取密码文件,进行对比。若密码相同,则会允许访问。否则无法访问。
# 03. 由于密码文件权限修改,www用户无法读物密码文件。因此报 500错误
- 修改 密码文件属主属组信息,并进行访问验证
[root@web01 password]# ps -ef|grep nginx
root 26999 1 0 11:53 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
www 27065 26999 0 12:11 ? 00:00:00 nginx: worker process
root 27090 26738 0 12:21 pts/1 00:00:00 grep --color=auto nginx
[root@web01 password]# chown www.www htpasswd
[root@web01 password]# ll
total 4
-rw------- 1 www www 46 Dec 23 11:49 htpasswd

- 有认证功能时,curl 命令怎么访问站点
[root@web01 ~]# curl www.yunxuanedu.com
<html>
<head><title>401 Authorization Required</title></head>
<body>
<center><h1>401 Authorization Required</h1></center>
<hr><center>nginx/1.20.2</center>
</body>
</html>
- curl 常用参数
参数 | 功能 |
---|---|
--user USER[:PASSWORD] Server user and password | 指定用户和密码 |
-s, --silent Silent mode. Don't output anything | 无声模式。不输出任何东西 |
[root@web01 ~]# curl www.yunxuanedu.com -u yunxuan:123456
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>中国制造</title>
<style type="text/css">
body{
background: #F0F8FF;
}
h1{
font-size: 32px;
color: red;
text-align: center;
}
p{
font-size: 25px;
color: black;
text-align: center;
}
</style>
</head>
<body>
<h1>中国制造</h1>
<p>人生自古谁无死</p>
<p>留取丹心照汗青</p>
</body>
</html>
- 扩展 - 500 状态码
1. 内部程序代码编写有问题
2. 程序服务中文件权限不正确
8. Nginx 企业实践应用
8.1 利用 Nginx 服务搭建网站文件共享服务器
8.1.1 编写配置文件 并 重启服务
Nginx 模块:ngx_http_autoindex_module
# 01. example
location / {
autoindex on;
}
# 02. 语法
Syntax: autoindex on | off;
Default: autoindex off;
Context: http, server, location
# 01. 编写 Nginx 配置文件
[root@web01 conf.d]# vim www.conf
server {
listen 80;
server_name www.yunxuanedu.com;
location / {
root /html/www;
index index.html; # -- 不能保留首页文件,否则会默认访问首页文件
autoindex on; # -- 开启Nginx站点目录索引功能
}
}
# 02. 重启服务
[root@web01 conf.d]# systemctl restart nginx
8.1.2 处理首页文件
[root@web01 conf.d]# cd /html/www/
[root@web01 www]# ll
total 8
-rw-r--r-- 1 www www 407 Dec 20 16:16 index.html
-rw-r--r-- 1 root root 28 Dec 20 17:41 index.html.bak01
drwxr-xr-x 2 root root 22 Dec 21 12:33 test
[root@web01 www]# mv index.html{,.bak}
[root@web01 www]# ll
total 8
-rw-r--r-- 1 www www 407 Dec 20 16:16 index.html.bak
-rw-r--r-- 1 root root 28 Dec 20 17:41 index.html.bak01
drwxr-xr-x 2 root root 22 Dec 21 12:33 test
8.1.3 访问环境创建 并进行访问测试
[root@web01 www]# pwd
/html/www
[root@web01 www]# mkdir CentOS{6,7,8}
[root@web01 www]# ll
total 8
drwxr-xr-x 2 root root 6 Dec 24 11:37 CentOS6
drwxr-xr-x 2 root root 6 Dec 24 11:37 CentOS7
drwxr-xr-x 2 root root 6 Dec 24 11:37 CentOS8
-rw-r--r-- 1 www www 407 Dec 20 16:16 index.html.bak
-rw-r--r-- 1 root root 28 Dec 20 17:41 index.html.bak01
drwxr-xr-x 2 root root 22 Dec 21 12:33 test
[root@web01 www]# touch CentOS{6,7,8}/system.txt
[root@web01 www]# echo "123456" > CentOS7/system.txt
[root@web01 www]# cat CentOS7/system.txt
123456

8.1.4 访问 txt 文件

发现直接打开文件,无法下载。原因如下
# 因为在该文件中将 txt 文件定义为文本文件,若文件中不存在 txt 文件的定义,会直接下载,不会显示文件内容
[root@web01 www]# grep txt /etc/nginx/mime.types
text/plain txt;
# 01. 过滤发现 /etc/nginx/mime.types 没有 php 文件的定义
[root@web01 www]# grep php /etc/nginx/mime.types
# 02. 站点目录下创建 php 文件
[root@web01 www]# echo "123456" > test.php
[root@web01 www]# cat test.php
123456
[root@web01 www]# ll
total 12
drwxr-xr-x 2 root root 24 Dec 24 11:38 CentOS6
drwxr-xr-x 2 root root 24 Dec 24 11:38 CentOS7
drwxr-xr-x 2 root root 24 Dec 24 11:38 CentOS8
-rw-r--r-- 1 www www 407 Dec 20 16:16 index.html.bak
-rw-r--r-- 1 root root 28 Dec 20 17:41 index.html.bak01
drwxr-xr-x 2 root root 22 Dec 21 12:33 test
-rw-r--r-- 1 root root 7 Dec 24 12:10 test.php
# 03. 访问测试,如下图

04. 在 /etc/nginx/mime.types 文件中,定义php文件为文本文件
[root@web01 www]# vim /etc/nginx/mime.types
[root@web01 www]# grep txt /etc/nginx/mime.types
text/plain txt php;
[root@web01 www]# systemctl restart nginx
# 05. 访问测试

8.1.5 总结
- 需要将 首页文件 进行删除
- mime.types 媒体资源类型文件作用
- 文件中有的扩展名信息资源,进行访问时会直接看到数据信息
- 文件中没有的扩展名信息资源,进行访问时会直接下载资源
8.2 网站页面 中文显示乱码问题
Nginx 模块:ngx_http_charset_module
- Example
include conf/koi-win;
charset windows-1251;
source_charset koi8-r;
- Syntax
Syntax: charset charset | off;
Default: charset off;
Context: http, server, location, if in location
8.2.1 中文页面访问测试
[root@web01 www]# echo '你好,中国!' > 测试.txt
[root@web01 www]# ll
total 16
drwxr-xr-x 2 root root 24 Dec 24 11:38 CentOS6
drwxr-xr-x 2 root root 24 Dec 24 11:38 CentOS7
drwxr-xr-x 2 root root 24 Dec 24 11:38 CentOS8
-rw-r--r-- 1 www www 407 Dec 20 16:16 index.html.bak
-rw-r--r-- 1 root root 28 Dec 20 17:41 index.html.bak01
drwxr-xr-x 2 root root 22 Dec 21 12:33 test
-rw-r--r-- 1 root root 7 Dec 24 12:10 test.php
-rw-r--r-- 1 root root 17 Dec 24 12:27 测试.txt
[root@web01 www]# cat 测试.txt
你好,中国!

8.2.2 编写 Nginx 配置文件
[root@web01 www]# vim /etc/nginx/conf.d/www.conf
[root@web01 www]# cat /etc/nginx/conf.d/www.conf
server {
listen 80;
server_name www.yunxuanedu.com;
location / {
root /html/www;
index index.html;
charset utf-8;
autoindex on;
}
}
[root@web01 www]# systemctl restart nginx
8.2.3 访问测试

8.3 利用 Nginx 服务 搭建 配置文件别名功能
8.3.1 编写配置文件
[root@web01 www]# vim /etc/nginx/conf.d/www.conf
[root@web01 www]# cat /etc/nginx/conf.d/www.conf
server {
listen 80;
server_name www.yunxuanedu.com yx.com;
location / {
root /html/www;
index index.html;
charset utf-8;
autoindex on;
}
}
[root@web01 www]# systemctl restart nginx
8.3.2 配置解析信息
8.3.3 访问测试

8.3.4 配置别名功能 作用
- 便于网站访问测试
- 定位要访问的网站服务器
8.4 利用 Nginx 状态模块功能对网站进行监控
状态模块:ngx_http_stub_status_module
- Example
location = /basic_status {
stub_status;
}
- Syntax
Syntax: stub_status;
Default: —
Context: server, location
8.4.1 编写配置文件
[root@web01 conf.d]# vim state.conf
[root@web01 conf.d]# cat state.conf
server {
listen 80;
server_name status.yunxuanedu.com;
stub_status;
}
[root@web01 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 conf.d]# systemctl restart nginx
8.4.2 配置映射关系
8.4.3 访问测试

8.4.4 结果说明
Active connections: 1
server accepts handled requests
1 1 1
Reading: 0 Writing: 1 Waiting: 0
- 解释说明
# 01. 激活的连接数信息
Active connections: 1
# 02. 接收连接数的总和(重启服务会 清零)
accepts
# 03. 处理的连接数总和
handled
# 04. 总计的请求数量(http协议的请求)
requests
# 05. Nginx 服务读取请求报文的数量
Reading
# 06. Nginx 服务响应报文的数量
Writing
# 07. Nginx 服务队列机制,要处理(读取或者响应保存的数量)
Waiting
8.5 Nginx 日志功能配置
日志文件 一定要做切割处理
8.5.1 访问日志
/var/log/nginx/access.log
Nginx 模块:ngx_http_log_module
- Example
# 定义日志格式
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;
- Syntax
Syntax: log_format name [escape=default|json|none] string ...;
Default: log_format combined "...";
Context: http
Syntax: access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]];
access_log off;
Default: access_log logs/access.log combined;
Context: http, server, location, if in location, limit_except
- 访问日志格式详解
$remote_addr -- 显示用户访问源IP地址信息
$remote_user -- 显示认证用户名信息(没有认证 '-' 代替)
[$time_local] -- 显示访问网站时间信息
"$request" -- 请求报文的请求行信息
$status -- 用户访问网站状态码信息
$body_bytes_sent -- 显示响应的数据尺寸信息
"$http_referer" -- 记录调用网站资源的链接地址信息(防止用户盗链)
"$http_user_agent" -- 记录用户使用什么客户端软件进行访问网站
"$http_x_forwarded_for" -- 跟负载均衡有关
8.5.2 错误日志
/var/log/nginx/error.log
Nginx 模块:Core functionality
- Syntax
Syntax: error_log file [level]; -- 指定错误日志路径和错误级别
Default: error_log logs/error.log error;
Context: main, http, mail, stream, server, location
# 错误级别
debug -- 调试级别,服务运行的状态信息和错误信息详细显示,信息越多
info -- 信息级别,只显示重要的运行状态信息和错误信息
notice -- 通知级别,更加重要的信息进行通知说明
warn -- 警告级别,可能出现了一些错误信息,但不影响服务运行
error -- 错误级别,服务运行已经出现了错误,需要进行纠正, 推荐选择级别
crit -- 严重级别,必须进行修改调整
alert -- 严重警告级别,既警告,而且必须进行错误修改
emerg -- 灾难级别,服务不能正常运行,信息越少
8.6 网站服务匹配功能
Nginx 模块:ngx_http_core_module
location 进行匹配(uri)
# 错误页面优雅显示
location /error {
root /html/www;
error_page 404 /error.jpg;
}
- Syntax
Syntax: location [ = | ~ | ~* | ^~ ] uri { ... }
location @name { ... }
Default: —
Context: server, location
- Example
# 01. 精确匹配 优先级01-最高
location = / {
[ configuration A ]
}
# 02. 默认匹配 优先级04-最低
location / {
[ configuration B ]
}
# 03. 按照目录进行匹配 优先级03
location /documents/ {
[ configuration C ]
}
# 04. 优先匹配(不识别uri信息中符号信息) 优先级02
location ^~ /images/ {
[ configuration D ]
}
# 05. 不区分大小写匹配 优先级03 (~ 区分大小写)
location ~* \.(gif|jpg|jpeg)$ {
[ configuration E ]
}
# 优先级
location = xxx 精确匹配 优先级01
location ^~ xxx 优先匹配 优先级02
location ~ xxx 区分大小写匹配 优先级03
location ~* xxx 不区分大小写匹配 优先级03
location uri 根据uri匹配 优先级03
location / 默认匹配 优先级最低
# 扩展 - 京东网站 location 应用 (节假日 11.11 12.12 6.18等)
有两个站点
站点1:平常使用 /html/jd_normal
站点2:节假日使用 /html/jd_special
location / {
root /html/jd_normal;
}
location 特殊 {
root /html/jd_special;
}
8.7 利用 Nginx 实现网站跳转功能
Nginx 模块:ngx_http_rewrite_module
- Syntax
Syntax: rewrite regex replacement [flag]; # -- rewrite 匹配正则信息 替换成什么信息
Default: —
Context: server, location, if
- Example
rewrite ^/(.*) http://www.yunxuan.com/$1 permanent;
# 跳转方式
# 01. 永久跳转 permanent 301 会将跳转信息进行缓存
# 02. 临时跳转 redirect 302 不会缓存跳转信息
出现无限循环跳转如何解决
01. 利用不同server区块配置打破循环
server {
listen 80;
server_name yunxaun.com;
rewrite ^/(.*) http://www.yunxuanedu.com/$1 permanent;
}
server {
listen 80;
server_name www.yunxuanedu.com;
location / {
root /html/www;
index index.html;
}
}
02. 利用if判断实现打破循环
if ($host ~* "^yunxuan.com$") {
rewrite ^/(.*) http://www.yunxuanedu.com/$1 permanent;
}
$host url信息
~* 不区分大小写
8.7.1 return 跳转
return 301 http://xxxxx
网友评论