Nginx и Cloudflare с shoutcast получают реальные IP - PullRequest
0 голосов
/ 01 мая 2018

Так что в настоящее время у меня есть сервер, на котором работает nginx и shoutcast. Я бегу Ubuntu 16.04.4 LTS (GNU/Linux 3.13.0-52-generic x86_64).

Используя cloudflare, я связываю поддомен (используя запись A) с моим IP. Этот субдомен проксируется на mypi: 8000 для доступа к shoutcast verver, используя этот домен.

Теперь я получаю shoutcast простой IP-адрес на сервере для каждого подключенного клиента, поэтому каждый IP-адрес одинаков, и у меня не может быть уникальных слушателей.

Вот мой конфиг Nginx:

##
# Default server configuration
##

server {
        listen 80;
        listen [::]:80 default_server ipv6only=on;
        listen 443 ssl http2;

        ssl on;
        ssl_certificate /etc/nginx/ssl/cert.pem;
        ssl_certificate_key /etc/nginx/ssl/key.pem;


        root /usr/share/nginx/html;
        index index.php index.html index.htm;

        server_name rallypodium.be www.rallypodium.be;

        if ($http_host = rallypodium.be){
                rewrite  (.*)  https://www.rallypodium.be$1;
        }

        location / {
                try_files $uri $uri/ /index.php?$query_string;
        }

        error_page 404 /404.html;

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
                root /usr/share/nginx/html;
        }

        # pass the PHP scripts to FastCGI server listening on the php-fpm socket
        location ~ \.php$ {
                fastcgi_cache phpcache; # The name of the cache key-zone to use
                fastcgi_cache_valid 200 30m; # What to cache: 'Code 200' responses, for half an hour
                fastcgi_cache_methods GET HEAD; # What to cache: only GET and HEAD requests (not POST)
                add_header X-Fastcgi-Cache $upstream_cache_status; # Add header so we can see if the cache hits or misses

                try_files $uri $uri/ /index.php?$query_string;
                fastcgi_pass unix:/run/php/php7.0-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;                
                include fastcgi_params;

        }

}

##############################################################
# SHOUTCAST SERVER CONFIGURATION
##############################################################

server {
        listen   80;
        listen [::]:80;
        listen 443 ssl http2;
        server_name stream.rallypodium.be;

        location / {
                proxy_pass http://serverip:8000;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }   
}

serverIP - это тот же IP-адрес, на котором работает nginx. Так что "localhost" здесь ... Есть идеи? Я уже пытался добавить set_real_ip_from со всеми Ip-адресами CF, а затем добавить real_ip_header CF-Connecting-IP;, но это, похоже, не работает ...

1 Ответ

0 голосов
/ 01 мая 2018

Ознакомьтесь с этой статьей Cloudflare о том, как восстановить исходный IP-адрес посетителя

По сути, вам нужно использовать ngx_http_realip_module и этот конф:

server {
    listen  80;
    server_name   www.example.com;

    # cloudflare
    set_real_ip_from 103.21.244.0/22;
    set_real_ip_from 103.22.200.0/22;
    set_real_ip_from 103.31.4.0/22;
    set_real_ip_from 104.16.0.0/12;
    set_real_ip_from 108.162.192.0/18;
    set_real_ip_from 131.0.72.0/22;
    set_real_ip_from 141.101.64.0/18;
    set_real_ip_from 162.158.0.0/15;
    set_real_ip_from 172.64.0.0/13;
    set_real_ip_from 173.245.48.0/20;
    set_real_ip_from 188.114.96.0/20;
    set_real_ip_from 190.93.240.0/20;
    set_real_ip_from 197.234.240.0/22;
    set_real_ip_from 198.41.128.0/17;
    set_real_ip_from 2400:cb00::/32;
    set_real_ip_from 2606:4700::/32;
    set_real_ip_from 2803:f800::/32;
    set_real_ip_from 2405:b500::/32;
    set_real_ip_from 2405:8100::/32;
    set_real_ip_from 2c0f:f248::/32;
    set_real_ip_from 2a06:98c0::/29;

    real_ip_header CF-Connecting-IP;

    # ...
}

Будьте в курсе использования IP: https://www.cloudflare.com/ips

Также попробуйте:

proxy_set_header X-Real-IP $http_cf_connecting_ip;
proxy_set_header X-Forwarded-For $http_x_forwarded_for;
...