nginx网站服务(下)
第1章 回顾1.1 nginx软件服务
1.1.1 软件概念以及特性介绍
1. 可以实现高并发访问处理,消耗资源小
2. 软件知识功能很多(web服务功能反向代理功能缓存功能)
3. 利用异步网络IO模型,实现快速处理用户请求(epoll)
1.1.2 软件部署过程
1)下载解压软件(nginx.org)
2)安装依赖软件
openssl-develpcre-devel
3)创建出一个worker进程管理用户
4)进行nginx软件编译安装
a 进行软件配置 ./configure
b 进行软件编译 make
c 进行软件编译安装 make install
5)创建程序目录软链接
6)启动nginx服务,利用curl命令或浏览器进行访问测试
1.1.3 介绍软件目录结构信息
conf --- 软件配置文件保存目录
html --- 软件服务站点目录
logs --- 软件服务日志默认保存目录
sbin --- 软件服务管理命令服务(nginx -t -s>1.1.4 介绍服务配置文件参数信息(默认)
规范一:大括号要成对出现
规范二:每行信息结尾要注意有分号 ;
规范三:相应参数指令只能放置在指定区块
1.1.5 搭建简单的web网站
第2章 虚拟主机的概念和类型介绍
所谓虚拟主机,在Web服务里就是一个独立的网站站点,这个站点对应独立的域名(也可能是IP或端口), 虚拟主机配置上有三种类型
2.1 基于域名的虚拟主机配置
2.1.1 修改配置文件
# vim nginx.conf
worker_processes1;
events {
worker_connections1024;
}
http {
include mime.types;
default_typeapplication/octet-stream;
sendfile on;
keepalive_timeout65;
server {
listen 80;
server_namewww.wuhuang.com;
root html/www;
indexindex.html index.htm;
}
server {
listen 80;
server_namebbs.wuhuang.com;
root html/bbs;
indexindex.html index.htm;
}
server {
listen 80;
server_nameblog.wuhuang.com;
root html/blog;
indexindex.html index.htm;
}
}
2.1.2 网站目录和文件环境准备
mkdir /application/nginx/html/{www,bbs,blog} -p
for name in www bbs blog;do echo "10.0.0.7 web01 $name" >/application/nginx/html/$name/index.html;done
# mkdir /application/nginx/html/{www,bbs,blog} -p
# for name in www bbs blog;do echo "10.0.0.7 web01 $name" >/application/nginx/html/$name/index.html;done
# ll ../html/
total 20
-rw-r--r-- 1 root root537 Feb5 19:53 50x.html
drwxr-xr-x 2 root root 4096 Feb5 20:03 bbs
drwxr-xr-x 2 root root 4096 Feb5 20:03 blog
-rw-r--r-- 1 root root612 Feb5 19:53 index.html
drwxr-xr-x 2 root root 4096 Feb5 20:03 www
# for name in www bbs blog;do cat /application/nginx/html/$name/index.html;done
10.0.0.7 web01 www
10.0.0.7 web01 bbs
10.0.0.7 web01 blog
2.1.3 检查配置文件语法,进行服务重启或者启动
# /application/nginx/sbin/nginx -t
nginx: the configuration file /application/nginx-1.12.2/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.12.2/conf/nginx.conf test is successful
# /application/nginx/sbin/nginx -s> # netstat -lntup |grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 8337/nginx
2.1.4 利用curl或者浏览器进行访问测试
需要对虚拟主机域名进行解析(编写hosts文件-linux里面hosts文件 windows里面hosts)
# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
172.16.1.5 lb01
172.16.1.6 lb02
172.16.1.7 web01 www.wuhuang.com bbs.wuhuang.com blog.wuhuang.com
# for name in www bbs blog;do curl $name.wuhuang.com;sleep 1;done
10.0.0.7 web01 www
10.0.0.7 web01 bbs
10.0.0.7 web01 blog
当客户端访问nginx服务端,返回的状态码信息为304时,表示进行读取缓存处理
2.2 基于端口的虚拟主机配置
2.2.1 修改配置文件
# vim nginx.conf
worker_processes1;
events {
worker_connections1024;
}
http {
include mime.types;
default_typeapplication/octet-stream;
sendfile on;
keepalive_timeout65;
server {
listen 80;
server_namewww.wuhuang.com;
root html/www;
indexindex.html index.htm;
}
server {
listen 81; --- 将默认80端口号改为81
server_namebbs.wuhuang.com;
root html/bbs;
indexindex.html index.htm;
}
server {
listen 80;
server_nameblog.wuhuang.com;
root html/blog;
indexindex.html index.htm;
}
}
2.2.2 优化nginx操作
# vim /etc/profile
# tail -1 /etc/profile
export PATH=$PATH:/application/nginx/sbin/
2.2.3 重启nginx服务
# nginx -t
nginx: the configuration file /application/nginx-1.12.2/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.12.2/conf/nginx.conf test is successful
# nginx -s>2.2.4 验证
2.2.5 网站访问过程原理图
2.3 基于IP的虚拟主机配置
2.3.1 修改配置文件
# vim nginx.conf
worker_processes1;
events {
worker_connections1024;
}
http {
include mime.types;
default_typeapplication/octet-stream;
sendfile on;
keepalive_timeout65;
server {
listen 10.0.0.7:80;
server_namewww.wuhuang.comg;
root html/www;
indexindex.html index.htm;
}
#server {
# listen 81;
# server_namebbs.wuhuang.com;
# root html/bbs;
# indexindex.html index.htm;
#}
#server {
# listen 80;
# server_nameblog.wuhuang.com;
# root html/blog;
# indexindex.html index.htm;
#}
}
强调说明(*****):当nginx配置文件中,涉及到IP地址信息改动,都需要重启nginx服务,
不能采用平滑重启,否则配置不生效
2.3.2 重启服务
# nginx -s stop
# nginx
#netstat -lntup|grep nginx
tcp 0 0 10.0.0.7:80 0.0.0.0:* LISTEN 8440/nginx
2.3.3 验证
# curl 10.0.0.7
10.0.0.7 web01 www
# curl 172.16.1.7
curl: (7) couldn't connect to host
# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
172.16.1.7 web01 bbs.wuhuang.com blog.wuhuang.com
10.0.0.7 www.wuhuang.com
注意:同一个IP地址可以对应多个域名,但一个域名只能对应一个IP地址
# curl www.wuhuang.com
10.0.0.7 web01 www
2.4 企业规范优化Nginx配置文件
2.4.1 第一个里程碑:创建扩展目录,生成虚拟主机配置文件
# vim nginx.conf
13 root html/www;
14 indexindex.html index.htm;
15 }
16 server {
17 listen 80;
18 server_namebbs.wuhuang.com;
19 root html/bbs;
20 indexindex.html index.htm;
21 }
22 server {
23 listen 80;
24 server_nameblog.wuhuang.com;
25 root html/blog;
26 indexindex.html index.htm;
27 }
28 }
# mkdir extra
# sed -n '10,15p' nginx.conf >extra/www.conf
# sed -n '16,21p' nginx.conf >extra/bbs.conf
# sed -n '22,27p' nginx.conf >extra/blog.conf
# cat extra/blog.conf
server {
listen 80;
server_nameblog.wuhuang.com;
root html/blog;
indexindex.html index.htm;
}
2.4.2 第二个里程碑:修改nginx主配置文件,加载相应虚拟主机配置文件
# cat nginx.conf
worker_processes1;
events {
worker_connections1024;
}
http {
include mime.types;
default_typeapplication/octet-stream;
sendfile on;
keepalive_timeout65;
include extra/www.conf;
include extra/bbs.conf;
include extra/blog.conf;
}
2.4.3 第三里程碑:重启服务,进行检验测试
# nginx -t
nginx: the configuration file /application/nginx-1.12.2/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.12.2/conf/nginx.conf test is successful
# nginx -s> # for name in www bbs blog;do curl $name.wuhuang.com;sleep 1;done
10.0.0.7 web01 www
10.0.0.7 web01 bbs
10.0.0.7 web01 blog
2.5 Nginx虚拟主机的别名配置
# cat bbs.conf
server {
listen 80;
server_namebbs.wuhuang.com bbs.com;
root html/bbs;
indexindex.html index.htm;
}
说明:添加别名信息需要设置到hosts解析文件中
# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
172.16.1.5 lb01
172.16.1.6 lb02
172.16.1.7 web01www.wuhuang.combbs.wuhuang.comblog.wuhuang.com bbs.com
验证
# nginx -t
nginx: the configuration file /application/nginx-1.12.2/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.12.2/conf/nginx.conf test is successful
# nginx -s> # curl bbs.com
10.0.0.7 web01 bbs
2.6 Nginx状态信息功能实战
为什么在编译安装时,需要配置状态模块?
可以查看nginx运行状态信息。
官方参考链接:http://nginx.org/en/docs/http/ngx_http_stub_status_module.html#stub_status
# cat extra/state.conf
server{
listen80;
server_namestate.wuhuang.com;
location / {
stub_status on;
access_log off;
}
}
# cat nginx.conf
worker_processes1;
events {
worker_connections1024;
}
http {
include mime.types;
default_typeapplication/octet-stream;
sendfile on;
keepalive_timeout65;
include extra/www.conf;
include extra/bbs.conf;
include extra/blog.conf;
include extra/state.conf;
}
说明:以上信息配置,将状态模块域名配置到windows系统的hosts文件中和/etc/hosts中。
# nginx -t
nginx: the configuration file /application/nginx-1.12.2/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.12.2/conf/nginx.conf test is successful
# nginx -s> # curl state.wuhuang.com
Active connections: 1
server accepts handled requests
9 9 9
Reading: 0 Writing: 1 Waiting: 0
状态模块说明:
Active connections: 1 ——当前客户端的连接数量(包含waiting连接数)
server accepts handled requests ——accepts:接收客户端连接的总数(只接受http协议信息)
9 9 9 —— handled:处理连接的总数(处理的是accepts接收的数据)
—— requests:客户端请求的总数(包括TCP建立接连)
Reading: 0 Writing: 1 Waiting: 0 —— Reading:监控请求头的连接数
—— writing:监控回应客户端的连接数
——waiting:监控空闲客户端的连接请求等待数
第3章 Nginx服务日志信息
3.1 错误日志
3.1.1 error_log的语法格式及参数语法说明:
error_log file level
关键字 日志文件 错误日志级别
日志信息错误级别分为8种:
0EMERG(紧急):会导致主机系统不可用的情况
1ALERT(警告):必须马上采取措施解决的问题
2CRIT(严重):比较严重的情况
3ERR(错误):运行出现错误
4WARNING(提醒):可能会影响系统功能的事件
5NOTICE(注意):不会影响系统但值得注意
6INFO(信息):一般信息
7DEBUG(调试):程序或系统调试信息等
error_log的默认值为:
Default:error_log logs/error.log error;
可以放置的标签段为:
Context:main, http, mail, stream, server, location
3.1.2 Nginx错误日志配置
# cat /application/nginx/conf/nginx.conf
worker_processes1;
error_loglogs/error.log;
events {
worker_connections1024;
}
http {
include mime.types;
default_typeapplication/octet-stream;
sendfile on;
keepalive_timeout65;
include extra/www.conf;
include extra/bbs.conf;
include extra/blog.conf;
include extra/state.conf;
}
3.2 访问日志(重点关注)
# cat /application/nginx/conf/nginx.conf
worker_processes1;
error_loglogs/error.log;
events {
worker_connections1024;
}
http {
include mime.types;
default_typeapplication/octet-stream;
log_formatmain'$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
############### 定义日志信息要记录的内容格式
access_loglogs/access.logmain;
sendfile on;
keepalive_timeout65;
include extra/www.conf;
include extra/bbs.conf;
include extra/blog.conf;
include extra/state.conf;
}
# cat bbs.conf
server {
listen 80;
server_namebbs.wuhuang.com;
location / {
access_log on;
}
root html/bbs;
indexindex.html index.htm;
}
Nginx记录日志的默认参数配置:
access_loglogs/access.logmain; --- 调用定义格式信息,生成访问日志
$remote_addr 10.0.0.1 --- 访问客户端的源地址信息
$remote_user - --- 访问客户端认证用户信息
[$time_local] --- 显示访问时间
$request GET / HTTP/1.1 --- 请求行信息
$status 304 --- 状态码信息(304状态码利用缓存显示页面信息)
$body_bytes_sent --- 服务端响应客户端的数据大小信息
$http_referer --- 记录链接到网站的域名信息
$http_user_agent --- 用户访问网站客户端软件标识信息(例:浏览器、手机客户端)
用户利用客户端浏览器测试访问时,win10默认浏览器会有异常问
$http_x_forwarded_for --- 反向代理
官方链接:http://nginx.org/en/docs/http/ngx_http_log_module.html#access_log
3.3 日志要进行切割
3.3.1 利用shell脚本实现日志切割
# cat cut_log.sh
#!/bin/bash
data_info=$(date +%F-%H:%M)
mv /application/nginx/logs/access.log /application/nginx/logs/access.log.$data_info
/application/nginx/sbin/nginx -s> # cut nginx log cron
* */6 * * * /bin/sh /server/scripts/cut_log.sh &>/dev/null
写入定时任务
# cut nginx log cron
* */6 * * * /bin/sh /server/scripts/cut_log.sh &>/dev/null
3.3.2 利用logrotate服务程序进行日志切割
作用:logrotate的配置文件是/etc/logrotate.conf,通常不需要对它进行修改。日志文件的轮循设置在独立的 配置文件中,它们放在/etc/logrotate.d/目录下。
logrotate全局配置参数说明:
vim /etc/logrotate.conf
weekly ###日志文件将按月轮循
rotate 4 ###一次将最多存储4个归档日志
create ###在生成轮询日志后,会自动创建一个新的文件
dateext ###生成的轮询日志后面加上时间格式
compress ###生成的轮询日志是否压缩,默认被注释
include /etc/logrotate.d ###存放日志文件的轮询配置
vim /etc/logrotate.d/log-file
/var/log/log-file {
monthly
-rotate 5
compress
delaycompress
missingok
create 644 root root
postrotate
/usr/bin/killall -HUP rsyslogd
endscript
}
第4章 location区块作用
4.1 企业需求解决:
搭建好一台nginx的web服务器。配置好内网卡地址与外网卡地址
web服务的网站域名为www.etiantian.org,站点目录为html/www
要求内网用户可以访问网站http://www.etiantian.org/AV资源信息
要求外网用户禁止访问网站http://www.etiantian.org/AV资源信息
4.2 部署过程
4.2.1 精确控制允许和拒绝
location / {
allow 172.16.1.0/24; ——允许172.16.1.0网段访问
denyall; ——拒绝所有访问
}
4.2.2 访问/AV 资源有访问控制
定位/AV资源不能随意访问
location 进行资源定位——相当于if 判断,满足什么做什么
格式:
location /AV {
}
4.2.3 创建测试访问资源信息
创建测试站点目录
mkdir /application/nginx/html/www/AV
curl-H host:www.wuhuang.com 10.0.0.7/lalala.html --- 表示指定访问nginx服务端哪一个虚拟主机
4.2.4 测试
# curl -H host:www..wuhuang.com 172.16.1.7/AV/lalala.html
wuhuang01 ——内网可以访问
# curl -H host:www.etiantian.org 10.0.0.7/AV//lalala.html
403 Forbidden ——403拒绝访问
403 Forbidden
nginx/1.12.2
4.3 location语法格式
Syntax: location [ = | ~ | ~*|^~ ] uri {... } ——uri就是域名后面的信息
4.3.1 比较
-eq 等于
-ne 不等于
-le 小于等于
-lt 小于
-ge 大于等于
-gt 大于
4.3.2 优先顺序
= ——精确匹配(不要多余的) 1
~ ——区分大小写匹配 3
~* ——不区分大小写匹配(grep -i) 3
^~ ——优先匹配(优先级) 2
/AV ——指定要匹配的目录资源 3
/ ——指定匹配站点目录 默认匹配
! ——表示取反匹配
4.3.3 测试匹配优先级方法
第一步:编写测试配置文件
# cat www.conf
server {
listen 80;
server_namewww.wuhuang.com;
root html/www;
location / {
return 401;
}
location = / {
return 402;
}
location /documents/ {
return 403;
}
location ^~ /images/ {
return 404;
}
location ~* \.(gif|jpg|jpeg)$ {
return 500;
}
access_log logs/access_www.log main;
}
第二步:根据返回状态码,确认优先级
总结取状态码方法:
1)curl -I www.wuhuang.com/wu1/ 2>/dev/null|awk 'NR==1{print $2}'
2)curl -I www.wuhuang.com/wu1/ -s|awk 'NR==1{print $2}'
3)curl -s -I www.wuhuang.com/wu1/ -w "%{http_code}\n" -o /dev/null
作用:编译以后进行网站运行状态监控
总结curl命令参数:
-v --- 显示用户访问网站详细报文信息(请求报文 响应报文)
-I --- 显示响应报文起始行和响应头部信息
-H host: --- 修改请求报文host字段信息
-L --- 进行访问跳转追踪
-u user:password --- 指定认证用户信息
-o --- 将输出内容可以指定到空
-w --- 指定需要输出显示的信息
-s --- 不显示错误输出,将错误信息追加到空
4.4 rewrite模块的使用——地址重写
4.4.1 作用功能
1. 将地址信息进行重写,实现域名地址信息跳转
2. 用于做伪静态
rewrite应用标签:server、location、if
如何实现类似百度重写域名的功能?
baidu.com===>www.baidu.com
etiantian.org===> bbs.etiantian.org
4.4.2 rewrite指令实践操作一:(错误)
# cat bbs.conf
server {
listen 80;
server_namebbs.wuhuang.com bbs.com;
rewrite ^/(.*) http://bbs.wuhaung.com/$1 permanent;
root html/bbs;
indexindex.html index.htm;
}
# curl -L etiantian.org --- 进行访问跳转追踪
curl: (47) Maximum (50) redirects followed
# curl -Lv etiantian.org --- 显示无限循环过程
说明:以上配置进入了无限循环状态
4.4.3 rewrite指令实践操作二:(正确)
# cat bbs.conf
server {
listen 80;
server_name etiantian.org;
rewrite ^/(.*) http://bbs.wuhuang.com/$1 permanent;
}
server {
listen 80;
server_namebbs.wuhuang.com bbs.com;
root html/bbs;
indexindex.html index.htm;
}
说明:以上状态不会循环,跳转成bbs,后再次重新匹配时,是以bbs的域名查找,这时候就不匹配第一个server_name了,直接进入下一个server区块
4.4.4 rewrite指令实践操作三:(正确)
# cat bbs.conf
server {
listen 80;
server_namebbs.wuhuang.com bbs.com;
if ($host ~* "^wuhuang.com$") { ——if这里相当于location
rewrite ^/(.*) http://bbs.wuhuang.com/$1 permanent;
}
root html/bbs;
indexindex.html index.htm;
}
说明:$host就是主机头信息,~*:不区分大小写,后面的是以wuhuang开头,com结尾。经过rewite就跳转为bbs, 然后在重新访问,这个时候就不满足if语句的条件了,那么就继续读取下一个location
4.4.5 Nginx的rewrite功能在企业里应用场景
Ø 可以调整用户浏览的URL,使其看起来更规范,合乎开发及产品人员的需求。
Ø 为了让搜索引擎收录网站内容,并让用户体验更好,企业会将动态URL地址伪装成静态地址提供服务。
Ø 网站换新域名后,让旧域名的访问跳转到新的域名上,例如:让京东的360buy换成了jd.com。
Ø 根据特殊变量、目录、客户端的信息进行URL跳转等。
第5章 Nginx访问认证
5.1 部署过程
5.1.1 第一个里程碑:编写配置文件,加入认证信息
auth_basic "wuhuang training";
auth_basic_user_file /application/nginx/conf/htpasswd;
# cat bbs.conf
server {
listen 80;
server_namebbs.wuhuang.com bbs.com;
auth_basic "wuhuang training";
auth_basic_user_file /application/nginx/conf/htpasswd;
if ($host ~* "^wuhuang.com$") {
rewrite ^/(.*) http://bbs.wuhuang.com/$1 permanent;
}
root html/bbs;
indexindex.html index.htm;
}
5.1.2 第二个里程碑:编写认证文件
利用htpasswd命令生成密文密码信息
rpm -qf `which htpasswd`
httpd-tools-2.2.15-59.el6.centos.x86_64 ——安装apache(需要用其中的htpasswd命令)
htpasswd -bc /application/nginx/conf/htpasswd wuhuang 123456 ---生成认证文件
chmod 400 /application/nginx/conf/htpasswd
chown www.www /application/nginx/conf/htpasswd
说明: -c #创建一个新的密码文件
-b #采用免交互的方式输入用户的密码信
5.1.3 第三个里程碑:重启服务进行测试
1. 401 Authorization Required --- 要求用户进行认证
2. 500 --- worker进程无法读取用户请求的问题,权限问题
# curl bbs.wuhuang.com -u wuhuang
Enter host password for user 'wuhuang':
10.0.0.7 web01 bbs
# curl bbs.etiantian.org -u wuhuang:123456
10.0.0.7 web01 bbs
5.2 总结curl命令参数
-v --- 显示用户访问网站详细报文信息(请求报文 响应报文)
-I --- 显示响应报文起始行和响应头部信息
-H host: --- 修改请求报文host字段信息
-L --- 进行访问跳转追踪
-u user:password --- 指定认证用户信息
-o --- 将输出内容可以指定到空
-w --- 指定需要输出显示的信息
-s --- 不显示错误输出,将错误信息追加到空
页:
[1]