Vietnam

    Nodejs.vn

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Popular
    • Tags
    • Groups
    • Search

    Về việc lấy địa chỉ ip của client trên express nodejs.

    Node.JS
    0
    7
    1639
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Acenter Ha
      Acenter Ha last edited by

      Mình thử các cách nhưng chỉ có thể lấy được "ffff::127.0.0.1" thôi.
      Bạn nào có giải pháp mong được chỉ giáo.
      Cảm ơn.

      Khoa Lê Duy 1 Reply Last reply Reply Quote 0
      • Khoa Lê Duy
        Khoa Lê Duy @Acenter Ha last edited by

        @Acenter-Ha

        var ip = req.headers['x-forwarded-for'];
        

        Bạn thử xem

        1 Reply Last reply Reply Quote 0
        • Acenter Ha
          Acenter Ha last edited by

          @Khoa-Lê-Duy
          Cảm ơn bạn, mình thử nhiều cách vẫn chỉ trả về ::ffff:127.0.0.1 thôi.
          TÌm hiểu thì đây là ipv6, nhưng vì mình dùng ip để lưu thông tin guest nên nếu toàn bộ ip đều là ::ffff:127.0.0.1 thì không có ý nghĩa gì cả.

          Khoa Lê Duy 1 Reply Last reply Reply Quote 0
          • Khoa Lê Duy
            Khoa Lê Duy @Acenter Ha last edited by

            @Acenter-Ha
            Lạ nhỉ?
            Mình test trên máy mình vẫn ngon 🙂
            Hay do config của ip6
            Thử check my ip xem

            1 Reply Last reply Reply Quote 0
            • tuanquynet
              tuanquynet last edited by

              @Acenter-Ha
              Bạn thử đoạn code này xem
              function getCallerIP(request) {
              var ip = request.headers['x-forwarded-for'] ||
              request.connection.remoteAddress ||
              request.socket.remoteAddress ||
              request.connection.socket.remoteAddress;
              ip = ip.split(',')[0];
              ip = ip.split(':').slice(-1); //in case the ip returned in a format: "::ffff:146.xxx.xxx.xxx"
              return ip;
              }

              papa elf

              Acenter Ha 2 Replies Last reply Reply Quote 0
              • Acenter Ha
                Acenter Ha @tuanquynet last edited by

                @tuanquynet Mình đã thử rồi vẫn không được bạn ạ.
                Mình nghi là do VPS nó đang tự động redirect về ipv6 tất cả các request.
                Không biết có đúng hay không.

                Thử bằng page My IP mà bạn @Khoa-Lê-Duy gửi thì vẫn show ip bình thường. Test ở local vẫn nhận ip bình thường.

                1 Reply Last reply Reply Quote 0
                • Acenter Ha
                  Acenter Ha @tuanquynet last edited by

                  @tuanquynet @Khoa-Lê-Duy
                  Được rồi 2 bạn ạ, mình thay đổi NGINX config thì nhận được rồi.

                  Cảm ơn 2 bạn rất nhiều.

                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post