【问题标题】:nginx configuration for a nodejs app and laravel app together一起为 nodejs 应用程序和 laravel 应用程序配置 nginx
【发布时间】:2019-02-11 16:26:07
【问题描述】:

所以我的服务器上有一个包含 2 个目录的 Web 应用程序项目: 1)前端 - 我从 pm2 开始的 nodejs 应用程序(next.js)。 2) api - laravel 后端 api

我也安装了 nginx 并尝试将此作为我的服务器设置:

server {
    location / {
        proxy_pass http://localhost:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }

    listen 80;
    listen [::]:80;
    root /project/api/public;
    index index.php index.html index.htm;

    location / {
        try_files $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass             unix:/var/run/php/php7.2-fpm.sock;
        fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

现在,当我访问我的网址时,前端部分可以工作,所以当我访问 example.com 时,我会看到我的网络应用程序(因此端口 3000 的代理配置工作)。

无论后端/api 不起作用,我认为是因为当我执行后端任务时它不会重定向到 api。像 http://example.com:3000/api/auth/login/ 应该转到我的 laravel 应用程序。

"/api/..." 是后端端点。

我想我需要以某种方式定义一个位置 /api {} 才能转到 laravel 应用程序。

我们将不胜感激。

【问题讨论】:

    标签: laravel nginx next.js


    【解决方案1】:

    您有 2 个“位置 /”定义...一个正在破坏另一个。

    尝试将以下内容用于您的第二个定义:

    location /api {
        try_files $uri/ /index.php?$query_string;
    }
    

    【讨论】:

    • 为此我得到 504 网关超时。你知道我的其他设置是否适合 laravel?
    • 如果不了解您的服务器配置,很难说。您运行的是 Red Hat 还是 Debian 变体?我可以尝试帮助您完成它。你能在你的 /var/log/nginx/error.log 文件中提供最后的堆栈跟踪吗?
    • 我还建议将这两行添加到location / 部分的底部...目前,您的代理设置无法正确转发错误。这两行应该有助于修复它:php proxy_next_upstream error timeout http_500 http_502 http_503 http_504 http_404; proxy_intercept_errors on;
    • 我正在运行 Ubuntu。这是错误日志:2019/02/11 18:31:48 [emerg] 17991#17991:/etc/nginx/sites-enabled/default:9 中的未知指令“php”。 504 错误是在对localhost:3000/api/auth/login 的请求之后出现的,所以我猜它没有正确代理到 api?
    • 根本不要使用“localhost:3000”...您的目标是让 nginx 在端口 80 上工作。localhost 应该显示与 localhost:3000 相同的内容...如果它不,您的 nginx 设置有其他问题。要正确回答您关于代理的问题,它根本不是代理...您根本没有访问 nginx...您是直接访问 Node 的 Web 服务器。
    猜你喜欢
    • 2017-01-17
    • 2016-11-16
    • 2018-01-28
    • 2021-05-10
    • 2019-03-26
    • 2019-05-17
    • 1970-01-01
    • 1970-01-01
    • 2017-12-13
    相关资源
    最近更新 更多