静态文件存放与转发配置

发布于 2020-08-19  388 次阅读


  • 为了缓存网站80%的可用HTML静态文件,在nginx配置时优先读取 服务的静态文件,如果静态文件不存在则继续进行php程序的解析,静态文件的存放遵循 /data/$host/[前置path]/[计算后的文件夹]/[文件名] 进行存放
  • [计算后的文件夹]= 文件名中的数字 / 100000 为一个单位进行文件夹存放
  • 文件夹的使用是为了防止静态文件因时间增长后过多,造成性能瓶颈
  • 因为nginx本身不能进行 运算符计算,需要引入外部module进行扩展,选用 nginx-let-module

nginx-let-module 插件安装插件所在地址:https://github.com/arut/nginx-let-module

先 nginx -V 查出原先./configure 时的参数
在 编译安装的 nginx源码目录中,重新执行

--prefix=/usr/local/nginx --with-http_stub_status_module  --add-module=../nginx-let-module

或者新装

./configure --prefix=/usr/local/nginx --with-http_stub_status_module 
--with-http_ssl_module  --add-module=../nginx-let-module

使用--add-module参数将插件加入进行编译

然后执行 make & make install

将安装目录中的objs/nginx 复制 替换到 /usr/local/nginx/sbin中即可,执行重启

nginx 站点文件配置

所有的静态文件存放在 /data/[cms.jwyo.com]/下

location ~ (.*?)/([a-z]+)([0-9]+)\.html$ {
    #此处root只在内部有效,而且是只针对try_filest第一参数路径有效,其后面返回执行则使用全局路径
    root /data/$host;
    set $id $3;
    let $dir $id / 100000; #let用法就是使用了 nginx-let-module插件
    #default_type    text/plain;
    #return 502 $dir; #可以用此方法输出$dir值
    #以下就是相应路径文件进行try_files后如果文件存在直接返回,不存在时进行php解析
    try_files $1/$dir/$2$3.html /index.php?$query_string;
}
线上配置
location ~* ^/([a-z]+)/([a-z]+_)(\d+)\.html$ {
            root /data/$host; #如果root无效,可以用alias
            set $id $3;
            let $dir $id / 10000;
            #default_type    text/plain;
            #return 502 $dir;
            try_files $1/$dir/$3.html /index.php$request_uri;
        }