I am trying to improve the page speed of a site I run by serving static content directly from NGINX rather than the request hitting PHP.
I have webpages at paths like this:
When a page is generated for these, it's stored in a path like this:
The path structure takes the first 4 letters of the subdomain, and then follows by another folder with the full path and replaces "." with "_" - e.g. "gamea.com" = "/game/gamea_com/". The actual cache page file is stored as "1.c"
How might this be accomplished through NGINX? I'm a bit stuck, I did find this article but I'm unsure how to use it in my case - can anyone provide an example NGINX config that points NGINX to the correct path as described above?
I thank you whoever can help me with this!
The first step is to capture the three parts of the subdomain using a regular expression, then paste them into a root statement. Use named captures as the numeric captures may be out of scope where they are evaluated. See this document for details.
For example:
server {
server_name "~^(?<name1>.{4})(?<name2>.*)\.(?<name3>.*)\.example\.com$";
root /path/to/root/$name1/$name1$name2_$name3;
...
}
Alternatively, decode the $http_host variable using a map. The regular expression is the same, and the result could be used in a root or try_files statement.
For example:
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;
}
}
You can split the try_files across two location blocks with:
root /path/to/root;
location / {
try_files $uri @other;
}
location @other {
try_files /$mypath$uri =404;
}
Swap the terms depending on which files you want Nginx to look for first. Use add_header statements in either location to customise the appropriate response. See this document for details.
Either location can contain specific headers