根据子域名通过NGINX提供静态内容

时间:2020-04-03 02:55:55

标签: php nginx

我试图通过直接从NGINX提供静态内容而不是向PHP发送请求来提高我运行的网站的页面速度。

我在这样的路径上有网页:

  • gamea.com.mysite.com
  • anotherb.net.mysite.com
  • finalc.org.mysite.com

为这些页面生成页面时,它存储在这样的路径中:

  • /storage/app/page-cache/games/game/gamea_com/1.c
  • /storage/app/page-cache/games/anot/anotherb_net/1.c
  • /storage/app/page-cache/games/fina/finalc_org/1.c

路径结构采用子域的前4个字母,然后跟随具有完整路径的另一个文件夹,并替换为“”。与“ _”-例如“ gamea.com” =“ / game / gamea_com /”。实际的缓存页面文件存储为“ 1.c”

如何通过NGINX实现?我有点卡住了,但确实找到了this article,但是我不确定如何在我的情况下使用它-任何人都可以提供示例NGINX配置,该配置将NGINX指向如上所述的正确路径吗?

我感谢您能帮助我解决这个问题的人!

1 个答案:

答案 0 :(得分:1)

第一步是使用正则表达式捕获子域的三个部分,然后将其粘贴到root语句中。使用命名捕获,因为数字捕获可能超出了评估范围。有关详细信息,请参见this document

例如:

server {
    server_name  "~^(?<name1>.{4})(?<name2>.*)\.(?<name3>.*)\.example\.com$";
    root /path/to/root/$name1/$name1$name2_$name3;
    ...
}

或者,使用$http_hostmap变量进行解码。正则表达式相同,结果可以用在roottry_files语句中。

例如:

map $http_host $mypath {
    default                                                     "nonexistent";
    "~^(?<name1>.{4})(?<name2>.*)\.(?<name3>.*)\.example\.com$" $name1/$name1$name2_$name3;
}
server {
    ...
    root /path/to/root;
    location / {
        try_files $uri /$mypath$uri =404;
    }
}

您可以使用以下方法将try_files分为两个location块:

root /path/to/root;
location / {
    try_files $uri @other;
}
location @other {
    try_files /$mypath$uri =404;
}

根据您希望Nginx首先查找的文件交换术语。在add_header中使用location语句来自定义适当的响应。有关详情,请参见this documentlocation都可以包含特定的标头

相关问题