Nginx实现动静分离
Nginx实现动静分离,其实就是在反向代理的时候,如果是静态资源,那么就直接从Nginx发布的路径去读取,而不需要从后台服务器获取了。何为静态资源,就是脱离容器(Tomcat)可以直接运行或者使用的资源,比如js、css、图片等。
具体配置,如下:
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;
#server
location /shop/ {
proxy_pass http://webservers;
proxy_set_header X-Real-IP $remote_addr;
#root /usr/share/nginx/html;
#index index.html index.htm;
}
#static file
location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|js|css)$ {
root /usr/share/nginx/html;
#cache
expires 3d;
}
}