简体   繁体   English

在Nginx中使用Node JS应用程序部署Laravel应用程序

[英]Deploying a laravel app with a node js app in nginx

I have a node.js app running using pm2. 我有一个使用pm2运行的node.js应用程序。 When I go to my endpoint it works, i see things. 当我到达终点时,我看到了东西。 But then when I try to do an action that connects to the backend api at laravel, then nothing happens. 但是,当我尝试执行连接到laravel的后端api的操作时,什么也没发生。

This is how I have my nginx config configured: 这是我配置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;
    }
}

When I try to perform an action that should connect to the backend (like sign in) then it fails to work try the signin function on make.tube ( http://localhost:3000/api/auth/login/ ). 当我尝试执行应连接到后端的操作(例如登录)时,它无法正常工作,请尝试在make.tube( http:// localhost:3000 / api / auth / login / )上登录功能

Does anyone know how I can get this working so when I perform the sign in it connects to my laravel backend? 有谁知道我该如何工作,以便在我执行登录操作时将其连接到我的laravel后端?

By playing around on my local environment I came up with the following simplified version: 通过在本地环境中玩耍,我想到了以下简化版本:

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;
    }
}

I managed to proxy_pass a request like http://test.test to another website http://node.test , which displayed the correct content "I am node." 我设法proxy_pass像一个请求http://test.test到另一个网站http://node.test “我是节点”,这显示正确的内容 Also all request that started with /api like http://test.test/api where directed to the file in /home/vagrant/test/api/index.html which shown "I am api". 同样所有以/api开头的请求(例如http://test.test/api /home/vagrant/test/api/index.html都指向/home/vagrant/test/api/index.html中显示“我是api”的文件。

In my answer on 54775192 I indeed made an error in the location block. 在对54775192的回答中,我确实在位置块中犯了一个错误。 The following: 下列:

location /api {
    root /location/of/laravel;
    index index.php;
}

Will result in an error that nginx tries to get a file named /location/of/laravel/api , which does not exist, instead of the intended /location/of/laravel/api/index.php . 将导致错误,nginx尝试获取名为/location/of/laravel/api ,该文件不存在,而不是预期的/location/of/laravel/api/index.php

Not sure why at this moment. 不知道为什么在这一刻。

This is just how Nginx works. 这就是Nginx的工作方式。 We can make use of alias to prevent this from happening. 我们可以使用别名来防止这种情况的发生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM