无法为Node.js应用正确配置Nginx

时间:2016-08-21 21:39:19

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

我正在尝试安装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/'); });

1 个答案:

答案 0 :(得分:0)

您的网站可供nginx使用,但未启用。因此,请尝试将您的配置(即/etc/nginx/sites-available/default)移动或sym链接到例如/etc/nginx/sites-enabled/your.domain.name/etc/init.d/nginx restart 然后运行

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

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

此外,您可能会从配置中添加以下内容中受益:

sites-enabled

当放入{{1}}时,我在DigitalOcean上的配置几乎相同,但对于https来说,它的工作正常。