9mlcy7 2024-04-22 18:08 采纳率: 0%
浏览 8
已结题

nginx IP 透传报错 504

更新:定位问题:nginx透传建立连接后,server直接和客户都进行通信,而非nginx
环境 Ubuntu 22
openresty/1.19.9.1

资料:https://www.nginx.com/blog/ip-transparency-direct-server-return-nginx-plus-transparent-proxy/#troubleshooting

client(192.168.0.125)-> openresty(192.168.0.230)-> server(192.168.0.180)

在230设置路由规则

# 路由规则
ip rule add fwmark 921 lookup 920
ip route add local 0.0.0.0/0 dev lo table 920

# 将流量打上标记
iptables -t mangle -A PREROUTING -i enp5s0 -p tcp ! -d 192.168.0.230 -s 192.168.0.180 --sport 80 -j MARK --set-mark 921

nginx.config

#加上
proxy_bind $remote_addr transparent;

nginx user

USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root           1  0.0  0.0  12756  8560 ?        Ss   17:51   0:00 nginx: master process /usr/local/openresty/bin/openresty -g daemon off;
root           7  0.0  0.0  12760  5104 ?        S    17:51   0:00 nginx: worker process
root           8  0.0  0.0  12760  5228 ?        S    17:51   0:00 nginx: worker process
root           9  0.0  0.0  12760  3140 ?        S    17:51   0:00 nginx: worker process
root          10  0.0  0.0  12760  3140 ?        S    17:51   0:00 nginx: worker process
root          11  0.0  0.0  12760  3140 ?        S    17:51   0:00 nginx: worker process
root          12  0.0  0.0  12760  3140 ?        S    17:51   0:00 nginx: worker process
root          13  0.0  0.0  12760  3140 ?        S    17:51   0:00 nginx: worker process
root          14  0.0  0.0  12760  3144 ?        S    17:51   0:00 nginx: worker process
root          21  0.7  0.0   4624  3564 pts/0    Ss   17:59   0:00 /bin/bash
root          33  0.0  0.0   7060  1588 pts/0    R+   17:59   0:00 ps aux

但是访问的时候 504 Gateway Time-out

180抓包日志

tcpdump: data link type LINUX_SLL2
tcpdump: verbose output suppressed, use -v[v]... for full protocol decode
listening on any, link-type LINUX_SLL2 (Linux cooked v2), snapshot length 262144 bytes
09:58:07.503059 ens160 In  IP 192.168.0.125.45932 > 192.168.0.180.9200: Flags [S], seq 3251489155, win 64240, options [mss 1460,sackOK,TS val 2068591 ecr 0,nop,wscale 7], length 0
09:58:07.503237 ens160 Out IP 192.168.0.180.9200 > 192.168.0.125.45932: Flags [S.], seq 2066791693, ack 3251489156, win 65160, options [mss 1460,sackOK,TS val 1857063966 ecr 2068591,nop,wscale 7], length 0
09:58:08.524013 ens160 Out IP 192.168.0.180.9200 > 192.168.0.125.45932: Flags [S.], seq 2066791693, ack 3251489156, win 65160, options [mss 1460,sackOK,TS val 1857064987 ecr 2068591,nop,wscale 7], length 0
09:58:08.529435 ens160 In  IP 192.168.0.125.45932 > 192.168.0.180.9200: Flags [S], seq 3251489155, win 64240, options [mss 1460,sackOK,TS val 2069618 ecr 0,nop,wscale 7], length 0
09:58:08.529509 ens160 Out IP 192.168.0.180.9200 > 192.168.0.125.45932: Flags [S.], seq 2066791693, ack 3251489156, win 65160, options [mss 1460,sackOK,TS val 1857064992 ecr 2068591,nop,wscale 7], length 0
09:58:10.540017 ens160 Out IP 192.168.0.180.9200 > 192.168.0.125.45932: Flags [S.], seq 2066791693, ack 3251489156, win 65160, options [mss 1460,sackOK,TS val 1857067003 ecr 2068591,nop,wscale 7], length 0
09:58:10.545511 ens160 In  IP 192.168.0.125.45932 > 192.168.0.180.9200: Flags [S], seq 3251489155, win 64240, options [mss 1460,sackOK,TS val 2071634 ecr 0,nop,wscale 7], length 0
09:58:10.545573 ens160 Out IP 192.168.0.180.9200 > 192.168.0.125.45932: Flags [S.], seq 2066791693, ack 3251489156, win 65160, options [mss 1460,sackOK,TS val 1857067008 ecr 2068591,nop,wscale 7], length 0
09:58:14.571995 ens160 Out IP 192.168.0.180.9200 > 192.168.0.125.45932: Flags [S.], seq 2066791693, ack 3251489156, win 65160, options [mss 1460,sackOK,TS val 1857071035 ecr 2068591,nop,wscale 7], length 0
09:58:14.769647 ens160 In  IP 192.168.0.125.45932 > 192.168.0.180.9200: Flags [S], seq 3251489155, win 64240, options [mss 1460,sackOK,TS val 2075858 ecr 0,nop,wscale 7], length 0
09:58:14.769740 ens160 Out IP 192.168.0.180.9200 > 192.168.0.125.45932: Flags [S.], seq 2066791693, ack 3251489156, win 65160, options [mss 1460,sackOK,TS val 1857071232 ecr 2068591,nop,wscale 7], length 0
09:58:22.961864 ens160 In  IP 192.168.0.125.45932 > 192.168.0.180.9200: Flags [S], seq 3251489155, win 64240, options [mss 1460,sackOK,TS val 2084050 ecr 0,nop,wscale 7], length 0
09:58:22.961958 ens160 Out IP 192.168.0.180.9200 > 192.168.0.125.45932: Flags [S.], seq 2066791693, ack 3251489156, win 65160, options [mss 1460,sackOK,TS val 1857079425 ecr 2068591,nop,wscale 7], length 0
09:58:31.212024 ens160 Out IP 192.168.0.180.9200 > 192.168.0.125.45932: Flags [S.], seq 2066791693, ack 3251489156, win 65160, options [mss 1460,sackOK,TS val 1857087675 ecr 2068591,nop,wscale 7], length 0
09:58:39.090254 ens160 In  IP 192.168.0.125.45932 > 192.168.0.180.9200: Flags [S], seq 3251489155, win 64240, options [mss 1460,sackOK,TS val 2100178 ecr 0,nop,wscale 7], length 0
09:58:39.090340 ens160 Out IP 192.168.0.180.9200 > 192.168.0.125.45932: Flags [S.], seq 2066791693, ack 3251489156, win 65160, options [mss 1460,sackOK,TS val 1857095553 ecr 2068591,nop,wscale 7], length 0
09:58:55.276026 ens160 Out IP 192.168.0.180.9200 > 192.168.0.125.45932: Flags [S.], seq 2066791693, ack 3251489156, win 65160, options [mss 1460,sackOK,TS val 1857111739 ecr 2068591,nop,wscale 7], length 0

