【问题标题】:Nginx configuration - Can't make connection for separate 3 individual servers (Client/API/DB)Nginx 配置 - 无法为单独的 3 个单独的服务器(客户端/API/DB)建立连接
【发布时间】:2019-09-04 13:00:06
【问题描述】:

我要做什么

我正在尝试通过在本地创建 3 个具有以下 IP 地址的虚拟机并在下面简单测试 React/Node 代码,从而在 3 个单独的服务器(客户端、API 和 mySQL)之间建立连接。

Client with React JS    192.168.56.103    Port  3000
API with Node JS        192.168.56.104    Port  4000
mySQL                   192.168.56.105

问题

API 服务器可以成功连接到 mySQL 服务器。但是,客户端服务器似乎无法连接到 API 服务器。我收到以下错误

GET http://192.168.56.103:3000/192.168.56.104/products 404(未找到)

还有这个错误

SyntaxError: Unexpected token

我正在尝试解决这个问题将近一天。请帮忙。

Nginx 配置

我在客户端和 API 服务器上都使用 Nginx 作为 Web 服务器。这是客户端 Nginx 配置。

server {
        listen 80 default_server;
        listen [::]:80 default_server;

        root /sample_application/sample_front/public;
        index index.html ;

        server_name _;
        location / {
           try_files $uri $uri/ =404;
        }

这里是 API Nginx 配置。

server {
        listen 80 default_server;
        listen [::]:80 default_server;

        server_name _;

        location /products {
                proxy_pass http://localhost:4000;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Host $host;
                proxy_cache_bypass $http_upgrade;
        }

反应/节点代码

以下是 React js 代码的一部分。我已经在本地主机(没有虚拟机)上进行了本地测试,它们都工作得很好。但在 VM 上时,它只显示静态信息,但显示来自后端和数据库的数据。

getProducts = _ => {
    fetch(BACK_URL + '/products')
    //fetch(BACK_URL + ':' +BACK_PORT + '/products')
      .then(response => response.json())
      .then(response => this.setState({ products: response.data }))
      .catch(err => console.error(err))
  }

  addProduct = _ => {
    const { product } = this.state;
    fetch(BACK_URL + '/products/add?name=' + product.name)
    //fetch(BACK_URL + ':' +BACK_PORT + '/products/add?name=' + product.name)
      .then(this.getProducts)
      .catch(err => console.error(err))
  }

以下是 Node js 代码的一部分。控制台日志为“连接成功”。访问192.168.56.104:4000,数据库数据显示成功。

connection.connect(function(err) {
    if (err) throw(err);
      console.log("Connected success");
  });

app.use(cors());

app.get('/products', (req, res) => {
    connection.query(SELECT_ALL_PRODUCTS_QUERY, (err, results) => {
        if(err) {
            return res.send(err)
        }
        else {
            return res.json({
                data: results
            })
        }
    });
});

app.listen(BACK_URL.BACK_PORT, () => {
    console.log('Back-end listening on port' + ' ' + BACK_URL.BACK_PORT)
});

注意

我已经在本地运行 npm start 和 node 命令在本地测试 React/Node/mySQL。通过浏览器打开,它们可以完美运行。

我的知识。

编码和服务器部署的新手。

【问题讨论】:

  • 个人项目的不寻常设置,但问题出在 GET 请求 URL 中。如果客户端尝试访问 API,则不应为 http://192.168.56.103:3000/192.168.56.104/products,而应为:http://192.168.56.104:4000/products。您需要重新配置 fetch 基本网址。
  • @MattCarlotta 你能多指点一下吗?我尝试将获取更改为fetch(BACK_URL + ':' +BACK_PORT + '/products')。之前的 GET 错误 http://192.168.56.103:3000/192.168.56.104/products 已经消失。但是换了一个TypeError: Failed to execute 'fetch' on 'Window': Failed to parse URL from 192.168.56.104:4000/products
  • 下载 Postman:getpostman.com/downloads 并向 API 发出 GET 请求。此外,在您的 API 上安装并使用 Morgan:npmjs.com/package/morgan 作为快速中间件 (app.use(morgan('tiny'))。两者的结合将告诉您请求是否发送到您的服务器以及请求是否正确路由。由于这里有很多变量在起作用,所以尽量消除: 1.) 请求是否到达 API? 2.) API 是否正确处理请求? 3.) API 是否正确响应? 4.) 客户端是否收到 API 响应?

标签: reactjs nginx server


【解决方案1】:

我找到了解决办法。

在ip地址前加http://

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-23
    • 1970-01-01
    • 2013-02-01
    • 2011-04-09
    • 1970-01-01
    • 2021-11-06
    相关资源
    最近更新 更多