Nginx动静分离以及location讲解

89 阅读4分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第15天,点击查看活动详情

一、概念

动静分离从目的实现的角度,大致分为两类:

  • 1.纯粹把静态文件独立成单独的域名,放在独立的服务器上,这种是目前主流推崇的方案之一;
  • 2.将动态资源和静态资源混合在一起发布,通过nginx调度。

二、匹配静态资源

vi /usr/local/nginx/conf/nginx.conf,在默认server中添加如下location,用以匹配静态资源。

server {

listen 80; //nginx对外服务端口

server_name localhost; //对外服务ip,即本服务器

location ~ .*.(gpg|png|css|jpg) { //匹配的静态资源

root /usr/local/nginx/static; //静态资源所在路径

}

}

启动nginx服务,打开http://192.168.116.12:80/a.jpg验证:

三、匹配动态资源

  • 在根目录下创建data文件夹,创建html,用于存放网页。
  • 修改Nginx的配置文件。

server {
	listen 80//nginx对外服务端口
	server_name localhost; //对外服务ip,即本服务器
  location /html/ { //匹配的动态资源
        root  /data/;
        index  index.html index.htm;
  }
}

浏览器地址栏访问show.html:

四、动静分离

分别指定location,分别匹配动态资源和静态资源即可!

  • 浏览器地址栏请求图片等静态资源时,进入到images。
  • 浏览器地址栏请求动态资源时,进入到html。
   
server {listen       80;server_name  192.168.172.128;
   location /html/ {
        root  /data/;
        index  index.html index.htm;
    }
    location /images/ {
        root  /data/;
        autoindex  on;
    }
}

五、location匹配规则

5.1 location 原理

location是Nginx中的块级指令(block directive),,location指令的功能是用来匹配不同的url请求,进而对请求做不同的处理和响应,这其中较难理解的是多个location的匹配顺序,本文会作为重点来解释和说明。

5.2 语法

location有两种匹配规则:

  • 匹配URL类型,有四种参数可选,当然也可以不带参数。
  • location [ = | ~ | ~* | ^~ ] uri { … }
  • 命名location,用@标识,类似于定于goto语句块。
  • location @name { … }

5.3 参数解释

(1) “=” ,精确匹配

location = /abc/ {
  .....
 }
        
# 只匹配http://abc.com/abc
#http://abc.com/abc [匹配成功]
#http://abc.com/abc/index [匹配失败]

(2) “~”,执行正则匹配,区分大小写。

location ~ /Abc/ {
  .....
}
#http://abc.com/Abc/ [匹配成功]
#http://abc.com/abc/ [匹配失败]

(3)“~*”,执行正则匹配,忽略大小写

location ~* /Abc/ {
  .....
}
# 则会忽略 uri 部分的大小写
#http://abc.com/Abc/ [匹配成功]
#http://abc.com/abc/ [匹配成功]

(4)“^~”,表示普通字符串匹配上以后不再进行正则匹配。

location ^~ /index/ {
  .....
}
#以 /index/ 开头的请求,都会匹配上
#http://abc.com/index/index.page  [匹配成功]
#http://abc.com/error/error.page [匹配失败]

(5)不加任何规则时,默认是大小写敏感,前缀匹配,相当于加了“”与“^

location /index/ {
  ......
}
#http://abc.com/index  [匹配成功]
#http://abc.com/index/index.page  [匹配成功]
#http://abc.com/test/index  [匹配失败]
#http://abc.com/Index  [匹配失败]
# 匹配到所有uri

(6)“@”,nginx内部跳转


location /index/ {
  error_page 404 @index_error;
}
location @index_error {
  .....
}
#以 /index/ 开头的请求,如果链接的状态为 404。则会匹配到 @index_error 这条规则上。

5.4 匹配顺序

= > ^~ > ~ | ~* > 最长前缀匹配 > /

六、请求转发

当在一台主机上部署了多个不同的web服务器,并且需要能在80端口同时访问这些web服务器时,可以使用 nginx 的反向代理功能: 用 nginx在80端口监听所有请求,并依据转发规则(比较常见的是以 URI 来转发)转发到对应的web服务器上。

例如有 webmail , webcom 以及 webdefault 三个服务器分别运行在 portmail , portcom , portdefault 端口,要实现从80端口同时访问这三个web服务器,则可以在80端口运行 nginx, 然后将 /mail 下的请求转发到 webmail 服务器, 将 /com下的请求转发到 webcom 服务器, 将其他所有请求转发到 webdefault 服务器。

假设服务器域名为example.com,则对应的 nginx http配置如下:

http {
    server {
            server_name example.com;

            location /mail/ {
                    proxy_pass http://example.com:protmail/;
            }

            location /com/ {
                    proxy_pass http://example.com:portcom/main/;
            }

            location / {
                    proxy_pass http://example.com:portdefault;
            }
    }
}

将后面的一些转发到后台服务器:

七、正则表达式

正则表达式

*:重复前面的字符0次或多次

?:重复前面的字符0次或1次

+:重复前面的字符1次或多次

.:匹配除换行符以外的任意一个字符

(a|b):匹配a或b

^:以...开头

$:以...结尾

{n}:重复前面的字符n次

{n,}:重复前面的字符n次或更多次

{n,m}:重复前面的字符n-m次

*?:重复前面的字符0次或多次,但尽可能少重复

+?:重复前面的字符1次或多次,但尽可能少重复

??:重复前面的字符0次或1次,但尽可能少重复

{n,m}?:重复前面的字符n-m次,但尽可能少重复

{n}?:重复前面的字符n次以上,但尽可能少重复

参考链接:

zhuanlan.zhihu.com/p/137042956

www.tofacebook.com/view/158198

www.cnblogs.com/xiao-xue-di…