nginx - 在连接到上游客户端时负载平衡没有实时上游
问题描述
我在本地网络上设置了一个带有两个 docker apache 实例的负载均衡器。我确认的两个节点都已启动,并且可以在单个端口 8081 和 8082 上查看,但是当尝试在 apache 中使用上游时,我得到 502 bad gateway 错误。
我尝试将 /etc/nginx 目录的权限更改为 nginx 用户。我尝试重新启动 apache 实例和 nginx 服务。当我将 server_name 更改为 localhost 而不是服务器主机的 ip 地址时,我可以从本地网络上的另一台机器上看到我的 Web 浏览器上的默认 nginx 服务器页面。如果我将它改回 IP 地址,我会收到错误的网关错误。
我将我的 default.conf 符号链接到可用站点
[root@www nginx]# vi sites-enabled/default.conf
upstream containerapp {
server 192.168.0.42:8081;
server 192.168.0.42:8082;
}
server {
listen *:80;
server_name localhost;
index index.html index.htm index.php;
access_log /var/log/nginx/localweb.log;
error_log /var/log/nginx/localerr.log;
location /{
proxy_pass http://containerapp;
}
}
这是我得到的日志文件错误
[root@www nginx]# cat /var/log/nginx/localerr.log
referrer: "http://192.168.0.42/"
2019/01/04 20:15:54 [error] 16310#0: *1 no live upstreams while
connecting
to upstream, client: 192.168.0.18, server: 192.168.0.42, request:
"GET / HTTP/1.1", upstream: "http://containerapp/",
host:"192.168.0.42"
2019/01/04 20:15:54 [error] 16310#0: *1 no live upstreams while
connecting
to upstream, client: 192.168.0.18, server: 192.168.0.42, request:
"GET
/favicon.ico HTTP/1.1", upstream: "http://containerapp/favicon.ico",
host:
"192.168.0.42", referrer: "http://192.168.0.42/"
这就是我的 nginx.conf 的样子
nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local]
"$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d
directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
#include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
#include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
include /etc/nginx/sites-enabled/*;
}
我可以点击http://192.168.0.42它会直接循环到http://192.168.0.42:8081或http://192.168.0.42:8082节点
解决方案
我发现解决方案与 selinux 相关。我用这个命令在这里找到了另一个帖子
setsebool -P httpd_can_network_connect 1
链接到原始问题,以防将来有人在连接上游时遇到此问题(13:权限被拒绝):[nginx]
推荐阅读
- batch-processing - OrientDB:有关 RecordLinks 的信息
- ios - Swift map 函数可变性
- python - 如何测试 pytest 夹具是否引发异常?
- python - 在 Pandas DF 中取消虚拟变量的最有效方法
- javascript - 反应 + 电子应用程序问题 > 无法加载资源:net::err_file_not_found bg.jpg
- python - 匀称:沿边缘在任意点分割 LineString
- angular - Sinon 不模拟带有参数的 GET 提供的 URL
- python - set_index() 之后的 Pandas KeyError
- c# - 从 OnActionExecuting 访问方法的属性
- java - Euler Project - 计算出以下 100 个 50 位数字之和的前十位数字