3

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

服务器设置如下: - Varnish 监听端口 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,但由于它是一个magento特定版本的Varnish,我把它放在下面:

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。

我很困惑,所以任何帮助表示赞赏!

4

1 回答 1

1

我想通了,必须将以下内容添加到服务器块中:

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/

于 2019-06-18T23:20:29.523 回答