使用Websocket的反向代理读取真实客户端IP的问题

时间:2019-09-04 23:13:06

标签: nginx nginx-location nginx-reverse-proxy

我使用一些聊天服务器(不是nodejs),最后经过艰苦的努力,我可以使其在https下工作,因为它无法在聊天服务器文件中进行修改,但是经过NGINX的多次尝试后,我还是可以正常工作

我的聊天服务器捕获所有用户ip,并允许我禁止或踢人等控制用户。 例如,显示我的服务器ip的所有访问者而不是每个客户端的isp ip 在代理服务器之前

  • 用户1> IP 41.34.229.20
  • 用户2> IP 50.34.60.10

代理服务器之后,现在所有用户都有我的服务器IP

  • 用户1> IP 90.80.70.60
  • 用户2> IP 90.80.70.60

客户端文件只是html和js文件,但是在ws://下工作,并且在代理服务器之后,我进行了一些更改,并将客户端转换为php和javascript,并将websocket更新为wss://,一切正常这是我的 问题,我无法获得用户ip

客户端路径文件夹/var/www/site1.com/html/client/index.php 我相信我的.conf文件中存在错误或缺失 感谢帮助 现在这是我在NGINX /etc/nginx/conf.d/site.com.conf

中的site.com配置文件
server {
    listen 80;
    listen [::]:80;
    listen 443 ssl;
    listen [::]:443 ssl;
    index index index.php index.html;
    server_name site1.com;
    root /var/www/site1.com/html;
    proxy_set_header Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    # index.html fallback
    location / {
        try_files $uri $uri/ /index.html;
    }
    location ~ \.php$ {
        fastcgi_param REMOTE_ADDR $http_x_real_ip;
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;     
    }

    # SSL
    ssl_certificate /etc/letsencrypt/live/site1.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/site1.com/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/site1.com/chain.pem;

    # logging
    access_log /var/log/nginx/site1.com.access.log;
    error_log /var/log/nginx/site1.com.error.log warn;
}


server {
    listen 35557 ssl;
    server_name  site1.com;
    index index index.php index.html;
    root /var/www/site1.com/chatserver/client;
    location / {
        access_log on;
        proxy_ssl_session_reuse on;
        proxy_pass http://site1.com:35556;
        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 Host $host;
        proxy_set_header REMOTE_ADDR $remote_addr;
        #proxy_set_header Host $host;
        # WebSocket support (nginx 1.4)
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "Upgrade";
            proxy_set_header Proxy "";
            proxy_set_header Host $http_host;
            proxy_connect_timeout   1000;
            proxy_send_timeout      1500;
            proxy_read_timeout      2000;

    }
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }

    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header X-Frame-Options SAMEORIGIN always;
    add_header X-XSS-Protection "1; mode=block" always;
    add_header X-Robots-Tag none;
    add_header "X-Permitted-Cross-Domain-Policies" "master-only";
    add_header 'Referrer-Policy' 'no-referrer-when-downgrade';
    add_header Content-Security-Policy "default-src 'self' wss: https://www.site2.com https://app.site2.com https://chat.site2.com " always;

    #  ssl on;
    ssl_certificate   /etc/letsencrypt/live/site1.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/site1.com/privkey.pem;
    ssl_session_timeout 1d;
    ssl_prefer_server_ciphers   on;
    ssl_session_cache shared:SSL:10m;
      ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
          ssl_ciphers 'ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA:ECDHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES256-SHA:ECDHE-ECDSA-DES-CBC3-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:DES-CBC3-SHA:!DSS';
          resolver 8.8.8.8 8.8.4.4;
          ssl_stapling on;
          ssl_stapling_verify on;
}

upstream webpush {
    ip_hash;
    server site1.com:35556;
}

0 个答案:

没有答案