Не удается получить реальный IP-адрес из nginx / varnish для передачи, всегда показывает 127.0.0.1 - PullRequest
0 голосов
/ 11 июня 2019

Я хотел бы, чтобы реальный IP-адрес отображался на сервере / magento, но он продолжает снижаться до 127.0.0.1

Сервер настроен следующим образом: - Varnish listen для порта 80 - Nginx listenна порту 8080 - трафик SSL передается на порт 80. Последний веб-сайт, который требуется для этого, - это сайт magento 2

Nginx версии 1.14.0 с with-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"));

После перезапуска обеих служб я по-прежнему получаю тот же 127.0.0.1 вжурнал как и в Magento 2.

Я озадачен, поэтому любая помощь приветствуется!

1 Ответ

0 голосов
/ 19 июня 2019

Я понял, к блоку сервера нужно добавить следующее:

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/

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...