配置 Nginx: 一个域中的两个应用程序

我有两个应用程序:界面 HTML + JS 和后端 PHP. 我想配置 Nginx 因此,它们都是一个域的服务。 使用URL开始执行返回请求

/api

.

我的尝试是这样的:

server {
root /path/to/frontend;
index index.html;
server_name example.com;

location / {
try_files $uri $uri/ /index.html;
}

location /api {
alias /path/to/backend;
index index.php;
try_files $uri $uri/ /index.php;
}

location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
}

但是,对于我得到的后端的所有请求 404: 主要场景未知。

有没有办法实现我想在这里做的事情? 如何?
已邀请:

卫东

赞同来自:

当人们不明白它是如何工作的时候,你犯了一个普通的错误 nginx. 记住以下内容:

nginx 始终提供请求

单身的


location

只有块

.

我建议你 (回覆) 阅读以下:
http://nginx.org/en/docs/http/ ... .html
现在,查看您的配置,必须由两个位置服务的服务器请求:


location /api


location ~\.php$

下列的
http://nginx.org/en/docs/http/ ... ation
文档,其中的第一个被称为

字首

地点和第二个 -

正则表达式

(正则表达式) 一。 nginx 检查两者,但最终只会选择一个,在您的情况下是正则表达式。

现在处理时 nginx 将请求重定向

/path/to/frontend/<yourfile>.php

在 PHP, 出行

root /path/to/frontend

, 因为这是唯一明确的。 然后后端找不到指定的文件。

您可以尝试以下操作:

location /api {
alias /path/to/backend;
index index.php;
try_files $uri $uri/ /index.php;

location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;

# Useless? Request always ending up with '.php' here...
fastcgi_index index.php;

include fastcgi_params;

# Try to secure this block, which might lead to arbitrary code execution.
}
}

至于缺乏安全性,我进行了一份报告 nginx 和 PHP-FPM 在 nginx.conf 2014 10月底。 幻灯片可用:
https://rosset.net/LAMP_just_died.pptx
. 视频将很快提供。
</yourfile>

要回复问题请先登录注册