Home > Enterprise >  How to use rewrite in location block with condition properly?
How to use rewrite in location block with condition properly?

Time:10-16

I want to handle requests in nginx in such a way, so when the crawler comes to get a requested page I need to send a static version of single page application by requested URL, otherwise dynamically generated index.html. Static and dynamic pages are located in different folders (required), so I use $src variable for this case.

My config:

server {
 listen       443 ssl http2;
 server_name  www.example.com;
 
 set $src "/www";
 set $crawler 0;

 if ($http_user_agent ~* "examplebot|anotherbot|crawlerbot") {
  set $crawler 1;
  set $src "/www-static";
 }

 root "/var${src}";

 location ~* ^/(en|us|uk)/products/ {
      if ($crawler = 1) {
          rewrite ^/(\w\w)/products/(\w )/(\w ) /pages/$1/$2/$3.html break;
      }
  }
  
 location / {
  try_files $uri /index.html;
 }
}

With the config above, when I go to my website I get 404 error, even if through a normal user-agent (not crawler's).

Crawler request url: https://www.example.com/en/products/dairy/cheese

Static page location: /var/www-static/pages/en/dairy/cheese.html


My expectation is, when the crawler requests e.g. https://www.example.com/en/products/dairy/cheese, it should get the static page and when the normal user requests any page, he should get index.html located in root folder.

I know if statement in nginx is not the best thing in the world, but is there any solution to achieve what I want to, no matter with or without if or rewrite statements, thanks?

I'd like to note that the "redirecting" to the desired folder with $src variable without first location block works without issue. So, I think the problem is not linked to $src variable and its logic.

CodePudding user response:

The logic that sends requests to your index.html is the try_files statement. To replicate that behaviour for URLs handled by the location ~* ^/(en|us|uk)/products/ block, you will need to add a try_files statement to that block too.

For example:

location ~* ^/(en|us|uk)/products/ {
    if ($crawler = 1) { ... }
    try_files $uri /index.html;
}
  
location / {
    try_files $uri /index.html;
}
  • Related