1

我有一个非常简单的.htaccess文件,我需要将其转换为 nginx 配置:

RewriteRule ^api$ api.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]

我试着做一个开始:

location / {
    rewrite ^(.*)$ /index.php;
}

但是每当我加载网页的基本 URL 时,我的浏览器只会下载 index.php 文件。我的配置中也有这个:

location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
}

但由于某种原因,nginx 仍然将其作为下载传递。这里的正确配置是什么?

4

1 回答 1

1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]

前 3 个您将使用 try_files 而不是在 nginx 中重写(有关该指令的详细信息,请参见http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files ):

location / {
  try_files $uri $uri/ $uri/index.php$args;
}

location = /api {
  rewrite ^ /api.php$args
}
于 2012-08-31T09:29:42.253 回答