使用代理时Nginx 502 Bad Gateway错误 [英] Nginx 502 Bad Gateway error when using proxy
问题描述
我有一个Angular构建和一个Laravel后端,提供了在一台服务器上运行的API.我已经在nginx中配置了它们,并且前端具有到后端服务器的代理.
i have a Angular build and an Laravel backend providing API's running on one server. I've configured them in nginx with the frontend having a proxy to the backend server.
后端在url上运行(例如,占位符) http://api.example.com 并且前端在 http://example.com
The backend is running on the url (example is placeholder) http://api.example.com and the frontend is running on 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错误.
Now when I do any api call from the frontend I get the a 502 Bad Gateway error from nginx.
来自nginx错误日志:
From nginx error log:
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"
有什么主意我可以解决这个问题吗?
Any idea how I can fix this?
推荐答案
我相信您的问题是主机名配置创建了一个递归循环,在该循环中,单个请求被代理回前端,从而迅速耗尽所有工作人员.您可以通过向前端发出单个请求来识别此问题,从而在访问日志中生成许多条目.
I believe your issue is hostname configuration creating a recursive loop in which a single request is proxied back to the front-end quickly exhausting all workers. You'll recognize this by a single request to the frontend generating many entries in the access log.
我能够使用您提供的配置快速重新创建该错误.下面是一个修改后的版本,它消除了配置在后端服务器上提供2个不同的静态文件的配置,以说明所需的最低配置.如果可行,则可以重新添加cgi_pass配置.
I was able to quickly recreate that error using the config you provided. Below is a modified version that eliminates config serving up 2 different static files on backend server to illustrate the minimum config required. If this works, you can add the cgi_pass config back in.
#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;
}
}
这篇关于使用代理时Nginx 502 Bad Gateway错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!