ubuntu - 使用代理时 Nginx 502 Bad Gateway 错误

标签 ubuntu nginx proxy

我有一个 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 Bad Gateway 错误。

来自 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"

知道如何解决这个问题吗?

最佳答案

你必须像这个例子一样在 location block 中使用 proxy-pass:

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

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

关于ubuntu - 使用代理时 Nginx 502 Bad Gateway 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47734366/

相关文章:

linux - 使用 list 命令时 GDB 不显示源代码

nginx - 玩! 1.2.4 + Nginx 负载均衡器 - 无法让两个实例在 PROD 模式下协同工作

proxy - capistrano 通过代理部署到没有公共(public) IP 的服务器

python - 无法在 ubuntu 中将默认 python 版本设置为 python3

linux - 缺少 127.0.0.1(环回地址)

nginx - 是否可以在 ingress-nginx 上提供来自 s3 的静态文件?

尝试通过代理克隆 Git 连接超时

android - 适用于 Android 的 SOCKet 安全 (SOCKS) 服务器

linux - 有关创建 Ubuntu 软件包的 .sh 脚本的一些信息?

python - Flask Nginx uWSGI 504 网关超时错误