我在 Windows 服务器上配置了 nginx + php。我使用带有 URL 重写的 MVC 框架,问题是我的服务器无法解析地址。很简单,这是我的代码:
server {
listen 127.0.0.1:80;
server_name localhost/kuimbi;
root www/kuimbi;
index index.php;
location / {
if (!-e $request_filename){
rewrite ^(.+)$ /index.php?url=$1 break;
}
}
location ~* ^.+\.(jpg|jpeg|gif|png|ico|swf|flv|woff)$ {
access_log off;
expires 9d;
}
location ~* ^.+\.(css|js)$ {
access_log off;
expires 1d;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9100;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
error_log logs/error.log;
error_log logs/error.log notice;
error_log logs/error.log info;
access_log logs/access.log main;
}
在 Apache 中我有等效的代码:
Options -MultiViews
RewriteEngine On
Options -Indexes
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
答案1
假设您的请求看起来像http://localhost/kuimbi/*
,您配置错误server_name
。
它不能包含 URI,只能包含域名。
因此你的配置应如下所示:
server {
listen 127.0.0.1:80;
server_name localhost;
root www/kuimbi;
location ~ ^/kuimbi/(.*)$ {
try_files /www/kuimbi/$1 /index.php?url=/$1;
}
...
}