我试图用 api 制作一个角度应用程序。除 API 路由外,每条路由都应该转到 Angular 应用程序。API 路由应该去一个 laravel 项目。但是 API route( /api
) 不起作用,它只是重定向到 Angular 应用程序。当我删除 Angular 应用程序 route( /
) 时,我会在/api
.
这是我的配置文件:
server {
listen 80;
server_name example.nl *.example.nl;
access_log /home/example/logs/access.log;
error_log /home/example/logs/error.log;
root /home/example/public_html;
location /api {
root /home/example/public_api/public;
index index.php;
try_files $uri $uri/ /index.php?$query_string;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
}
location / {
index index.html;
try_files $uri$args $uri$args/ /index.html;
}
}
当我删除最后一个块时,我得到了一个错误:
open() "/usr/share/nginx/html/index.php" failed (2: No such file or directory)
但我是说root /home/example/public_api/public;
。为什么它在采摘/usr/share/nginx/html
?
/usr/share/nginx/html
是我的default
路线。
更新
Nginx 现在正在读取正确的目录,但是 PHP 坏了,这是我更新的配置文件:
server {
listen 80;
server_name example.nl *.example.nl;
access_log /home/example/logs/access.log;
error_log /home/example/logs/error.log;
root /home/example/public_html;
location /api {
alias /home/example/public_api/public;
index index.php;
try_files $uri $uri/ /index.php?$query_string;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
}
location / {
index index.html;
try_files $uri$args $uri$args/ /index.html;
}
}
在我得到的错误日志中:
FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream
这是我在浏览器中得到的内容:
File not found.
这是卷曲内容(/api
):
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.10.0 (Ubuntu)</center>
</body>
</html>
这是卷曲内容/api/index.php
:
File not found.
经过大量搜索,我终于找到了答案: