OpenStack MERN应用程序抛出了net :: ERR_CONNECTION_REFUSED

时间:2019-03-06 20:37:59

标签: node.js reactjs nginx openstack mern

我的应用程序必须在OpenStack环境(正常的托管服务提供商托管的Ubuntu 18.04 VPS)上运行。

在我的本地计算机上,一切正常,但是当我部署到OpenStack服务器时,在对Node.js后端的React调用中出现错误:GET https://localhost:3000/api/room/name/Palace net::ERR_CONNECTION_REFUSED

Error: Network Error
    at e.exports (createError.js:17)
    at XMLHttpRequest.p.onerror (xhr.js:87)

NGINX配置:

  GNU nano 2.9.3                                                                                      /etc/nginx/conf.d/ticket-system.ml.conf

server {
    if ($host = www.xxxxxxxx) {
        return 301 https://$host$request_uri;
    }


    if ($host = xxxxxxxx) {
        return 301 https://$host$request_uri;
    }

}

server {
        listen 443 ssl;
        listen [::]:443 ssl;
        server_name xxxxxxxx www.xxxxxxxx;
        ssl_certificate /etc/letsencrypt/live/xxxxxxxx;
        ssl_certificate_key /etc/letsencrypt/live/xxxxxxxx;
        ssl_ciphers         EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH;
        ssl_protocols       TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;
        location /  {
                proxy_pass    http://localhost:5000; # React app running on port 5000
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade';
                proxy_set_header Host $host;
                proxy_cache_bypass $http_upgrade;
        }
}

我的Node.js应用配置有:

// static files location
app.use(express.static(path.join(__dirname, 'client', 'build')));
...
// configure our app to handle CORS requests
app.use(cors());
...
const port = process.env.PORT || 3000;

app.listen(port, () => console.log(`Server running on port ${port}`));

我的React应用配置有:

package.json:

"proxy": "https://192.168.0.12:3000"

room.component.js:

  componentDidMount() {
    axios
      .get('https://localhost:3000/api/room/name/Palace')
      .then(res => {
        this.setState({
          ******
        });
      })
      .catch(err => console.log(err));
  }

cat /var/log/nginx/error.log显示任何类型的条目

我已经尝试了多种预期的解决方案,但是都没有用。 有人可以在设置的某处看到错误吗?

1 个答案:

答案 0 :(得分:0)

通过将nodejs位置添加到nginx配置文件中进行修复:

        location /api/ {
                proxy_pass http://localhost:8000/api/; # Node.js app running on port 8000, change if needed
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade';
                proxy_set_header Host $host;
                proxy_cache_bypass $http_upgrade;
        }

重新加载nginx并解决问题。