代码之家  ›  专栏  ›  技术社区  ›  a11smiles

k6将localhost重定向到环回

k6
  •  0
  • a11smiles  · 技术社区  · 3 年前

    我在Mac电脑上,我正试图运行我的k6脚本 http://localhost:4200 (angular应用)本地。

    angular应用程序正在运行,我可以通过浏览器和 curl .

    我的k6脚本的基本URL设置为 http://localhost:4200 .然而,所有请求都是向 http://127.0.0.1:4200 相反,MacOS否认了这一点。

    如何强制k6不重写 localhost 回送地址?

    编辑

    增加 curl -vv .

    本地主机:4200

    *   Trying ::1...
    * TCP_NODELAY set
    * Connected to localhost (::1) port 4200 (#0)
    > GET / HTTP/1.1
    > Host: localhost:4200
    > User-Agent: curl/7.64.1
    > Accept: */*
    > 
    < HTTP/1.1 200 OK
    < X-Powered-By: Express
    < Access-Control-Allow-Origin: *
    < Content-Type: text/html; charset=utf-8
    < Accept-Ranges: bytes
    < Content-Length: 942
    < ETag: W/"3ae-UQojFJZul+b6hEhgbvnN6wFCVuA"
    < Date: Thu, 20 Jan 2022 21:38:55 GMT
    < Connection: keep-alive
    < Keep-Alive: timeout=5
    < 
    <!doctype html>
    <html lang="en">
    <head>
      <meta charset="utf-8">
      <title>MyApp</title>
      <base href="/">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <script src="assets/scripts/apm.js"></script>
      <link rel="apple-touch-icon" sizes="180x180" href="/assets/images/apple-touch-icon.png">
      <link rel="icon" type="image/x-icon" href="favicon.ico">
      <link rel="icon" type="image/png" sizes="32x32" href="/assets/images/favicon-32x32.png">
      <link rel="icon" type="image/png" sizes="16x16" href="/assets/images/favicon-16x16.png">
      <link rel="manifest" href="/site.webmanifest">
    <link rel="stylesheet" href="styles.css"></head>
    <body>
      <app-root></app-root>
    <script src="runtime.js" type="module"></script><script src="polyfills.js" type="module"></script><script src="styles.js" defer></script><script src="vendor.js" type="module"></script><script src="main.js" type="module"></script></body>
    </html>
    * Connection #0 to host localhost left intact
    * Closing connection 0
    

    127.0.0.1:4200

    *   Trying 127.0.0.1...
    * TCP_NODELAY set
    * Connection failed
    * connect to 127.0.0.1 port 4200 failed: Connection refused
    * Failed to connect to 127.0.0.1 port 4200: Connection refused
    * Closing connection 0
    curl: (7) Failed to connect to 127.0.0.1 port 4200: Connection refused
    

    编辑2

    主机文件

    127.0.0.1   localhost
    255.255.255.255 broadcasthost
    ::1             localhost
    # Added by Docker Desktop
    # To allow the same kube context to work on the host and the container:
    127.0.0.1 kubernetes.docker.internal
    
    0 回复  |  直到 3 年前
        1
  •  0
  •   knittl    3 年前

    没有应用程序在端口4200上侦听您的IPv4地址 127.0.0.1 .127.0.0.1是IPv4环回地址。当k6请求 localhost ,此主机名解析为IPv4 127.0.0.1。

    然而,您的应用程序似乎正在端口4200上监听您的IPv6地址 ::1 .::1是IPv6环回地址。 curl 解析主机名 本地服务器 到它的IPv6地址。

    如何将应用程序绑定到端口?通常,当绑定到主机的所有接口时,您会使用特殊的IP地址 0.0.0.0 .

    我看到了一个潜在的解决方案:

    • 使应用程序绑定到IPv4 IPv6,通常通过绑定到地址 0.0.0.0 .
    • 更改k6脚本以连接到IPv6 ::1 直接地
    • 具体说明 --dns "policy=preferIPv6" 或者加上 dns:{policy:"preferIPv6"} 给你的 options (从0.29.0开始)
    • 在操作系统中禁用IPv6。这是一个巨大的变化,我不建议这样做
    • 更改主机文件以将localhost解析为IPv4地址
    推荐文章