Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

618
Views
Serve static content through NGINX, based on subdomain name

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:

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

When a page is generated for these, it's stored in a path like this:

  • /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

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!

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!