Nginx - multiple/nested IF statements -


what want do:

  • check if request comes facebook
  • check if url domain.com/2
  • if above conditions true - show content /api/content/item/$1?social=1
  • if above conditions false - show "normal page"

it single page app. before changes configuration looked (and worked):

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

i've tried 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 configuration works expected if both conditions true or false. if 1 of conditions true , second false (or vice versa) nginx returns status 404.

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

any ideas?

best regards.

there article common pitfalls in nignx wiki.

first, i've moved root directive server level. second, location best way check urls. rethink requirements as

  • if location consist of digits
  • and request facebook

we have rewrite url, , 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 no reason have =404 after /index.html in try_files directive.


Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -