当Azure上托管后端时,create-react-app代理请求失败,404

tut*_*ain 6 azure reactjs create-react-app

我在设置我的create-react-app应用程序以将请求代理到Microsoft azure上的测试托管时遇到了一些麻烦.我在我的应用程序的package.json中设置了代理,如下所示:

"proxy":{
   "/api/*":{
   "target":"https://mytestbackend.azurewebsites.net",
   "secure":false
}
}
Run Code Online (Sandbox Code Playgroud)

我已经设置了一个axios请求,要求发送到azure上的后端服务器.它是一个独立的.js,我从我的一个反应应用程序的事件中调用它.它看起来像这样:

import axios from 'axios';

const login = async (username, password) => {
   console.log("Username to send is:"+username);
   console.log("password to send is:"+password);
   let response = await axios.post('/api/user/login',  {username:username,password:password});
   console.log(response);
};

export {login};
Run Code Online (Sandbox Code Playgroud)

问题不在我的反应组件中,因为这两个console.log()调用显示正在接收输入的值.如果我从package.json中删除"secure":false设置,则请求失败并显示Http Error:500.但如果我使用安全设置,则会失败并显示404页面.有人可以说清楚我做错了什么吗?我可以只在"localhost"上使用代理吗?文档另有说明.任何帮助是极大的赞赏.

我已经验证在Azure管理门户上运行dev服务器的域上启用了CORS.如果我直接使用后端的URL(即不使用create-react-app代理)来执行请求,它就可以工作.问题必须是代理配置的方式.

不使用安全时发生的HTTP Errpr 500的响应文本是:

Proxy error: Could not proxy request /api/user/login from localhost:3000 to https://mytestbackend.azurewebsites.net (undefined).
Run Code Online (Sandbox Code Playgroud)

附加信息:我还通过在我的开发机器上本地运行我的后端进行了测试.出现错误消息,但括号中的"未定义"表示"UNABLE_TO_VERIFY_LEAF_SIGNATURE".如果使用"secure:false,我可以成功调用登录端点,但是对需要身份验证的其他端点的调用失败,因为axios不发送cookie.

做:curl -v https://mytestbackend.azurewebsites.net/api/user/login

有这个输出:

* SSLv3, TLS handshake, Client hello (1):
* SSLv3, TLS handshake, Server hello (2):
* SSLv3, TLS handshake, CERT (11):
* SSLv3, TLS alert, Server hello (2):
* SSL certificate problem: unable to get local issuer certificate
* Closing connection #0
curl: (60) SSL certificate problem: unable to get local issuer certificate
More details here: http://curl.haxx.se/docs/sslcerts.html

curl performs SSL certificate verification by default, using a "bundle"
of Certificate Authority (CA) public keys (CA certs). If the default
bundle file isn't adequate, you can specify an alternate file
using the --cacert option.
If this HTTPS server uses a certificate signed by a CA represented in
the bundle, the certificate verification probably failed due to a
problem with the certificate (it might be expired, or the name might
not match the domain name in the URL).
If you'd like to turn off curl's verification of the certificate, use
the -k (or --insecure) option.
Run Code Online (Sandbox Code Playgroud)

Tar*_*ani 4

create-react-appuseWebPackDevServer使用https://github.com/chimurai/http-proxy-middleware#options

所以你可以使用同一个选项中的所有选项

header现在,在外部托管服务器的这种情况下导入的一个关键是host。如果不正确,有时会出现问题,请参见下面的示例

Websocket 适用于 EC2 url,但不适用于 ElasticBeanstalk URL

接下来是可能与 关联的 cookie localhost,我检查过,它们应该无需任何修改。cookieDomainRewrite: ""但您可能也想使用该选项

所以我将使用的最终配置如下

"proxy":{
   "/api/*":{
   "target":"https://mytestbackend.azurewebsites.net",
   "secure":false,
   "headers": {
     "host": "mytestbackend.azurewebsites.net"
    },
    "cookieDomainRewrite": ""
  }
}
Run Code Online (Sandbox Code Playgroud)

另外,在您的客户端上您想使用withCredentials:true

let userinfo =await axios.get('/api/secured/userinfo',{withCredentials:true});
Run Code Online (Sandbox Code Playgroud)