如何解决在生产中出现 net::ERR_CONNECTION_REFUSED 错误
我已经部署了我的 React 应用程序,它也有一个后端。我已经使用 npm start 命令从我的服务器启动了我的前端和后端。前端工作正常,但后端没有。 (从本地主机完美运行)
前端从 3000 端口开放,后端从 9000 端口开放。
这是我的 Nginx 配置
server {
server_name myservername.com www.myservername.com;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
server {
if($host = www.myservername.com){
return 301 https://$host$request_uri;
}
if($host = myservername.com){
return 301 https://$host$request_uri;
}
listen 80 default_server;
listen [::]:80 default_server;
server_name myservername.com www.myservername.com;
return 404;
}
正在使用的端口
我在控制台中遇到的错误
POST http://localhost:9000/bot net::ERR_CONNECTION_REFUSED
我该如何解决这个问题?
问题正在尝试执行此提取
fetch("http://localhost:9000/bot",{
method: 'POST',headers: {'Content-Type': 'application/json'},body: JSON.stringify(data)
}).then(res => res.text())
.then(res => {
res = JSON.parse(res);
this.setState({ apiResponse: res.response,apiIntent: res.intent},() => {this.setBotMsg()});
console.log(`This is the response: ${res.response},${res.intent}`);
;
});
解决方法
这将调用您计算机端口 9000 上的本地环回适配器。我认为这不是您想要的。使用您的服务器 IP 或域名代替 localhost。
fetch("http://myservername.com:9000/bot",{
method: 'POST',headers: {'Content-Type': 'application/json'},body: JSON.stringify(data)
}).then(res => res.text())
.then(res => {
res = JSON.parse(res);
this.setState({ apiResponse: res.response,apiIntent: res.intent},() => {this.setBotMsg()});
console.log(`This is the response: ${res.response},${res.intent}`);
;
});
我会使用 NGINX 将请求代理到您的后端,而不是直接暴露端口 9000。
fetch("https://myservername.com/bot",${res.intent}`);
;
});
nginx 配置取决于您想要做什么。子域或位置?子域可能类似于 bot.myserver.com
。位置 myserver.com/bot
。
server {
listen 443
.....
location /bot {
proxy_pass http://localhost:9000;
proxy_set_header Host $host;
....
}
}
附加说明:我已更新配置以从前端使用 https。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。