Nginx location匹配
针对用户请求的URL进行匹配出来,然后进行对应的处理
Location [ = | ~ | ~* | ^~ ] url {
#做出相应的处理动作
}
nginx有关location的匹配 符号如下
匹配符号 匹配会规则 优先级
= 精确匹配 1
^~ 以某个字符开头,不做正则处理 2
~* 支持正则的匹配模式 3
/blog/ 当你访问192.168.18.128/blog/xxx 4
/ 通用匹配,不符合其它的location的匹配规则,就走到这里 5
表示nginx.conf支持在虚拟主机中定义多个location,进行用户的请求url解析
案例动静分离小例子
[root@redis1 conf.d]# vim pc.conf
server {
server_name www.magedu.org
root /data/nginx/html/pc;
location ~* .(gif|jpg|jpeg|bmp|png|tiff|tif|ico|wmf|js|css) {
root /opt/pc/app;
}
}
[root@redis1 conf.d]# mkdir -p /opt/pc/{static,app}
[root@redis1 static]# ls
1.jpg index.html
[root@redis1 app]# cat test.php
<?php
phpinfo();
?>
[root@redis1 ~]# systemctl reload nginx
访问
http://www.magedu.org/1.jpg
http://www.magedu.org/text.php
访问控制,10.9.9.9这个网段可以访问,其他都拒绝
server {
listen 80;
server_name www.magedu.org;
location / {
root /data/nginx/html/pc;
allow 10.0.0.0/24;
deny all;
}
}
账户认证功能
安装工具软件
yum install httpd-tools -y
生成密码文件
htpasswd -cb /apps/nginx/conf/conf.d/.nginx-user xiaoming 123456
htpasswd -b /apps/nginx/conf/conf.d/.nginx-user xiaohong 123456
[root@redis1 conf.d]# vim pc.conf
server {
listen 80;
server_name www.magedu.org;
root /data/nginx/html/pc;
location /admin {
auth_basic "login password";
auth_basic_user_file /apps/nginx/conf/conf.d/.nginx-user;
}
}
[root@redis1 ~]# mkdir -p /data/nginx/html/pc/admin
[root@redis1 ~]# echo "admin" > /data/nginx/html/pc/admin/index.html
[root@redis1 conf.d]# nginx -s reload
访问
http://www.magedu.org/
自定义错误页面
[root@redis1 logs]# mkdir -p /data/html/error
[root@redis1 app]# cat /apps/nginx/conf/conf.d/pc.conf
server {
listen 80;
server_name www.magedu.org;
root /data/nginx/html/pc;
location /admin {
auth_basic "login password";
auth_basic_user_file /apps/nginx/conf/conf.d/.nginx-user;
}
error_page 404 /40x.html;
location = /40x.html {
root /data/html/error/ ;
}
}
[root@redis1 logs]# mkdir -p /data/html/error
[root@redis1 error]# ls
40x.html
[root@redis1 conf.d]# nginx -s reload
访问
http://www.magedu.org/xx
如果404就跳转到到首页
server {
listen 80;
server_name www.magedu.org;
root /data/nginx/html/pc;
location /admin {
auth_basic "login password";
auth_basic_user_file /apps/nginx/conf/conf.d/.nginx-user;
}
error_page 404 =302 /index.html;
}
检测文件是否存在不论输入啥域名后面都跳转到首页
[root@redis1 app]# vim /apps/nginx/conf/conf.d/pc.conf
server {
listen 80;
server_name www.magedu.org;
root /data/nginx/html/pc;
try_files uri.html $uri/index.html /index.html;
location /admin {
auth_basic "login password";
auth_basic_user_file /apps/nginx/conf/conf.d/.nginx-user;
}
}
长连接配置
server {
listen 80;
server_name www.magedu.org;
root /data/nginx/html/pc;
try_files uri.html $uri/index.html /index.html;
keepalive_requests 3; 最多限制下载3个资源就断开,根据业务来
keepalive_timeout 60; 保持60秒就断
location /admin {
auth_basic "login password";
auth_basic_user_file /apps/nginx/conf/conf.d/.nginx-user;
}
}
博客系统的话必须改,不然默认值太低

状态页
server {
listen 80;
server_name www.magedu.org;
root /data/nginx/html/pc;
location /repo {
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
limit_rate 1024k;
}
location /nginx_status {
stub_status;
auth_basic "login password";
auth_basic_user_file /apps/nginx/conf/conf.d/.nginx-user;
allow 10.0.0.0/16;
allow 127.0.0.1;
deny all;
}
}
日志
默认日志配置
server {
listen 80;
server_name www.magedu.org;
root /data/nginx/html/pc;
access_log /apps/nginx/logs/www.magedu.org.access.log;
location /repo {
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
limit_rate 1024k;
}
location /nginx_status {
stub_status;
auth_basic "login password";
auth_basic_user_file /apps/nginx/conf/conf.d/.nginx-user;
allow 10.0.0.0/16;
allow 127.0.0.1;
deny all;
}
}
json日志定制,用于elk
修改总的配置文件
[root@redis1 app]# vim /apps/nginx/conf/nginx.conf
[root@redis1 app]# egrep -v "#|^time_iso8601",'
'"host":"remote_addr",'
'"size":upstream_response_time",'
'"http_host":"uri",'
'"xff":"http_referer",'
'"tcp_xff":"http_user_agent",'
'"status":"$status"}';
sendfile on;
keepalive_timeout 65;
include /apps/nginx/conf/conf.d/*.conf;
}
[root@redis1 app]# vim /apps/nginx/conf/conf.d/pc.conf
server {
listen 80;
server_name www.magedu.org;
root /data/nginx/html/pc;
access_log /apps/nginx/logs/www.magedu.org.access.log;
access_log /apps/nginx/logs/access-json-www.magedu.org.log access_json;
location /repo {
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
limit_rate 1024k;
}
location /nginx_status {
stub_status;
auth_basic "login password";
auth_basic_user_file /apps/nginx/conf/conf.d/.nginx-user;
allow 10.0.0.0/16;
allow 127.0.0.1;
deny all;
}
}
[root@redis1 app]# nginx -s reload
[root@redis1 logs]# pwd
/apps/nginx/logs
[root@redis1 logs]# ls
access-json-www.magedu.org.log access.log error.log nginx.pid www.magedu.org.access.log
[root@redis1 logs]# cat access-json-www.magedu.org.log
{"@timestamp":"2023-01-12T00:34:26+08:00","host":"10.0.0.8","clientip":"10.0.0.1","size":103,"responsetime":0.000,"upstreamtime":"-","upstreamhost":"-","http_host":"www.magedu.org","uri":"/nginx_status","xff":"-","referer":"-","tcp_xff":"-","http_user_agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36","status":"200"}
查看json日志可以使用工具
yum install -y jq
cat access-json-www.magedu.org.log |jq
