nginx虚拟主机、日志配置、相关模块讲解
1、nginx虚拟主机
访问nginx虚拟主机的三种方式
1、基于多ip的方式
2、基于多端口的方式
3、基于多域名的方式
1、基于多ip的方式
1、修改网址配置文件
[root@web01 conf.d]# vim game1.conf
server {
listen 80;
server_name 172.16.1.7;
location / {
root /opt/Super_Marie;
index index.html;
}
}
server {
listen 80;
server_name 192.168.15.7;
location /{
root /opt/chess;
index index.html;
}
}
2、检查配置文件
[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
3、重启nginx服务(在每次对配置文件进行修改以后都要重启nginx服务来使配置文件生效)
[root@web01 conf.d]# systemctl restart nginx
4、网页测试
2、基于多端口的方式
1、修改网址配置文件
[root@web01 conf.d]# vim game2.conf
server {
listen 80;
server_name 172.16.1.7;
location /{
root /opt/Super_Marie;
index index.html;
}
}
server {
listen 81;
server_name 172.16.1.7;
location / {
root /opt/chess;
index index.html;
}
}
2、检查配置文件
[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
3、重启nginx服务(在每次对配置文件进行修改以后都要重启nginx服务来使配置文件生效)
[root@web01 conf.d]# systemctl restart nginx
4、网页测试
3、基于多域名的方式
1、修改网址配置文件
[root@web01 conf.d]# vim game3.conf
server {
listen 80;
server_name www.marie.com;
location / {
root /opt/Super_Marie;
index index.html;
}
}
server {
listen 80;
server_name www.c_chess.com;
location / {
root /opt/chess;
index index.html;
}
}
2、检查配置文件
[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
3、重启nginx服务(在每次对配置文件进行修改以后都要重启nginx服务来使配置文件生效)
[root@web01 conf.d]# systemctl restart nginx
4、域名解析
修改windows电脑里C:\Windows\System32\drivers\etc\hosts文件,进行域名解析
172.16.1.7 www.marie.com www.chess.com
5、网页测试
2、日志配置
1、简易格式
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
2、json格式
log_format json '{"@timestamp":"$time_iso8601",'
'"host":"$server_addr",'
'"service":"nginxTest",'
'"trace":"$upstream_http_ctx_transaction_id",'
'"log":"log",'
'"clientip":"$remote_addr",'
'"remote_user":"$remote_user",'
'"request":"$request",'
'"http_user_agent":"$http_user_agent",'
'"size":$body_bytes_sent,'
'"responsetime":$request_time,'
'"upstreamtime":"$upstream_response_time",'
'"upstreamhost":"$upstream_addr",'
'"http_host":"$host",'
'"url":"$uri",'
'"domain":"$host",'
'"xff":"$http_x_forwarded_for",'
'"referer":"$http_referer",'
'"status":"$status"}';
3、日志的作用
1、知晓网站的访问来源
2、可以通过日志文件进行拍错
4、相关参数介绍
$remote_addr : 远程客户端的IP地址
$remote_user : 远程客户端用户名称,用于记录浏览者进行身份验证时提供的名字,如登录百度的用户名scq2099yt,如果没有登录就是空白
$time_local : 访问的时间与时区,比如18/Jul/2012:17:00:01 +0800,时间信息最后的"+0800"表示服务器所处时区位于UTC之后的8小时
$request : 请求的URI和HTTP协议,这是整个PV日志记录中最有用的信息,记录服务器收到一个什么样的请求
$status : 记录请求返回的http状态码,比如成功是200
$body_bytes_sent : 发送给客户端的文件主体内容的大小,比如899,可以将日志每条记录中的这个值累加起来以粗略估计服务器吞吐量
$http_referer : 记录从哪个页面链接访问过来的
$http_user_agent : 客户端浏览器信息(请求头User-Agent的内容 )
$http_x_forwarded_for : 客户端的真实ip,通常web服务器放在反向代理的后面,这样就不能获取到客户的IP地址了,通过$remote_add拿到的IP地址是反向代理服务器的iP地址。反向代理服务器在转发请求的http头信息中,可以增加 x_forwarded_for信息,用以记录原有客户端的IP地址和原来客户端请求的服务器地址(在反向代理中生效)
3、nginx访问控制模块
- ngx_http_access_module模块
1、功能
允许或者拒绝某些客户端IP地址的访问
2、相关参数
allow : 允许访问
deny : 拒绝访问
3、相关案例
案例1:允许192.168.15.1访问,不允许其他的IP访问
[root@web01 conf.d]# vim game4.conf
server {
listen 80;
server_name 192.168.15.7;
location / {
allow 192.168.15.1;
deny all;
root /opt/Super_Marie;
index index.html;
}
}
案例2:允许192.168.15.0这个网段访问,不允许其他网段访问
[root@web01 conf.d]# vim game4.conf
server {
listen 80;
server_name www.marie.com;
location / {
allow 192.168.15.0/24;
deny all;
root /opt/Super_Marie;
index index.html;
}
}
案例3:只允许通过VPN来访问
[root@web01 conf.d]# cat game4.conf
server {
listen 80;
server_name www.marie.com;
location / {
allow 172.16.1.81;
deny all;
root /opt/Super_Marie;
index index.html;
}
}
补充:如何通过其他服务器实现网页测试
[root@web02 ~]# curl -H'HOST:www.marie.com' -I 172.16.1.7
- ngx_http_auth_basic_module模块
1、功能
通过对访问用户身份的验证(登录名和密码)来限制用户的访问
2、相关参数
auth_basic : 后跟字符串
auth_basic_user_file : 后跟文件(用户密码文件)
3、相关案例
想要实现对用户的身份验证,首先要创建个用户和密码并保存于文件里
1、安装httpd-tools
[root@web01 conf.d]# yum install httpd-tools -y
2、生成用户名密码文件
[root@web01 conf.d]# htpasswd -c /etc/nginx/auth jinx
New password:
Re-type new password:
Adding password for user jinx
3、在配置文件里写入该模块相关参数
[root@web01 conf.d]# cat game4.conf
server {
listen 80;
server_name www.marie.com;
location /{
auth_basic 'My Lover';
auth_basic_user_file /etc/nginx/auth;
root /opt/Super_Marie;
index index.html;
}
}
4、重启nginx
[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
- ngx_http_autoindex_module模块
1、功能
展示目录索引
2、相关参数
autoindex :自动索引 默认为off,打开为on
autoindex_exact_size : 目录列表大小输出,默认为开启
autoindex_format : 设置目录列表的格式 默认为html
autoindex_localtime : 设置目录文件创建的本地时间(东八区:UTC时间+8小时),默认关闭(即UTC时间)
3、示例
案例:将/opt目录下的文件以网页的形式展示
[root@web01 conf.d]# vim test.conf
server {
listen 80;
server_name www.test.com;
location / {
root /opt;
autoindex on ;
autoindex_exact_size on;
autoindex_localtime on;
# autoindex_format json;
}
}
4、Nginx状态监控模块
ngx_http_stub_status_module模块
1、功能
提供对基本状态信息的访问
2、参数
stub_status :添加即实现状态信息访问的监控
3、示例
[root@web01 conf.d]# vim test.conf
server {
listen 80;
server_name www.marie.com;
location / {
stub_status;
}
}
展示相关参数:
Active connections: 1
server accepts handled requests
1 1 1
Reading: 0 Writing: 1 Waiting: 0
相关参数介绍
Active connections :当前活动客户端连接数,包括waitng连接数
accepts : 接收的客户端连接总数
handled :处理的连接总数。除非是accepts达到某些资源限制,一般情况两者是相同的
requests :客户端请求的总数
Reading :nginx正在读取请求头的当前连接数
Writing :nginx将相应写回客户端的当前连接数
Waiting :当前等待请求的空闲客户端连接数
5、Nginx访问连接控制模块
1、limit_conn_zone
控制nginx的连接数(nginx被连接的个数(并发数))
2、limit_req_zone
控制nginx的访问量(即一定时间内请求访问nginx的次数)
3、参数介绍
1、声明连接池(用来实现对nginx访问的控制)
limit_conn_zone模块:limit_conn_zone $binary_remote_addr zone=one:10m;
limit_req_zone模块:limit_req_zone $binary_remote_addr zone=one:10m rate=10r/s;
参数讲解:
$binary_remote_addr:客户端访问地址
zone:=one:10m :设置一个名字为one,大小为10M的缓存空间
rate=1r/s: 限制访问速率,此处设置为每秒接受1个请求
2、调用连接池
limit_conn_zone模块:limit_conn addr 1;
limit_req_zone模块:limit_req zone=one burst=5;
burst=5意指一秒内最大访问次数不能超过这个限制,否则全被拒绝。为了避免当出现有大量请求爆发的情况,设置一个burst为5的缓存区,当大量访问爆发时,访问超过了上面的限制可以先放到缓冲区内。
4、案例
案例1:要求每秒只能有一个访问
[root@web01 conf.d]# vim test.conf
limit_req_zone $remote_addr zone=one:10m rate=1r/s;
server {
listen 80;
server_name 192.168.15.7;
limit_req zone=one burst=5;
location / {
root /opt/Super_Marie;
index index.html;
}
}
案例2:要求最大连接数不能超过1
[root@web01 conf.d]# vim test.conf
limit_conn_zone $remote_addr zone=addr:10m;
server {
listen 80;
server_name 192.168.15.7;
limit_conn addr 1;
location / {
root /opt/Super_Marie;
index index.html;
}
}
5、压力测试
1、安装ab测试命令
yum install httpd-tools -y
2、ab的命令参数比较多,我们经常使用的是-c和-n参数
-n : 总共需要访问多少次
-c : 每次访问多少个
3、压力测试
案例2:要求最大连接数不能超过1
[root@web01 conf.d]# ab -n 100000 -c 200 http://192.168.15.7/ # 一共访问10万次,每次访问200
Concurrency Level: 200 # 每次访问的个数
Time taken for tests: 2.802 seconds # 访问的总时间
Complete requests: 100000 # 总共访问的个数
Failed requests: 16527 # 失败的访问数
案例1:要求每秒只能有一个访问
[root@web01 conf.d]# ab -n 100000 -c 200 http://192.168.15.7/
Concurrency Level: 200
Time taken for tests: 7.002 seconds # 访问的总时间为7秒,每秒的访问次数限制为1次
Complete requests: 100000
Failed requests: 99992 # 失败的访问次数,成功的则为8次