502 Bad Gateway - Nginx + Laravel + Nuxt 3

guz6ccqo  于 2023-08-03  发布在  Nginx
关注(0)|答案(1)|浏览(138)

我一直在面对这个问题很长一段时间,我的前端使用Nuxt 3,我的API使用Laravel 9,它们都在同一个服务器上。我的问题是,成功登录后,返回502坏网关,但当我重新加载浏览器,它工作正常。
Nuxt 3 nginx配置

server {
    listen 80;
    server_name maindomain.com;
    
    location / {

        proxy_pass http://127.0.0.1:3000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header  Connection "keep-alive";
        proxy_connect_timeout            300s;
        proxy_send_timeout               300s;
        proxy_read_timeout               300s;
        send_timeout                     300s;
    }

   
}

字符串
Laravel API Nginx配置

server {
    listen 80;
    listen [::]:80;
    server_name api.maindomain.com;
    root /var/www/html/maindomain/backend/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-Content-Type-Options "nosniff";

    index index.php;

    charset utf-8;

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

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php-fpm/www.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }

}


任何建议都非常感谢!

l3zydbqr

l3zydbqr1#

我已经通过更新前端nginx配置解决了我的问题

large_client_header_buffers 4 32k;

 upstream frontend {
    server 127.0.0.1:3000;
    keepalive 8;
 }

server {
    listen 80;
    server_name maindomain.com;
    #root /var/www/html/maindomain;

    location / {

        #try_files $uri $uri/ /index.html;

        #proxy_pass http://127.0.0.1:3000;
        proxy_pass http://frontend;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header  Connection "keep-alive";
        proxy_connect_timeout            300s;
        proxy_send_timeout               300s;
        proxy_read_timeout               300s;
}

字符串
在更改后,我重新启动了AWS示例,它可以正常工作。重新启动我的nginx没有帮助,这就是为什么我重新启动我的示例。

相关问题