繁体   English   中英

NGINX配置正则表达式问题

[英]NGINX config regex issue

我在这里迷路了。 在我的nginx conf文件中,我想要一个规则来声明:

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

这是我得到的,但是不起作用:

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

我认为正则表达式不匹配吗?

编辑:这是解决方案

if ($request_uri !~* (images|css|js)) {
    rewrite $ /index.php;
}
  1. 如果是邪恶而昂贵的。
  2. 请改用正逻辑。

最好将所有静态内容放在目录中。

-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;
}


* 编辑: *试试这个

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM