我正在重构一个非常老的应用程序,并且被困在Nginx配置上;我所有的尝试都导致PHP脚本被下载而不是被执行,或者以“找不到文件”结束]
目录结构
public
│
└─── front
│ │ index.php
│ │
│ │
│ └─── web_www.example.com
│ │ index.php
│ │ style.css
│ │ export/
...
目标
除了静态文件(.js
,.css
...]之外的所有URL,都必须路由到public/front/index.php
示例:
public/front/index.php
public/front/index.php
public/front/index.php
public/front/index.php
serve the file
为什么某些URL看起来像是要执行的php脚本?因为它是一个旧的应用程序,所以可以这样工作;现在,只有1个前端控制器(public/front/index.php
)负责执行代码
Base,不起作用的虚拟主机
server {
...
root /srv/app/public/front;
index index.php;
location / {
try_files $uri $uri/ /index.php$is_args$args =404;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php-fpm.app.sock;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~* \.(js|css|bmp|png|jpg|jpeg|gif|swf|ico)$ {
try_files $uri =404;
}
rewrite ^/(.*)$ /web_$http_host$uri break;
}
我在做什么错/想念吗?
编辑1
将尝试文件添加到该位置,如@Florentin Stemate的建议:
location ~ \.php$ {
try_files /index.php?$args $uri;
....
}
((我切换了顺序并将$uri
放置在末尾,因此当URL包含.php
时,我们总是尝试通过前端控制器)
这样做对所有页面都有效,除了:
https://www.example.com/product-1.htm
=>现在给出:* 2重写或内部重定向周期,而内部重定向到“ /web_www.test-boutique.vm/web_www.test-boutique.vm/web_www.test-boutique.vm/web_www.test-boutique.vm/web_www.test-boutique.vm / .....”
try_files
语句中有错误。删除=404
,使/index.php$is_args$args
为last参数。
例如:
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
try_files
语句的最后一个参数可以是URI,状态码或命名位置。选一个!有关详细信息,请参见this document。