问题原文
原帖链接 · Jan Wytze · CC BY-SA 3.0
I a trying to make an angular app with an api. Every route should go to the angular app, except the API route. The API route should go to an laravel project. But the API route(/api) won't work, it just redirects to the angular app. And when I remove the angular app route(/) I am able to get an error at /api.
This is my config file:
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;
}
}
When I removed the last block I was able to get an error:
open() "/usr/share/nginx/html/index.php" failed (2: No such file or directory)
But I am saying root /home/example/public_api/public;.
Why it is picking /usr/share/nginx/html?
/usr/share/nginx/html is my default route.
Update
Nginx is now reading the right directory, but PHP is broken, this is my updated config file:
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;
}
}
In the error log I am getting:
FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream
This is the content I get in the browser:
File not found.
And this is the curl content(/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>
And this is the curl content of /api/index.php:
File not found.
已采纳解答
原帖链接 · Jan Wytze · CC BY-SA 3.0
After a lot of searching I finally found the answer:
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;
}
}
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
