多虚拟主机配置:
多ip配置
服务器配置多个网卡 每个网卡一个ip 一个ip一个server
基于ip地址访问
[root@web01 conf.d]# cat www.conf
server {
listen 172.16.1.7:80; --- 修改地址信息
server_name www.oldboy.com;
access_log /var/log/nginx/access_oldboy.log oldboy;
location / {
root /html/www;
index index.html index.htm;
}
}
PS:nginx程序涉及到IP地址的修改,都需要重启服务(不是平滑重启)
多端口方式
多的端口 多个server
基于端口访问
server {
listen 8080; --- 修改端口号
server_name www.oldboy.com;
location / {
root /html/www;
index index.html index.htm;
}
}
多域名方式
nginx程序企业应用方法:
1)如何搭建多个网站 www bbs blog
第一个历程:创建多个网站主机文件
[root@web01 conf.d]# cat www.conf bbs.conf blog.conf
server {
listen 80;
server_name www.oldboy.com;
location / {
root /html/www;
index index.html index.htm;
}
}
server {
listen 80;
server_name bbs.oldboy.com;
location / {
root /html/bbs;
index index.html index.htm;
}
}
server {
listen 80;
server_name blog.oldboy.com;
location / {
root /html/blog;
index index.html index.htm;
}
}
第二个历程:创建不同网站站点目录/创建网站页面信息index.html (www.oldboy.com)
[root@web01 conf.d]# for name in {www,bbs,blog};do echo "$name.oldboy.com" >/html/$name/index.html;done
[root@web01 conf.d]# for name in {www,bbs,blog};do cat /html/$name/index.html;done
www.oldboy.com
bbs.oldboy.com
blog.oldboy.com
第三个历程:配置DNS解析信息
10.0.0.7 www.oldboy.com bbs.oldboy.com blog.oldboy.com
第四个历程:重启nginx程序
方法一:使用systemctl命令重启nginx程序
systemctl restart/reload nginx
方法二:利用nginx命令重启nginx程序
nginx
nginx -s reload
nginx -t 检查配置文件语法
如何确认语法没问题
1-每个指令参数编写正确/位置放置正确
2-每个区域信息需要有一对花括号
3-每个指令参数最后有分号结尾
PS:以上两种方式不要混用
补充:网站页面异常排错思路:
01:检查DNS解析信息
02:检查HTTP请求信息 抓包
03:检查HTTP响应信息 抓包
利用nginx显示网站目录索引
第一个历程:将首页文件进行删除
第二个历程:修改配置文件添加指令,建立目录索引
cat /etc/nginx/conf.d/software.conf
server {
listen 80;
server_name software.yangyijing.cn;
access_log /var/log/nginx/software_access.log;
error_log /var/log/nginx/software_error.log warn;
autoindex_exact_size off; #---默认为on,显示出文件的确切大小,单位是bytes。改为off后,显示出文件的大概大小,单位是kB或者MB或者GB
autoindex_localtime on; #---当前页显示的时间为服务器的时间
autoindex on; #--- 当首页文件不存在,会显示站点目录索引结构信息
charset UTF-8; #---字符集
root /data/software;
}
4) 利用nginx实现安全访问控制
1)基于用户访问信息进行控制 /images /av
/images 允许 oldboy.jpg
/av 禁止 oldboy.html
第一个历程:部署站点目录环境
mkdir /html/www/{images,av}
第二个历程:编辑配置文件
[root@web01 conf.d]# cat www.conf
server {
listen 80;
server_name www.oldboy.com;
access_log /var/log/nginx/access_oldboy.log oldboy;
root /html/www; --- 全局配置
location / {
root /html/www; --- 局部配置
index index.html index.htm;
}
location /images {
allow all;
root /html/www;
index oldboy.jpg index.html index.htm;
}
location /av {
allow 10.0.0.1;
deny all;
root /html/www;
}
}
2)基于用户认证信息进行控制
第一个历程:创建密码文件信息
[root@RainGod html]# htpasswd -bc /etc/nginx/software.password yangtao ******
Adding password for user yangtao
[root@RainGod html]# cat /etc/nginx/software.password
yangtao:$apr1$l59SE.I8$vPMByxDXXnPBh9h9JN04f0
第二个历程:编写主机配置文件
[root@RainGod conf.d]# cat software.conf
server {
listen 80;
server_name software.yangyijing.cn;
access_log /var/log/nginx/software_access.log;
error_log /var/log/nginx/software_error.log warn;
auth_basic "Please input password"; #这里是验证时的提示信息
auth_basic_user_file /etc/nginx/software.password;
autoindex_localtime on;
autoindex on;
charset UTF-8;
root /data/software;
}
PS: 500 Internal Server Error
01. 编写代码信息不正确
02. web范围文件权限有问题
用户浏览器 --- nginx(www(oldboy:oldboy123))--- nginx.password
5) 利用nginx实现网站状态监控
第一个历程:编写监控网站主机配置文件
[root@web01 conf.d]# cat stat.conf
server {
listen 80;
server_name stat.oldboy.com;
location / {
stub_status;
}
}
页面监控信息:
Active connections --- 总的激活并发连接数
accepts --- 总的接收连接数信息
handled --- 总的处理连接数信息
requests --- 总的请求数量
Reading: 读取请求报文数量
Writing: 回复响应报文数量
Waiting: 等待队列
取出指定信息进行监控:
curl -H host:stat.oldboy.com 172.16.1.7 2>/dev/null|awk 'NR==3{print $1}'
curl -H host:stat.oldboy.com 172.16.1.7 -s|awk 'NR==3{print $1}'
作业:
- 预习 location rewrite(跳转)LNMP p m
- 总结