@st1v1ns

Сокеты. Как получить адрес клиента?

После добавления в ngnix.conf записей:
server
{
listen 443 ssl;
ssl_certificate /etc/ssl/site.ru.crt;  
ssl_certificate_key /etc/ssl/site.ru.key;
server_name site.ru www.site.ru;
charset UTF-8;
index index.php index.html;
disable_symlinks if_not_owner from=$root_path;
include /etc/nginx/vhosts-includes/*.conf;
include /etc/nginx/vhosts-resources/site.ru/*.conf;
access_log /var/www/httpd-logs/site.ru.access.log;
error_log /var/www/httpd-logs/site.ru.error.log notice;
ssi on;
set $root_path /var/www/user/data/www/site.ru;
root $root_path;
location / {
  location ~ [^/]\.ph(p\d*|tml)$ {
   try_files /does_not_exists @fallback;
  }
  location ~* ^.+\.(jpg|jpeg|gif|png|svg|js|css|mp3|ogg|mpe?g|avi|zip|gz|bz2?|rar|swf)$ {
   try_files $uri $uri/ @fallback;
  }
  location / {
   try_files /does_not_exists @fallback;
  }
}
location @fallback {
  proxy_pass http://127.0.0.1:8080;
  proxy_redirect http://127.0.0.1:8080 /;
  proxy_set_header Host $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 $scheme;
  proxy_set_header X-Forwarded-Port $server_port;
  access_log off;
}
location /ws/ {
                proxy_pass http://site.ru:8000/components/socket.php;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
                proxy_set_header Host $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_read_timeout 999999999; # neccessary to avoid websocket timeout disconnect
                proxy_redirect off;
    }
}


Remoteip apache2 работает. $_SERVER['REMOTE_ADDR'] отображает все правильно.

А вот socket_getpeername($socket_new, $ip) получает адрес сервера, а не клиента.
  • Вопрос задан
  • 80 просмотров
Пригласить эксперта
Ответы на вопрос 1
SagePtr
@SagePtr
Еда - это святое
В вашем случае вы в nginx передаёте этот адрес в заголовок X-Real-IP:
proxy_set_header X-Real-IP $remote_addr;
Потому можно проверять значение переменной $_SERVER["HTTP_X_REAL_IP"]. Но это в случае, если сокет доступен только из nginx, но напрямую недоступен (иначе клиент может любой заголовок туда протолкнуть).
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы