
代理静态资源是nginx的强项,可以减少请求对后台服务器的压力,使响应更加迅速。
根据静态资源url和具体资源路径是否相同,可以分为两类:
url和静态资源相同
可以通过配置root指令来实现
如url为 : 127.0.0.1:8000/asset/img/a.png
对应的静态资源路径path: /home/static/asset/img/a.png
通过root来配置:
server { listen 8000; server_name localhost; location /asset/img/ { root /home/static/; } }
此时静态资源的真实路径=root + url
url和静态资源不同
通过alias来配置
如url为 : 127.0.0.1:8000/asset/img/a.png
对应的静态资源路径path: /home/static/images/a.png
通过alias来配置
server { listen 8000; server_name localhost; location /asset/img/{ alias /home/static/images/; } }
用到alias属性,alias会抛弃url,直接访问alias指定路径