server {
listen 80;
server_name 192.168.1.133;
location / {
root /usr/local/nginx/html/web/dist;
# index.html not cache
location /index.html {
expires off;
add_header Cache-Control "no-cache";
}
location /h5-dist {
alias /usr/local/nginx/html/mobile/h5-dist;
# css/js/img allow cache
}
location /css {
expires max;
}
location /js {
expires max;
}
location /img {
expires max;
}
}
这段配置是用于Nginx服务器的,目的是设置不同的缓存策略和处理规则。以下是对这段配置的详细解释:
监听端口和服务器名称:
listen 80;:Nginx监听80端口,这是HTTP协议的默认端口。
server_name 192.168.1.133;:服务器的名称为192.168.1.133,这是服务器的IP地址。
根目录设置:
location / { root /usr/local/nginx/html/web/dist; }:对于所有请求,Nginx将查找文件于/usr/local/nginx/html/web/dist目录下。
特定文件的缓存策略:
location /index.html { expires off; add_header Cache-Control “no-cache”; }:对于index.html文件,设置不缓存,并且添加HTTP头Cache-Control为no-cache,这意味着浏览器在每次请求时都会从服务器获取最新的index.html文件。
移动端的静态资源目录:
location /h5-dist { alias /usr/local/nginx/html/mobile/h5-dist; }:对于路径/h5-dist的请求,Nginx将查找文件于/usr/local/nginx/html/mobile/h5-dist目录下。
静态资源的缓存策略:
location /css { expires max; }:对于路径/css下的CSS文件,设置最大的缓存时间。
location /js { expires max; }:对于路径/js下的JavaScript文件,设置最大的缓存时间。
location /img { expires max; }:对于路径/img下的图片文件,设置最大的缓存时间。
通过这段配置,Nginx服务器能够对不同的请求路径应用不同的缓存策略,优化网站的加载速度和用户体验。例如,index.html文件不缓存,确保用户总是获取最新版本,而CSS、JavaScript和图片文件则尽可能长时间地缓存,减少服务器负担并加快页面加载速度。