当Nginx配置文件中有且只有一个Server的时候,该Server就被Nginx认为是默认网站,所有发送给nginx服务器80端口的数据都会默认给该Server
server {
listen 80;
server_name localhost;
location / { // '/'代表网站的根目录,也就是/usr/local/nginx/html
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
只有一个Server的时候,我们将其称为默认网站,当有多个Server时,将其称为虚拟主机
Nginx目录访问控制
往往在一个网站下会有很多目录
例如:
[root@localhost html] mkdir a
[root@localhost html] mkdir b
[root@localhost html] mkdir c
[root@localhost html] echo "aaaa" ./a/index.html
[root@localhost html] echo "bbbb" ./b/index.html
[root@localhost html] echo "cccc" ./c/index.html
访问页面:
[root@localhost html] elinks 192.168.43.128/a --dump
[root@localhost html] aaaa
windows主机访问192.168.43.128/a
但是我现在只想让我虚拟机本机访问a目录,其他机器拒绝访问,那么就要设置访问控制
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
location /a {
allow localhost; //仅允许本机访问
deny all; //拒绝其他访问
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
测试:
[root@localhost html] killall nginx //杀死nginx
[root@localhost html] ../sbin/nginx //启动nginx
注意: 在生产中,不能直接将nginx杀掉再重启,这样会将业务中断
可以先检测修改配置是否正确:
[root@localhost html] ../sbin/nginx -g ../conf/nginx.conf
nginx: [emerg] unexpected end of parameter, expecting ";" in command line
测试后发现有显示报错,
我们将
location /a {
allow localhost;
deny all;
}
改为
location /a {
allow 192.168.43.128;
deny all;
}
发现可以启动nginx
[root@localhost html] elinks 192.168.43.128/a --dump
aaaa
但是windows浏览器不能访问,返回403
403代表是有目录但是不让访问,404代表的是没有这个目录,我们可以手动让其返回404来迷惑访问者
location /a {
allow 192.168.43.128;
deny all;
return 404;
}
但是每次杀死进程重启进程很麻烦,可以使用:
killall -s HUP nginx 来重新加载配置文件
当然,也可以返回一个网页或者图片,但是图片必须是url形式的
location /a {
allow 192.168.43.128;
deny all;
return http://www.jd.com;
}
接下来想对目录b进行目录用户验证:任何人都可以访问,但是需要凭用户密码才能访问
登陆验证
需要两个指令:
· auth_basic
· auth_basic_user_file fileauth_basic是登录提示语
语法:auth_basic string | off;
默认值:auth_basic off;auth_basic_user_file file是存放验证用户登录的文件所在
例如:
location /b{
auth_basic “登陆验证”;
auth_basic_user_file /etc/nginx/htpasswd; //密码文件存放文件
}
现在 /etc/nginx/htpasswd文件中没有账号和密码
在文件里面书写比较麻烦
在这里可以使用:htpasswd和openssl这两个命令中的一个来添加账号和密码
如果没有htpasswd这个命令的话,htpassword的yum安装包是:httpd-tools
步骤:
1)创建文件目录
[root@localhost ~] mkdir /etc/nginx/
2)添加验证账号密码
[root@localhost ~] htpasswd -m(加密的意思) /etc/nginx/htpasswd sky(正常在文件中直接添加的账户名和密码不能登录,因为密码未加密)
[root@localhost ~] cat /etc/nginx/htpasswd
sky:$apr1$acgnUWV.$eKIIAj/EcIgwJ9EBGbn0W/
日志格式
Nginx访问日志主要有两个参数控制
1) log_format #用来定义记录日志的格式(可以定义多种日志格式,取不同名字即可)
log_format log_name string
2) access_log #用来指定日志文件的路径及使用的何种日志格式记录日志
access_log logs/access.log main;
log_format格式变量
$remote_addr #记录访问网站的客户端地址
$remote_user #记录远程客户端用户名
$time_local #记录访问时间和时区
$request #用户的http请求起始行信息
$status #http状态码、记录请求返回的状态码,例如200、301、404
$body_bytes_sent #服务器发送给客户端的相应body字节数
$http_referer #记录此次请求是从哪个连接访问访问过来的,可以根据该参数设置防盗链设置
$http_user_agent #记录客户端访问信息。例如:浏览器 手机客户端等等
$http_x_forwarded_for #当前端有代理服务器时,设置web节点记录客户端地址的配置,该参数生效的前提是服务器也要进行相关的x_forwarded_for设置
在这里自定义一个日志格式
自定义一个名为zhouxiongxiong的日志 这个日志想知道什么时间、是谁访问了我的什么 并且返回码是多少修改日志文件host.access.log的格式,使它的日志格式为zhouxiongxiong
测试
[root@localhost ~] killall nginx
[root@localhost ~] /usr/local/nginx/sbin/nginx
用浏览器访问一下b目录
[root@localhost logs] cat host.access.log
[06/Nov/2019:15:39:06 +0800] 192.168.43.1 "GET /b/ HTTP/1.1" 304sendfileon
但是这样输出日志不太好看,我们在这里定义一个json格式的日志(key:value)
log_format main_json '{"@timestamp":"$time_local",'
'"client_ip": "$remote_addr",'
'"request": "$request",'
'"status": "$status",'
'"bytes": "$body_bytes_sent",'
'"x_forwarded": "$http_x_forwarded_for",'
'"referer": "$http_referer"'
'}';
用浏览器访问一下b,cat一下日志
{"@timestamp":"06/Nov/2019:15:45:57 +0800","client_ip": "192.168.43.1","request": "GET /b/ HTTP/1.1","status": "304","bytes": "0","x_forwarded": "-","referer": "-"}
防盗链
那什么是盗链?
在这里举个例子,如上图,有个图片提供公司,他有图片服务器,给用户免费提供图片,
它需要给运营商提供带宽费,同样网站的访问率增加。但是这时出现了个"第三者",在这里就为百度吧,百度通过爬虫等等爬到了人家图片服务器的图片。人们发现浏览百度就可以浏览到该图片服务器的照片,那么就不再去原来的图片服务器了,直接去百度。百度在这里起到了一个类似代理的作用。这样的话,图片提供公司不但要给运营商付带宽费,同时用户数量越来越少,这就是盗链。
那怎样防止盗链呢?
在http请求头中,有一个referer字段,这个字段记录着该访问是从哪里来的,我们只需要在需要防盗链的目录中设置信任的访问地址,而将未知和不信任的访问进行拦截就可以
假如c目录里面是图片,需要防盗链,那么在配置文件中输入以下代码
location /c {
valid_referers none blocked *.xiongxiong.com;
if ($invalid_referer){
return 403;
}
注:none代表referer字段没有内容,blocked代表防火墙,*.xiongxiong.com代表我自己的域名
如果不满足以上三个条件,即invalid_referer,那么就返回403
假如对所有的目录都进行防盗链设置,那么在配置文件中输入以下代码
location ~* \.(png|gif|bmp)$ {
valid_referers none blocked *.xiongxiong.com;
if ($invalid_referer){
return 403;
}
注:'~'是匹配的意思 '*'是不区分大小写 '\.(png|gif|bmp)$'是以.png .gif .bmp结尾的