Nginx位置不适用于数字海洋

时间:2017-06-02 03:08:13

标签: node.js nginx location digital-ocean

我在DigitalOcean&#39的Ubuntu机器上创建了两个node.js实例。 两者都使用不同的端口监听localhost。 然后通过curl http:\ localhost:$ {port}进行测试。两者都有效。

然后我将它们添加到/ etc / nginx / site-available / default

location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            #try_files $uri $uri/ =404;
            proxy_pass http://localhost:8837;
            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;
}

location /app {
            proxy_pass http://localhost:9837;
            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;
}

仅限http:// $ {ip_address}:$ {port} / working。

http:// $ {ip_address}:$ {port} / app return "不能GET / app"

我在node.js中使用express是问题来自吗?

1 个答案:

答案 0 :(得分:0)

我不知道哪里出错了,这就是我发现的:

如果从node.js中删除express并像这样监听端口:

var http = require('http');
http.createServer(function (req, res) {
  ...
}).listen(8001, 'localhost');

/ app工作,没有错误发生。 如果使用表达如下:

const app = express();
app.listen(8001, 'localhost', function() {
   ...
});

/ app将返回错误。

要解决此问题,我打开另一个端口,通过复制/ etc / nginx / sites-available / default创建另一个配置,然后将其链接到/ etc / nginx / sites-enabled sudo ln -s / etc / nginx / sites-available / default2 / etc / nginx / sites-enabled / 并更改服务器{}

中的端口