Nginx 502使用代理时出现Bad Gateway错误

2q5ifsrm  于 2022-11-02  发布在  Nginx
关注(0)|答案(3)|浏览(375)

我有一个Angular版本和一个Laravel后端提供的API运行在一个服务器上。我已经在nginx中配置了它们,前端有一个到后端服务器的代理。
后端在url(示例为占位符)http://api.example.com上运行,前端在http://example.com上运行
前端配置:

server {
    listen       80;
    server_name  example.com;

    location /api {
        proxy_pass http://api.example.com;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
    }

    location / {
        root  /var/www/angular/em-frontend/dist;
        index  index.html index.htm;
        try_files $uri $uri/ /index.html$is_args$args;
    }
}

后端配置:

server {
        listen 80;
        server_name api.example.com;

        root /var/www/angular/em-backend/public;

        index index.php index.html index.htm;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ /index.php?$query_string;
                # Uncomment to enable naxsi on this location
                # include /etc/nginx/naxsi.rules
        }
        location ~ \.php$ {
                try_files $uri =404;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }
}

现在,当我从前端进行任何API调用时,我会从nginx得到一个502坏网关错误。
从nginx错误日志:

2017/12/09 23:30:40 [alert] 5932#5932: 768 worker_connections are not enough
2017/12/09 23:30:40 [error] 5932#5932: *770 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: IP_MASKED, server: example.com, request: "GET /api/endpoint HTTP/1.1", upstream: "http://IP_ADDRESS:80/api/endpoint", host: "example.com", referrer: "http://example.com/dashboard"

你知道我该怎么补救吗?

dy1byipe

dy1byipe1#

您必须在location块中使用proxy-pass,如下例所示:

upstream myproject {
   server ip1 ;
   server ip2 ;
   server ip3 ;

} 
location / { 
   proxy_pass      http://myproject;
}
slwdgvem

slwdgvem2#

我认为您的问题是主机名配置创建了一个递归循环,其中一个请求被代理回前端,很快耗尽了所有工作线程。您可以通过一个对前端的请求在访问日志中生成许多条目来识别这一点。
我能够使用您提供的配置快速重新创建该错误。下面是一个修改后的版本,它消除了在后端服务器上提供2个不同静态文件的配置,以说明所需的最低配置。如果这起作用,您可以重新添加cgi_pass配置。


# set api domain to use alternate port, could also just tack onto proxy_pass.

upstream api.example.com {
    server localhost:8081;
}

# frontend listening on port 8080

server {
    listen       8080;
    server_name  example.com;

    location /api {
        proxy_pass http://api.example.com;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
    }

    location / {
        root  /usr/local/var/www;
        index  index.html index.htm;
        try_files $uri $uri/ /index.html$is_args$args;
    }
}

# backend listening on 8081

server {
        listen 8081;
        server_name api.example.com;

        index index.php index.html index.htm;

        location / {  # will match any url not ending in .php
              root  /usr/local/var/www;
              try_files $uri $uri/ /index.html;
        }
        location ~ \.php { #successfully responds to http://example.com:8080/api/*.php
              root  /usr/local/var/www;
              try_files $uri $uri/ /service.html;
        }
}
31moq8wy

31moq8wy3#

我也遇到过类似的问题,解决它的办法是:

events {
    worker_connections 1024;
}

events块与http块处于同一级别。

相关问题