无法从nginx / varnish传递真实IP,始终显示127.0.0.1

时间:2019-06-10 23:02:08

标签: varnish nginx-reverse-proxy real-ip

我想在服务器/ magento中显示真实IP,但它会一直回落到127.0.0.1

服务器设置如下: -清漆在端口80上监听 -Nginx监听端口8080 -SSL流量传递到端口80 最终访问的网站是magento 2网站

Nginx是1.14.0版,带有-http_realip_module

我的Nginx服务器块如下:

upstream fastcgi_backend {
  server   unix:/var/run/php/php7.2-fpm-magento.sock;
}

server {
    listen 8080;
    server_name example.example.com;

    set $MAGE_ROOT /opt/magento/public_html;
    set $MAGE_MODE developer; # or production

    include snippets/letsencrypt.conf;
    include /opt/magento/public_html/nginx.conf.sample;
    error_log /var/log/nginx/example.example.com-8080-error.log;
    access_log /var/log/nginx/example.example.com-8080access.log;
}

server {
    listen 443 ssl http2;
    server_name example.example.com;

    ssl_certificate /etc/letsencrypt/live/example.example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.example.com/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/example.example.com/chain.pem;
    include snippets/ssl.conf;
    include snippets/letsencrypt.conf;

    location / {
        proxy_pass http://localhost:80;
        proxy_set_header Host $http_host;
        proxy_set_header X-Forwarded-Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto https;
        proxy_set_header X-Forwarded-Port 443;
    }
    access_log /var/log/nginx/example.example.com-443access.log;
    error_log /var/log/nginx/example.example.com-443error.log;
}

我的Nginx访问日志(8080access.log)显示所有条目的127.0.0.1 错误日志(8080-error.log)也为客户端显示127.0.0.1 SSL流量443日志显示了实际IP。

以下是我的Nginx配置输出:

configure arguments: --with-cc-opt='-g -O2 -fdebug-prefix-map=/build/nginx-FIJPpj/nginx-1.14.0=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -fPIC' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --modules-path=/usr/lib/nginx/modules --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_v2_module --with-http_dav_module --with-http_slice_module --with-threads --with-http_addition_module --with-http_geoip_module=dynamic --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_xslt_module=dynamic --with-stream=dynamic --with-stream_ssl_module --with-mail=dynamic --with-mail_ssl_module

我试图将以下代码添加到/etc/nginx/nginx.conf

set_real_ip_from   127.0.0.1;
real_ip_header      X-Forwarded-For;

然后     设置req.http.X-Forwarded-For = client.ip; 到/etc/varnish/default.vcl,但是由于它是Varnish的magento特定版本,因此我将其放置如下:

sub vcl_recv {
set req.http.X-Forwarded-For = client.ip;
if (req.method == "PURGE") {
    if (client.ip !~ purge) {
        return (synth(405, "Method not allowed"));
    }
    # To use the X-Pool header for purging varnish during automated deployments, make sure the X-Pool header
    # has been added to the response in your backend server config. This is used, for example, by the
    # capistrano-magento2 gem for purging old content from varnish during it's deploy routine.
    if (!req.http.X-Magento-Tags-Pattern && !req.http.X-Pool) {
        return (synth(400, "X-Magento-Tags-Pattern or X-Pool header required"));

重新启动这两个服务后,我仍然在日志和Magento 2中得到相同的127.0.0.1。

我很困惑,不胜感激!

1 个答案:

答案 0 :(得分:0)

我知道了,必须在服务器块中添加以下内容:

set_real_ip_from 10.0.0.0/8; #your ip
real_ip_header X-Real-IP;
real_ip_recursive on;

在这里找到了原始答案: https://calvin.me/forward-ip-addresses-when-using-nginx-proxy/