我想将一个PHP应用程序从Apache迁移到Nginx。 问题是,应用程序中断,因为路由不再工作,我不知道如何解决它。
PHP应用程序包含一些.htaccess文件,我试图将它们转换为Nginx。
第一个是在文档根目录下:
<IfModule mod_rewrite.c> RewriteEngine on RewriteRule ^$ public/ [L] RewriteRule (.*) public/$1 [L] </IfModule>
第二个是在/ public /
<IfModule mod_rewrite.c RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d # Rewrite all other URLs to index.php/URL RewriteRule ^(.*)$ index.php?url=$1 [PT,L] </IfModule> <IfModule !mod_rewrite.c> ErrorDocument 404 index.php </IfModule>
第三个也是最后一个是:
deny from all
我的nginx版本如下所示:
#user nobody;
worker_processes 1;
#pid logs / nginx.pid;
事件{
worker_connections 1024;
}
http {
包括mime.types;
default_type application / octet-stream;
发送文件;
#tcp_nopush在;
keepalive_timeout 65;
gzip on;
服务器{
听8080;
server_name localhost;
root / Library / WebServer / Documents / admin;
位置 / {
index index.php;
重写^ / $ / public / break;
重写^(。*)$ / public / $ 1 break;
}
位置/公共{
如果(!-e $ request_filename){
重写^(。*)$ /index.php?url=$1 break;
}
}
位置/图书馆{
否认一切;
}
#error_page 404 /404.html;
#将服务器错误页面redirect到静态页面/50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
根html;
}
位置〜\ .php $ {
root / Library / WebServer / Documents / admin;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $ document_root $ fastcgi_script_name;
包括fastcgi_params;
}
}
}
我面对的问题是,路由被中断,只是返回一个404页面。 希望有人有一个想法,并知道如何解决它;)
谢谢
编辑
我得到它与这个configuration工作
位置/图书馆{
否认一切;
}
位置〜* ^。+。(jpg | jpeg | gif | png | ico | css | zip | tgz | gz | rar | bz2 | doc | xls | exe | pdf | ppt | txt | tar | mid | midi | wav | bmp | rtf | js)$ {
access_logclosures;
重写^(。*)$ / public / $ 1 break;
}
位置 / {
重写^ /(。+)$ /index.php?url=$1 last;
}
我相信有更好的解决scheme,我愿意提出build议。
看起来好像你有一个路由循环。
您正在将来自/来自/public所有stream量以及来自/public所有stream量/index.php
基于/public/.htaccess,所有/ public的请求应该被路由到/public/index.php?url=$1而不是/index.php?url=$1
location /public { if (!-e $request_filename){ rewrite ^(.*)$ /public/index.php?url=$1 break; } }