我有一个使用pm2运行的node.js应用程序。当我到达终点时,我看到了东西。但是,当我尝试执行连接到laravel的后端api的操作时,什么也没发生。
这是我配置Nginx配置的方式:
server {
listen 80;
server_name make.tube;
root /maketube/api/public;
# This is the "last resort" nginx will direct to when no other matches with location have been found.
# This will proxy pass to another website (should be tested with http://localhost:3000 in your case).
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
# All requests that start with /api are directed to the laravel location.
# It will only look for a file named index.html
location /api {
index index.html;
}
}
当我尝试执行应连接到后端的操作(例如登录)时,则无法正常工作,请尝试在make.tube(http://localhost:3000/api/auth/login/)上登录功能。
有人知道我如何使它工作,以便当我执行登录时将其连接到我的laravel后端吗?
答案 0 :(得分:0)
通过在本地环境中玩耍,我想到了以下简化版本:
server {
listen 80;
server_name test.test;
root /home/vagrant/test;
# This is the "last resort" nginx will direct to when no other matches with location have been found.
# This will proxy pass to another website (should be tested with http://localhost:3000 in your case).
location / {
proxy_pass http://node.test/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
# All requests that start with /api are directed to the laravel location.
# It will only look for a file named index.html
location /api {
index index.html;
}
}
我设法proxy_pass
向另一个网站http://test.test
发出了类似http://node.test
的请求,该网站显示了正确的内容“我是节点”。同样,所有以/api
开头的请求(例如http://test.test/api
)都指向/home/vagrant/test/api/index.html
中显示“我是api”的文件。
在对54775192的回答中,我的确在位置块中犯了一个错误。以下:
location /api {
root /location/of/laravel;
index index.php;
}
将导致错误,nginx尝试获取名为/location/of/laravel/api
的文件,该文件不存在,而不是预期的/location/of/laravel/api/index.php
。
现在不确定为什么。
这就是Nginx的工作方式。我们可以利用alias来防止这种情况的发生。