【发布时间】:2021-09-25 09:29:05
【问题描述】:
我正在学习使用 nginx 在 docker compose 中连接服务器和应用程序。在应用程序中,我试图将数据发布到数据库并发送5 requests a time。 nginx 似乎对此不满意,然后我得到了502 错误:POST http://localhost/api/somerequest 502 (Bad Gateway)。如果我在1 request a time 使用较低的频率,它会起作用。
问题是是否有可能提高 nginx 性能以允许它处理多个大量请求,例如在5 request frequency。配置中有什么设置可以入手吗?
当前配置文件:
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
include conf.d/events.conf;
include conf.d/http.conf;
events {
worker_connections 1024;
}
http {
upstream server {
server ${SERVER_HOST}:${SERVER_PORT}; # env variable from container
keepalive 15;
}
upstream client {
server ${CLIENT_HOST}:${CLIENT_PORT}; # env variable from container
keepalive 15;
}
server {
listen 80;
server_name myservice; # my service container name in docker-compose.yml
error_log /var/log/nginx/myservice.error.log;
access_log /var/log/nginx/myservice.access.log;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $http_host;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Connection "Keep-Alive";
proxy_set_header Proxy-Connection "Keep-Alive";
proxy_pass http://client;
}
location /api {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $http_host;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Connection "Keep-Alive";
proxy_set_header Proxy-Connection "Keep-Alive";
proxy_pass http://server/api;
}
}
}
注意:nginx 容器中只有一个核心。我尝试将连接工作人员增加到4000,但它仍然不起作用。
提前谢谢你。
【问题讨论】:
-
Nginx 通常非常高效。如果您收到 HTTP 502 Bad Gateway 错误,则可能是后端服务没有足够快地产生响应,并且 Nginx 代理已决定它不可用。
-
好的,谢谢您的信息。假设后端是固定的,是否可以让 nginx 等待响应避免中断?
标签: docker nginx docker-compose nginx-reverse-proxy nginx-config