动静分离是让动态网站里的动态网页根据一定规则把不变的资源和经常变的资源区分开来,动静资源做好了拆分以后,我们就可以根据静态资源的特点将其做缓存操作,这就是网站静态化处理的核心思路。
upstream test{ server weiyigeek.top:8080; server weiyigeek.top:8081; } server { listen 80; server_name weiyigeek.top; location / { root e:wwwroot;//文件夹路径 index index.html; } # 所有静态请求都由nginx处理,存放目录为nginx中的html location ~ .(gif|jpg|jpeg|png|bmp|swf|css|js)$ { root e:wwwroot; } # 例子,比如 #location /css{ #root html; #index index.html index.htm; #} # 所有动态请求都转发给tomcat处理 location ~ .(jsp|do)$ { proxy_pass http://test; } error_page 500 502 503 504 /50x.html; location = /50x.html { root e:wwwroot; } }
- =:用于不含正则表达式的uri前,要请求字符串与uri严格匹配,如果匹配成功,就停止继续向下搜素并立即处理该请求。
- ~:用于表示uri包含正则表达式,并且区分大小写。
- ~*:用于表示uri包含正则表达式,并且不区分大小写。
- ^~:用于不含正则表达式的uri前,要求Nginx服务器找到标识uri和请求字符串匹配度最高的location后,立即使用此location处理请求,而不再使用location块中正则uri和请求字符串做匹配。
注意:如果uri包含正则表达式,则必须要有~或者~*标识!!!
location 匹配规则:
- 多个正则
location
直接按书写顺序匹配,成功后就不会继续往后面匹配 - 普通(非正则)
location
会一直往下,直到找到匹配度最高的(最大前缀匹配) - 当普通
location
与正则location
同时存在,如果正则匹配成功,则不会再执行普通匹配 - 所有类型
location
存在时,“=”匹配 > “^~”匹配 > 正则匹配 > 普通(最大前缀匹配)
location ~*/(css|img|js)$ { root /usr/local/nginx/static; index index.html index.htm; }