Bundy
Bundy

Reputation: 311

NGINX config regex issue

I'm a little bit lost here. In my nginx conf file I want a rule that states this:

if $request_uri does not contain following words (css|images|js) then rewrite all to /index.php

this is what I got, but it doesn't work:

if ($request_uri ~ !(images|css|js)) {
    rewrite $ /index.php;
}

I think the regex isn't matching up?

Edit: this is the solution

if ($request_uri !~* (images|css|js)) {
    rewrite $ /index.php;
}

Upvotes: 0

Views: 1735

Answers (1)

Eric Fortis
Eric Fortis

Reputation: 17350

  1. IfIsEvil and expensive.
  2. Use Positive Logic instead.

Better to put all static content in a directory.

-static 
 |--images
 |--css
 |--js


server {
  server_name www.foo.com;
  root /your_nginx_root/;

  location / {
    index  index.php;
  }

  location /static {
    add_header Cache_control "public";
    expires max;
  }

  error_page 301 302 400 401 402 403 404 405      /index.php;
  error_page 406 408 409 410 411 412 413 414      /index.php;
  error_page 415 416 495 496 497 500 501 502      /index.php;
  error_page 503 504 507                          /index.php;
}


*EDIT:*Try this

if ($request_uri !~* (images|css|js)) {
    rewrite $ /index.php;
}

Upvotes: 1

Related Questions