nginx默认网站
当Nginx配置文件里 有且仅有一个 Server 的时候,该server就被nginx认为是默认网站,所有发送给nginx服务器80端口的数据都会默认给 该Server
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| server { # 虚拟主机使用的端口 listen 80; # 虚拟主机域名 server_name localhost;
# 字符集 #charset koi8-r; # 虚拟主机的访问日志路径 如果你不设置 就会使用全局的日志 #access_log logs/host.access.log main;
# 定义web根路径 location / { # 根目录路径 root 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 html; } }
|
nginx 目录访问控制
如果你的其他电脑访问不到请关闭防火墙
1 2 3 4 5 6 7 8 9 10 11 12 13
| https://blog.csdn.net/cool_summer_moon/article/details/78744009
# 查看默认防火墙状态(关闭后显示not running,开启后显示running) firewall-cmd --state
# 开启防火墙 systemctl start firewalld.service
# 关闭防火墙: systemctl stop firewalld.service
# 重启防火墙 systemctl restart firewalld.service
|
1 2 3 4 5 6 7 8 9
| # 去nginx 的网站根目录 cd /usr/local/nginx/html # 新建三个目录 a b c mkdir a mkdir b mkdir c echo a > c/index.html echo b > b/index.html echo c > c/index.html
|
修改nginx配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| server { listen 80; server_name localhost; charset utf-8; #access_log logs/host.access.log main;
# 定义web根路径 location / { # 根目录路径 root html; index index.html index.htm; }
# 对a路径的范围设置 location /a { # 允许个别 allow localhost; allow 你本机ip # 拒绝所有 deny all; # 迷惑别人 # return 404; # 还可以返回京东 return http://www.jd.com; } # 错误路径 #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 html; } }
|
不要太粗暴的重启nginx
1 2 3 4 5 6 7 8 9 10
| # 杀掉nginx进程 killall nginx # 此时不要这样 # /usr/local/ngin/sbin/nginx 直接启动
# 应该先检查配置文件是否正确 /usr/local/ngin/sbin/nginx /usr/local/ngin/conf/nginx.conf
# 直接重新加载配置文件的方法 killall -s HUP nginx
|
访问验证,需要登录
auth_basic
- 语法 auth_basic string | off;
- 默认值 auth_basic off;
auth_basic_user_file file;
1 2 3 4 5 6 7 8 9
| # 目录用户验证:任何人都可以访问,但是需要用户密码才能访问 location /b { auth_basic "hjx的登录验证唯一性(加点料)dfsdafsdafk321kj3h12"; auth_basic_user_file /etc/nginx/htpasswd; }
# 解释 这个是你放账号密码的文件 # /etc/nginx/htpasswd
|
此时账号密码还没有(而且对应/etc/nginx/htpasswd目录也没有你需要建立目录)
生成账号密码
1 2
| # 安装 yum -y install httpd-tools
|
建立目录文件
1 2 3 4 5 6 7 8 9 10 11 12 13
| mkdir /etc/nginx/htpasswd
vi /etc/nginx/htpasswd # 输入如下内容 sko:123456
# 重启nginx killall -s HUP nginx
# 然后访问 http://xx.xx.xx.xx/b 目录 输入账号 sko 输入密码 123456 # 还是失败 ! 因为你没有加密
|
加密两种方式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| # 如果你没有那个加密的文件 以及对应的目录 htpasswd -c /etc/nginx/htpasswd 用户名 # 回车 输入你的密码 回车 再次输入你的密码 回车
# 如果你有那个文件 你就 htpasswd -m /etc/nginx/htpasswd 用户名 # 回车 输入你的密码 回车 再次输入你的密码 回车
编辑你的 /etc/nginx/htpasswd 因为刚才还存留上次的 sko:123456 删掉这行
# 此时你就可以输入密码后 访问 b目录了
|
日志格式
Nginx 访问日志主要有两个参数控制
- log_format #⽤用来定义记录⽇日志的格式(可以定义多种⽇日志格式,取不不同名字即可)
log_format log_name string
- access_log #⽤用来指定⽇日⾄至⽂文件的路路径及使⽤用的何种⽇日志格式记录⽇日志
access_log logs/access.log main
log_format格式变量量:
1 2 3 4 5 6 7 8 9
| $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设置
|
自定义日志
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| http { include 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 logs/access.log main; # 自定义日志 # 自定义日志 # 自定义日志 log_format hjx_log '[$time_local] $remote_addr $request $status'
server{ # ... # 此处指定 日志格式文件 access_log logs/host.access.log hjx_log
# ... } }
|
重启nginx
去你的浏览器访问 你对应 ip的地址
查看日志是否生效
1 2 3 4
| cat /usr/local/nginx/logs/host.access.log
#或者 滚动输出 tailf /usr/local/nginx/logs/host.access.log
|
运维关心下json格式的日志定义
自定义日志格式为json
1 2 3 4 5 6 7 8
| 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"' '}’; access_log logs/access_json.log main_json;
|
nginx.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| http { include 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 logs/access.log main; # 自定义日志 # 自定义日志 # 自定义日志 log_format hjx_log '[$time_local] $remote_addr $request $status'; # json格式自定义日志 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",' '}’;
server{ # ... # 此处指定 日志格式文件 access_log logs/host.access.log main_json
# ... } }
|
防盗链
先上传到你的服务器一个图片
1 2 3 4
| scp 本地文件路径 centos文件存放地路径
scp /Users/huangjiaxi/Desktop/aa.png root@xx.xx.xx.xx:/usr/local/nginx/html/c ```
|
c目录下
location /c {
宽广匹配 ~
* 代表不区分大小写
location ~* .(png|gif|bmp)$ {
# valid_referers none 代表用户直接访问的
# blocked 有防火墙标志的
# *.ayitula.com; 我自己的服务器域名下的
valid_referers none blocked *.ayitula.com;
# 假如是非法 referer直接返回403
if ($invalid_referer) {
return 403;
}
}
`