小编Bob*_*Bob的帖子

静态和 PHP 文件的 NGINX 配置

我正在尝试配置 nginx 来提供静态文件和 PHP 文件。我的配置不起作用。我想要以下本地文件夹结构:

src/static/  -> contains HTML, CSS, JS, images etc
src/api/     -> contains PHP files for a small REST service
Run Code Online (Sandbox Code Playgroud)

如果我访问http://mysite.local我希望获得 /static 文件夹中的文件。如果我访问http://mysite.local/api我希望获得 API PHP 文件。我希望重写对 api 的请求并将其发送到 index.php 文件。

一些例子:

http://mysite.local/test.html                   -> served from src/static/test.html
http://mysite.local/images/something.png        -> served from src/static/images/something.png
http://mysite.local/css/style.css               -> served from src/static/css/style.css

http://mysite.local/api/users                   -> served from src/api/index.php?users
http://mysite.local/api/users/bob               -> served from src/api/index.php?users/bob
http://mysite.local/api/biscuits/chocolate/10   -> served from src/api/index.php?biscuits/chocolate/10
Run Code Online (Sandbox Code Playgroud)

以下配置适用于静态文件,但不适用于 api 文件。如果我访问其中一个 API 路径,则会收到 404 错误。

server {
    listen       80;
    server_name …
Run Code Online (Sandbox Code Playgroud)

html php nginx

5
推荐指数
1
解决办法
5469
查看次数

标签 统计

html ×1

nginx ×1

php ×1