简体   繁体   English

2 ip 在 1 个请求中使用 docker/kubernetes 上的 axio

[英]2 ip in 1 request with axio on docker/kubernetes

I am making an application with docker and orchestrator kubernetes.我正在使用 docker 和协调器 kubernetes 进行应用程序。 In the fronted it is made with create-react-app, the problem comes when I want to use axios I get 2 ip in a request.在前面,它是用 create-react-app 制作的,当我想使用 axios 我在请求中得到 2 ip 时,问题就来了。

the code below is the action of the redux to capture the data下面的代码是 redux 捕获数据的动作

import { LOGIN, LOGIN_EXITO, LOGIN_ERROR } from "../types";

// import clienteAxios from "../../axios";
import axios from "axios";
import Swal from "sweetalert2";

const HOST = window._env_.REACT_APP_HOST_BACKEND;
const PORT = window._env_.REACT_APP_PORT_BACKEND;

export function loginAction(user, password) {
  return async (dispatch) => {
    dispatch(login());

    try {
      // let datos = await clienteAxios.post("/login", { user, password });
      console.log(`${HOST}:${PORT}/api`);
      let datos = await axios.post(`${HOST}:${PORT}/api`, { user, password });
      console.log(datos);

      dispatch(loginExito(datos.data));

      if (datos.data[0].r_mensaje === "ok") {
        Swal.fire("Acceso...", "Bienvenido", "success");
      } else {
        Swal.fire({
          icon: "error",
          title: "Error...",
          text: datos.data[0].r_mensaje,
        });
      }
    } catch (error) {
      console.log(error);
      dispatch(loginError(true));

      Swal.fire({
        icon: "error",
        title: "Error...",
        text: "Intente de nuevo...",
      });
    }
  };
}

and when making the query this error throws me, that 2 ip join在进行查询时,这个错误会抛出我,2 ip join 在此处输入图像描述

this is my nginx.conf that I use in the dockerfile.这是我在 dockerfile 中使用的 nginx.conf。

server {

  listen 3000;

  location / {
    root   /usr/share/nginx/html;
    index  index.html index.htm;
    try_files $uri $uri/ /index.html;
  }

  error_page   500 502 503 504  /50x.html;

  location = /50x.html {
    root   /usr/share/nginx/html;
  }

}

Axios is interpreting your argument as a relative path and is automatically appending the scheme, host and port to form the request URL. Axios 将您的参数解释为相对路径,并自动附加方案、主机和端口以形成请求 URL。

For example, axios.post('/user', ...) , would translate to http://172.17.0.3:30599/user .例如, axios.post('/user', ...)将转换为http://172.17.0.3:30599/user

You can fix this by passing the full URL:您可以通过传递完整的 URL 来解决此问题:

axios.post(`http://${HOST}:${PORT}/api`, ...)

Alternatively, create an axios instance, set baseURL in the config, and use instance.post('/api') .或者,创建一个 axios 实例,在配置中设置baseURL ,然后使用instance.post('/api')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM