k6将localhost重定向到环回

  • 本文关键字:重定向 localhost k6 k6
  • 更新时间 :
  • 英文 :


我在Mac上,正试图在本地针对http://localhost:4200(angular应用程序(运行我的k6脚本。

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

我的k6脚本的基本URL设置为http://localhost:4200。然而,所有请求都被发送到http://127.0.0.1:4200,但被MacOS拒绝。

如何强制k6不将localhost重写到环回地址?

编辑

添加curl -vv

localhost: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

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

但是,您的应用程序似乎正在端口4200上侦听您的IPv6地址::1。:1是IPv6环回地址。CCD_ 10将主机名CCD_ 11解析为其IPv6地址。

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

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

  • 使您的应用程序绑定到IPv4IPv6,通常通过绑定到地址0.0.0.0来完成
  • 更改k6脚本以直接连接到IPv6::1
  • 指定--dns "policy=preferIPv6"或将dns:{policy:"preferIPv6"}添加到options(自0.29.0起(
  • 在操作系统中禁用IPv6。这是一个巨大的变化,我不建议这样做
  • 更改主机文件以将localhost解析为IPv4地址

最新更新