Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nginx - multiple/nested IF statements

Tags:

nginx

What i want to do:

  • Check if request comes from Facebook
  • Check if URL is like domain.com/2
  • If above conditions are true - show content from /api/content/item/$1?social=1
  • If above conditions are false - show "normal page"

It is a single page app. Before my changes configuration looked like this (and it worked):

location / {
    root /home/eshlox/projects/XXX/project/project/assets/dist;
    try_files $uri $uri/ /index.html =404;
}

I've tried to use if statements:

location / {
    set $social 1;
    if ($http_user_agent ~* "facebookexternalhit") {
        set $social UA; 
    }       
    if ($uri ~* "^/(\d+)$") {
        set $social "${social}URL";
    }       
    if ($social = UAURL) {
        rewrite ^/(\d+)$ /api/content/item/$1?social=1;
    }

    root /home/eshlox/projects/XXX/project/project/assets/dist;
    try_files $uri $uri/ /index.html =404;
}

With this configuration everything works as i expected only if both conditions are true or false. If one of conditions is true and the second is false (or vice versa) then nginx always returns status 404.

I have found "IfIsEvil" on nginx site, i've tried to use mapping (can i use mapping in this case?) but still i can't resolve this problem.

Any ideas?

Best regards.

like image 673
eshlox Avatar asked Mar 23 '14 13:03

eshlox


1 Answers

There is good article about common pitfalls in nignx wiki.

First, I've moved root directive to server level. Second, location is the best way to check urls. So I rethink your requirements as

  • if location consist of digits
  • and request from facebook

we have to rewrite url, and the result is:

root /home/eshlox/projects/XXX/project/project/assets/dist;

location / {
    try_files $uri $uri/ /index.html;
}

location ~ "^/\d+$" {
    if ($http_user_agent ~* "facebookexternalhit") {
        rewrite (.+) /api/content/item$1?social=1;
    }

    try_files $uri $uri/ /index.html;
}

Also, there is almost no reason to have =404 after /index.html in try_files directive.

like image 66
Alexey Ten Avatar answered Oct 10 '22 00:10

Alexey Ten