URL重写在Nginx中不起作用,操作系统是Ubuntu 12.4 Lts
当打开http://mvc.loc时,它正在运行 但是当我试图打开http://mvc.loc/login不工作时
404 Not Found
的nginx / 1.1.19
.htaccess
<IfModule !mod_rewrite.c>
ErrorDocument 500 "mod_rewrite must be enabled"
</IfModule>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?u=$1
mvc.loc的虚拟主机
server {
listen 80;
server_name mvc.loc;
access_log /var/log/nginx/mvc.loc.access.log;
error_log /var/log/nginx/mvc.loc.error.log;
root /usr/share/nginx/www/mvc;
index index.php;
# use fastcgi for all php files
# Are you sure you have this set up?
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# deny access to apache .htaccess files
location ~ /\.ht {
deny all;
}
}
答案 0 :(得分:8)
location / {
rewrite ^(.*)$ /index.php?u=$1 last;
}
答案 1 :(得分:2)
嗯@ NanheKumar的回答得到了正确的改写,但它忽略了htaccess中的前两条规则
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
这意味着检查请求是否与文件不匹配且与目录不匹配,以模仿您可以使用try_files
这样的确切行为
location / {
try_files $uri $uri/ /index.php?u=$request_uri;
}
这将确保直接提供指向资产或目录的请求,如果它们都没有将请求传递给index.php
编辑,除非index.php
能够投放资源,否则会导致所有资产(图片,css,javascript等)显示错误,因为index.php
将会收到这是不期待的论点。想想这样的事情http://example.com/index.php?u=/images/background.jpg