当前位置: 动力学知识库 > 问答 > 编程问答 >

.htaccess - Rewrite all requests for non existing files to index.php with try_files with Nginx

问题描述:

I am trying to convert trivial htaccess file to Nginx and can't make it work. It returns 404 error.

Here is htaccess content:

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php [L]

Here is my current nginx config:

server {

listen 80;

server_name domain.biz;

root /var/www/domain.biz;

charset utf-8;

autoindex off;

location / {

try_files $uri $uri/ /index.php?$args;

}

location ~ \.php$ {

include fastcgi_params;

fastcgi_pass unix:/var/run/php5-fpm.sock;

fastcgi_param SCRIPT_FILENAME /var/www/domain.biz$fastcgi_script_name;

}

}

网友答案:
  1. How about other php files you call directly? For example an info.php with just a

    phpinfo();

    inside?

    I ask this because your server conf seems to be using try_files just right, but I'm not sure you're serving php scripts right.

  2. ¿Is your fastcgi pool listening on that sock? ¿Are you sure it isn't listening in port 9000 for example? In any case, I prefer to define an upstream in the http section and use it later in the server section

    http {
    
        upstream phpbackend {
            server unix:/var/run/php5-fpm.sock;
        }
        ...
    
    
        server {
            ...
            location ~ \.php$ {
                include       fastcgi_params;
                fastcgi_pass  phpbackend;
                fastcgi_param SCRIPT_FILENAME /var/www/domain.biz$fastcgi_script_name;
            }    
        }
    }
    
  3. Are you sure your php.ini has the cgi.fix_pathinfo set to false?

分享给朋友:
您可能感兴趣的文章:
随机阅读: