使用 Slim 框架的 Nginx - php 个文件正在下载
Nginx with Slim framework - php files getting downloaded
我正在尝试为数据库连接设置一个简单的 REST api,并决定使用 nginx 和 Slim 框架来实现。两者都安装了(通过 Composer 依赖管理器 /home/some_user/slim_project/
在项目目录中本地安装)。
在那之后,我尝试实现 Slim 代码示例并遵循了一些指南,但我所有的尝试都以失败告终。要么下载 php 文件,要么我只是获取 Slim "Page not found"
页面。
该项目 运行 在 raspberry pi 上配置了动态 DNS。
我的结构和代码如下所示:
/home/some_user/slim_project/public/index.php
<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
require '../vendor/autoload.php';
$app = new \Slim\App;
$app->get('/hello/{name}', function (Request $request, Response $response,
array $args) {
$name = $args['name'];
$response->getBody()->write("Hello, $name");
return $response;
});
$app->run();
和 nginx 配置:
/etc/nginx/sites-enabled/default
server {
listen 80 default_server;
listen [::]:80 default_server;
root /home/some_user/slim_test/public/;
server_name mySlimTest1.ddns.net;
location / {
if (!-e $request_filename){
rewrite ^(.*)$ /index.php break;
}
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
}
}
无论我尝试什么,所发生的只是下载 php 文件,或者在某些情况下,当我将示例性的“/hello/Hans”添加到服务器 url.
例如,我遵循了本教程:https://www.slimframework.com/docs/v3/tutorial/first-app.html
我显然在这里遗漏了一些东西。任何帮助将不胜感激。
您的 rewrite...break
导致在同一位置处理新的 URI,当从不同的 location
内部重定向到 .php
URI 时,您应该使用 rewrite...last
堵塞。有关详细信息,请参阅 this document。
但是,您的 if
块执行与 try_files
类似的功能。可以通过以下方式实现相同的功能:
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
我正在尝试为数据库连接设置一个简单的 REST api,并决定使用 nginx 和 Slim 框架来实现。两者都安装了(通过 Composer 依赖管理器 /home/some_user/slim_project/
在项目目录中本地安装)。
在那之后,我尝试实现 Slim 代码示例并遵循了一些指南,但我所有的尝试都以失败告终。要么下载 php 文件,要么我只是获取 Slim "Page not found"
页面。
该项目 运行 在 raspberry pi 上配置了动态 DNS。
我的结构和代码如下所示:
/home/some_user/slim_project/public/index.php
<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
require '../vendor/autoload.php';
$app = new \Slim\App;
$app->get('/hello/{name}', function (Request $request, Response $response,
array $args) {
$name = $args['name'];
$response->getBody()->write("Hello, $name");
return $response;
});
$app->run();
和 nginx 配置:
/etc/nginx/sites-enabled/default
server {
listen 80 default_server;
listen [::]:80 default_server;
root /home/some_user/slim_test/public/;
server_name mySlimTest1.ddns.net;
location / {
if (!-e $request_filename){
rewrite ^(.*)$ /index.php break;
}
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
}
}
无论我尝试什么,所发生的只是下载 php 文件,或者在某些情况下,当我将示例性的“/hello/Hans”添加到服务器 url.
例如,我遵循了本教程:https://www.slimframework.com/docs/v3/tutorial/first-app.html
我显然在这里遗漏了一些东西。任何帮助将不胜感激。
您的 rewrite...break
导致在同一位置处理新的 URI,当从不同的 location
内部重定向到 .php
URI 时,您应该使用 rewrite...last
堵塞。有关详细信息,请参阅 this document。
但是,您的 if
块执行与 try_files
类似的功能。可以通过以下方式实现相同的功能:
location / {
try_files $uri $uri/ /index.php$is_args$args;
}