【问题标题】:Unable to configure Nginx correctly for Node.js app无法为 Node.js 应用程序正确配置 Nginx
【发布时间】:2016-08-22 15:30:50
【问题描述】:

我正在尝试安装 react.js 应用并使用 pm2 包来运行服务器。

我的/etc/nginx/sites-available/default 文件的内容是:

server {
    listen 80;

    server_name my.domain.name;

    location / {
        proxy_pass http://127.0.0.1:8080;
        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;
    }
}

我的反应服务器在端口 8080 上运行,因为当我从服务器执行 curl localhost:8080 时,我得到了适当的响应。另外,通过curl localhost,我也得到了正确的反应服务器响应。

但是,当我在浏览器中访问my.domain.name 时,我只是得到默认的 nginx 页面,说它已成功安装。

我在这里错过了什么?

编辑:

添加了 Reactjs 应用的 server.js 文件:

var webpack = require('webpack');
var WebpackDevServer = require('webpack-dev-server');
var config = require('./webpack.config');

new WebpackDevServer(webpack(config), {
  publicPath: config.output.publicPath,
  hot: true,
  historyApiFallback: true
}).listen(8080, 'localhost', function (err, result) {
  if (err) {
    return console.log(err);
  }

console.log('收听http://localhost:8080/'); });

【问题讨论】:

  • 这个问题与 react 或任何形式的实际服务器应用程序完全无关。这是纯粹的 nginx 配置问题,因此问题的标题具有误导性。
  • @OlegSklyar 更新了标题
  • 检查您的主机条目设置天气来自您的主机的请求映射到 localhost:8080

标签: node.js reactjs nginx digital-ocean pm2


【解决方案1】:

您的站点可用于 nginx,但未启用。因此,请尝试将您的配置(即/etc/nginx/sites-available/default)移动或符号链接到例如/etc/nginx/sites-enabled/your.domain.name 然后运行 ​​

/etc/init.d/nginx restart

接下来,the following hint 可能会进一步帮助解决节点内部问题。

此外,您可能会受益于将以下内容添加到您的配置中:

proxy_redirect  http://localhost:8080 http://your.domain.name;

当放入 sites-enabled 时,我在 DigitalOcean 上的配置几乎相同,但对于 https,它工作得很好。

【讨论】:

  • 我已经符号链接了我的配置文件。添加proxy_redirect 似乎也无济于事。
  • 如果您已对配置进行符号链接,请再次更新您的问题,因为问题出在某个小细节上:鉴于文件的内容和位置,这应该可以工作。但是请尝试在服务器配置中省略“localhost”,这将使其监听 0.0.0.0。如果可行,请将其限制为 127.0.0.1 并重试
  • 检查我添加到答案的链接,基本上它与我在评论中的内容大致相同:尝试在节点 http 服务器监听方法中跳过主机名。
猜你喜欢
  • 2021-03-31
  • 2017-10-01
  • 2021-01-20
  • 1970-01-01
  • 2019-05-10
  • 2023-03-17
  • 2020-01-14
  • 2010-11-02
  • 2015-03-09
相关资源
最近更新 更多