首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在生产环境中获取net::ERR_CONNECTION_REFUSED错误

问在生产环境中获取net::ERR_CONNECTION_REFUSED错误
EN

Stack Overflow用户
提问于 2021-01-13 17:09:12
回答 1查看 802关注 0票数 0

我已经部署了我的react应用程序,它也有一个后端。我已经使用npm start命令从我的服务器启动了前端和后端。前端运行正常,但后端运行不正常。(在localhost中完美工作)

前端从端口3000打开,后端从端口9000打开。

这是我的nginx配置

代码语言:javascript
复制
    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;

}

正在使用的端口

​

​

我在控制台中遇到的错误

代码语言:javascript
复制
POST http://localhost:9000/bot net::ERR_CONNECTION_REFUSED

如何解决此问题?

问题是尝试执行此获取操作

代码语言:javascript
复制
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}`); 
          ;
        });
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-13 17:32:01

这将向计算机端口9000上的本地环回适配器发出调用。我不认为这是你想要的。在localhost的位置使用您的服务器IP或域名。

代码语言:javascript
复制
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。

代码语言:javascript
复制
fetch("https://myservername.com/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的配置取决于你想要做什么。子域或位置?子域可以是类似bot.myserver.com的域名。一个位置myserver.com/bot。

代码语言:javascript
复制
server {
 listen 443


  .....


  location /bot {
    proxy_pass http://localhost:9000;
    proxy_set_header Host $host;
    ....
  }

}

补充说明:我已经更新了配置,以便从前端使用https。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65698714

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档