nginx重写 - 文件存在但被重写

时间:2011-05-04 09:50:39

标签: url-rewriting nginx rewrite

我在/path/

下有一个php应用程序

不存在的所有内容(文件/目录)需要重定向到/path/index.php

if (!-e $request_filename)
{
  rewrite ^/path/(.+)$ /path/index.php last;
}

并且所有内容都与真实文件不同,出于某种原因。现有的.css文件仍然会重定向到索引...例如/path/CSS/style.css

更新

使用rewrite ^/path/(.+)/$ /path/index.php last;修复,因为我需要重写的所有网址都以尾部斜杠结尾,但仍然令人困惑

nginx.conf

http {
 include       /etc/nginx/mime.types;  
 access_log  /var/log/nginx/access.log;

 sendfile        on;

 keepalive_timeout  65;
 tcp_nodelay        on;

 gzip  on;
 gzip_disable "MSIE [1-6]\.(?!.*SV1)";

 include /etc/nginx/conf.d/*.conf;
 include /etc/nginx/sites-enabled/*;
}

完整网站启用/文件

server {
  listen  valueyourvote.org.nz:80;
  server_name  valueyourvote.org.nz valueyourvote.co.nz;

  if ($http_host != www.valueyourvote.org.nz) {
      rewrite  (.*)  http://www.valueyourvote.org.nz$1;
  }
  access_log  /var/log/nginx/valueyourvote.org.nz.access.log;
  error_log   /var/log/nginx/valueyourvote.org.nz.error.log;
  location / {
    root   /var/www/vote.incode.co.nz/;
    index  index.php index.html index.htm;
  }

  if (!-e $request_filename)
  {
    rewrite ^/supercity-2010/(.+)/$ /supercity-2010/index.php last;
  }


  # Pass all .php files onto a php-fpm/php-fcgi server.
  location ~ \.php$ {
    fastcgi_pass   localhost:9000;
    fastcgi_index  index.php;

    include fastcgi_params;
  }

  # deny access to .htaccess files, if Apache's document root
  # concurs with nginx's one
  location ~ /\.ht {
     deny  all;
  }
}   

2 个答案:

答案 0 :(得分:2)

你应该尽可能避免在nginx中使用“if” - 请参阅nginx Wiki上的"If is evil"。对于您的使用,更好的使用指令是try_files

对于不起作用的指令,您正在测试“root”指令范围之外的文件 - 即nginx不知道在哪里查看这些URI是否是文件!

在您的位置块中放置一个try_files指令,该指令应该有效。

答案 1 :(得分:0)

你可以这样做

location [your_location]{
   try_files $uri $uri/ @w;
 }
location @w {
rewrite ^/path/(.+)$ /path/index.php last;
}