OpenStack MERN应用程序正在抛出net :: ERR_CONNECTION_REFUSED

问题描述 投票:0回答:1

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

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

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不显示任何类型的条目

我尝试了多种预期的解决方案,但它们都没有工作。任何人都可以在设置中的某处看到错误?

node.js reactjs nginx openstack mern
1个回答
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并解决问题。

© www.soinside.com 2019 - 2024. All rights reserved.