一、location详解
在server中location字段使用
语法规则
location [ = | ~ | ~* | ^~] uri { ... }
#匹配优先级
location优先级:(location =) > (location ^~ 路径) > (location ~,~* 正则顺序) >
(location 完整路径) > (location 部分起始路径) > (/)
- =:精确匹配
#精确匹配/usr/share/nginx/images/aaa.jpg
location = /aaa.jpg {
root /usr/share/nginx/images;
}
- ~:区分大小写的正则匹配
#以A开头匹配的jpg,区分大小写,?表示A后面零次或一个字符
location ~ /A.?\.jpg {
root /usr/share/nginx/images;
}
- ^~:对URL开头做匹配检查
#匹配任何以/images/开头的地址,匹配符合以后,停止往下搜索正则,采用这一条
location ^~ /images {
root /usr/share/nginx;
}
- ~*:不区分大小写的正则匹配
#以A开头匹配的jpg,区分大小写,?表示A后面零次或一个字符
location ~* /A.?\.jpg {
root /usr/share/nginx/images;
}
#文件后缀名匹配
location ~* \.(gif|jpg|jpeg|bmp|png|tiff|tif|ico|wmf|js|css)$ {
root /usr/share/nginx/static;
index index.html;
}
二、Nginx四层访问控制
ngx_http_access_module模块实现访问控制:匹配客户端源IP地址进行限制,官方文档参考,请点击此处
location = /aaa.jpg {
root /usr/share/nginx/images;
deny 192.168.1.1;
allow 10.0.0.10/24;
deny all;
}
三、Nginx账户认证
官网文档参考,点击此处
location / {
auth_basic "closed site";
auth_basic_user_file /etc/nginx/conf.d/.htpasswd;
}
yum -y install httpd-tools
#非交互式创建用户密码
htpasswd -cb /etc/nginx/conf.d/.htpasswd ammon 123456
四、检测文件是否存在
location / {
try_files $uri $uri.html $uri/index.html /usr/share/nginx/html/default.html;
#或者直接启用响应码
try_files $uri $uri/index.html $uri.html =489;
}
五、下载服务器和上传服务器配置
ngx_http_autoindex_module 模块处理以斜杠字符 “/” 结尾的请求,并生成目录列表,可以做为下载服务
配置使用,官方文档参考,请点击此处
#1.下载服务器配置
location /download {
root /usr/share/nginx;
#自动文件索引功能,默为off
autoindex on | off;
#计算文件确切大小(单位bytes),off 显示大概大小(单位K、M),默认on
autoindex_exact_size on | off;
#显示本机时间而非GMT(格林威治)时间,默认off
autoindex_localtime on | off ;
autoindex_format html | xml | json | jsonp; #显示索引的页面文件风格,默认html
#限制响应客户端传输速率,单位B/s,默认值0,表示无限制
limit_rate rate;
#变量限速,单位B/s,同时设置,此项优级高
set $limit_rate 4k;
}
#2.上传服务器配置
location /download {
root /usr/share/nginx/download;
#设置上传单个文件的最大值,默认值为1m,上传文件超过此值会出413错误
client_max_body_size 1m;
#缓冲区大小;默认16k;超出时,其将被暂存到client_body_temp_path位置
client_body_buffer_size size;
#临时存储路径及子目录结构和数量,目录名为16进制的数字,使用hash之后的值从后往前截取1位、2位、2位作为目录名
client_body_temp_path path [level1 [level2 [level3]]];
}
六、状态页:依赖ngx_http_stub_status_module,编译安装时需增加编译参数with-http_stub_status_module
location = /basic_status {
stub_status;
}
七、Nginx变量部分介绍:内置变量和自定义变量,echo属于第三方模块,编译后才可正常使用,echo开源模块网址
#常见内置变量
location /var{
root /usr/share/nginx;
echo "hello world,main-->";
#便于client经过多个代理服务器后,Nginx仍然可以识别真实Client IP
$proxy_add_x_forwarded_for
#客户端公网IP
echo $remote_addr ;
#URI?args 参数
echo $args ;
#当前资源请求的系统根目录
echo $document_root;
#不包含参数和请求指令,http://www.ammon.com/var/index.do?id=20220127&partner=search,将返回/var/index.do
echo $document_uri;
#请求host名称
echo $host;
#客户端浏览器的详细信息
echo $http_user_agent;
#客户端的所有cookie信息
echo $http_cookie;
#当前请求的资源文件的磁盘路径
echo $request_filename;
#请求的协议,例如:http,https,ftp等
echo $scheme;
echo $scheme://$host$document_uri?$args;
#限速
limit_rate 1024;
echo $limit_rate;
#客户端端口
$remote_port;
}
#自定义变量
location /var{
root /usr/share/nginx;
set $name ammon;
echo $name;
#$server_port请求服务器端口号
set $my_port $server_port;
echo $my_port;
echo "$server_name:$server_port";
}
以上内容均参考Nginx官网,在学习中进步,如有错误,请多多批评指正