我正在尝试使用 api 制作一个 angular 应用程序。除了 API 路由之外,每个路由都应该转到 angular 应用程序。API 路由应该转到 laravel 项目。但是 API 路由(/api
)不起作用,它只是重定向到 angular 应用程序。当我删除 angular 应用程序路由(/
)时,我能够在 处收到错误/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.
这是 curl 内容( /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>
这是 curl 的内容/api/index.php
:
File not found.
答案1
经过大量搜索我终于找到了答案:
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;
try_files $uri $uri/ @laravel;
index index.php;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /home/example/public_api/public/index.php;
}
}
location @laravel {
rewrite /api/(.*)$ /api/index.php?/$1 last;
}
location / {
index index.html;
try_files $uri$args $uri$args/ /index.html;
}
}