230 openresty 日志(资料说是非root,但是我确实是root)


2024/04/22 18:31:58 [error] 7#7: *1 upstream timed out (110: Connection timed out) while connecting to upstream, client: 192.168.0.125, server: _, request: "GET / HTTP/1.1", upstream: "http://192.168.0.180:9200/", host: "192.168.0.230"
  • 写回答

6条回答 默认 最新

  • 专家-赤兔[在线] 优质创作者: 编程框架技术领域 2024-04-22 18:08
    关注

    引自免费微信小程序:皆我百晓生

    看起来您遇到了一个Nginx作为透明代理时返回504 Gateway Time-out的问题。根据您提供的信息,我会逐步分析可能的原因并提供相应的解决步骤。

    首先,让我们检查一下Nginx配置文件中的proxy_bind指令是否正确设置为$remote_addr transparent;。确保您的Nginx配置文件中有如下配置:

    http {
        ...
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    
        # 确保有以下行
        proxy_bind $remote_addr transparent;
    
        ...
    }
    

    接下来,我们需要确认Nginx运行用户是否有权限执行透明绑定。由于Nginx通常以root用户运行,而透明绑定可能需要更高的权限,您可以尝试将Nginx运行用户切换为nobody或其他非特权用户,并重新启动Nginx。在 /etc/nginx/nginx.conf 中找到 user 行,修改如下:

    user nobody nobody;
    

    然后重启Nginx:

    sudo systemctl restart nginx
    

    为了排除防火墙和网络策略的影响,请暂时禁用防火墙,看看是否能解决504错误。使用以下命令临时关闭ufw防火墙:

    sudo ufw disable
    

    如果您使用的是其他防火墙,例如firewalld,可以使用以下命令停止并禁用它:

    sudo systemctl stop firewalld
    sudo systemctl disable firewalld
    

    现在,检查Nginx日志文件 /var/log/nginx/error.log/var/log/nginx/access.log,看是否有更详细的错误信息。如果有任何与连接超时、错误响应或服务器拒绝连接相关的消息,请记录下来,这可能会指向问题的具体原因。

    另外,确保从客户端到Nginx再到后端服务器的网络路径是通的。使用 traceroute 命令检查客户端(192.168.0.125)到服务器(192.168.0.180)的路径:

    traceroute 192.168.0.180
    

    如果所有节点都能成功到达,且没有跳数过多或时间过长的情况,那么网络基本正常。

    最后,查看后端服务器(192.168.0.180)的日志,确保它没有报告任何错误或异常行为。如果后端服务器运行的是Web服务,比如Apache或Nginx,检查其error_log文件,寻找可能影响请求处理的问题。

    如果上述步骤都无法解决问题,请提供更多关于后端服务器的信息,或者再次审查整个网络配置,包括路由器和交换机的设置,以确定是否存在其他潜在的网络问题。如果还有问题,请提供更多信息以便我能进一步协助您。

    评论 编辑记录

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 5月6日
  • 修改了问题 4月22日
  • 修改了问题 4月22日
  • 修改了问题 4月22日
  • 展开全部