Nginx-location

Nginx-学习目录

1、基础

1.1、什么是location

Location用来控制访问网站的uri路径

1.2、location语法

Syntax: location [ = | ~ | ~* | ^~ ] uri { ... }
location @name { ... }
Default: —
Context: server, location

匹配符     匹配规则                       优先级
=          精确匹配                       1
^~         以某个字符串开头                2
~          区分大小写的正则匹配             3
~*         不区分大小写的正则匹配           4
/          通用匹配,任何请求都会匹配到      5

2、实战

2.1、location优先级语法-示例

2.1.1、配置nginx

cat >/etc/nginx/conf.d/location.cyc.com.conf <<'EOF'
server {
 listen 80;
 server_name location.cyc.com;

 location = / {
  return 200 "location = /";
  default_type text/html;
 }

 location / {
  return 200 "location /";
  default_type text/html;
 }

 location /documents/ {
  return 200 "location /documents";
  default_type text/html;
 }

 location ^~ /images/ {
  return 200 "location ^~/images/";
  default_type text/html;
 }

 location ~* \.(gif|jpg|jpeg)$ {
  return 200 "location  (gif|jpg|jpeg)";
  default_type text/html;
 }
}
EOF

2.1.2、重新加载nginx

systemctl reload nginx

2.1.3、配置hosts

192.168.10.101 location.cyc.com

2.1.4、测试访问

]# curl http://location.cyc.com/
location = /

]# curl http://location.cyc.com/index.html
location /

]# curl http://location.cyc.com/documents/1.html
location /documents

]# curl http://location.cyc.com/images/1.jpeg
location ^~/images/

]# curl http://location.cyc.com/test/1.jpeg
location  (gif|jpg|jpeg)

2.2、locaiton规则配置应用场景-示例

2.2.1、配置nginx

cat >/etc/nginx/conf.d/location.cyc.com.conf <<'EOF'
server {
 listen 80;
 server_name location.cyc.com;
 
 # 通用匹配,任何请求都会匹配到
 location / {
   root html;
   index index.html;
 }
 
 # 精准匹配,必须请求的uri是/nginx status
 location = /nginx_status {
   stub_status;
 }

 # 严格区分大小写,匹配以.php结尾的都走这个Location
 location ~ \.php$ {
   return 200 "php访问成功";
   default_type text/html;
 }
 
 # 严格区分大小写,匹配以.jsp结尾的都走这个Location
 location ~ \.jsp$  {
   return 200 "jsp 访问成功";
   default_type text/html;
 }
 
 # 不区分大小写匹配,只要用户访问.jpg,gif,png,js,css 都走这条Location
 location ~* \.(jpg|gif|png|js|css)$ {
   return 403;
 }

 # 不区分大小写匹配,禁止访问
 location ~* \.(sql|bak|tgz|tar.gz|.git)$ {
   deny all;
 }
}
EOF

2.2.2、重新加载nginx

systemctl reload nginx

2.2.3、测试访问

]# curl location.cyc.com
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.24.0</center>
</body>
</html>

]# curl location.cyc.com/nginx_status
Active connections: 1 
server accepts handled requests
 15 15 19 
Reading: 0 Writing: 1 Waiting: 0 


]# curl location.cyc.com/test.php
php访问成功

]# curl location.cyc.com/test.jsp
jsp 访问成功


]# curl location.cyc.com/test/test.png -I 
HTTP/1.1 403 Forbidden
Server: nginx/1.24.0
Date: Thu, 27 Apr 2023 00:00:26 GMT
Content-Type: text/html
Content-Length: 153
Connection: keep-alive

]# curl location.cyc.com/test/test.sql -I 
HTTP/1.1 403 Forbidden
Server: nginx/1.24.0
Date: Thu, 27 Apr 2023 00:00:34 GMT
Content-Type: text/html
Content-Length: 153
Connection: keep-alive

 

posted @ 2023-04-27 21:08  小粉优化大师  阅读(22)  评论(0编辑  收藏  举报