Nginx代理传递真实IP不起作用 [英] Nginx proxy pass real ip doesn't work

查看:54
本文介绍了Nginx代理传递真实IP不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在docker容器中运行了一个nginx反向代理服务器.后端是运行在主机上的apache服务器,正在监听 10082 端口. Laravel 处理请求.我用

I run a nginx reverse proxy server in the docker container. And the backend is a apache server running on the host, which is listening 10082 port. Laravel handle the request. And I use

$ request-> getClientIp()

获取真实IP.但是,直接通过 http://myip:10082 访问服务器而没有代理的结果与通过反向代理访问服务器的结果相冲突.

to get real ip. However, the result of visiting the server by http://myip:10082 directly without proxy conflicts with the result of visiting it by reverse proxy.

laravel中的测试代码:

The test code in laravel:

echo $request->ip().'<br>';
echo $request->headers->get('X-Real-IP').'<br>';
echo $request->getClientIp().'<br>';

使用代理的结果:

192.168.80.2
218.205.17.167
192.168.80.2

没有代理的结果(XX.XXX.236.29是我的真实IP):

The result without proxy(the XX.XXX.236.29 is my real ip):

XX.XXX.236.29

XX.XXX.236.29

nginx的配置:

server { 
    listen       80; 
    server_name  myserver.com; 

    access_log  logs/myserver.access.log  main;


    location / { 
        proxy_pass http://myip:10082;
        proxy_set_header   Host    $host; 
        proxy_set_header   X-Real-IP   $remote_addr; 
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for; 
    }

}

我很困惑.有人可以帮我解决.谢谢!

I'm so confused. Could someone help me solve it. Thanks!

推荐答案

由于您已经在nginx代理中设置了 X-Real-IP 标头,因此它才是真正的ip.

Since you have set the X-Real-IP header in your nginx proxy, it's the real ip.

这篇关于Nginx代理传递真实IP不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