【问题标题】:Nginx location not work on digital-oceanNginx 位置不适用于数字海洋
【发布时间】:2017-06-02 03:08:12
【问题描述】:

我在 DigitalOcean 的 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}/ 有效。

http://${ip_address}:${port}/app 返回 “无法获取 /app”

我在 node.js 中使用 express 是不是问题出在哪里?

【问题讨论】:

  • 你能发布你的 nginx 错误日志吗?它们常见于/var/log/nginx/error.log
  • 感谢 Oliver,我找不到 error.log,因为我通过复制 /etc/nginx/sites-available/ 中的默认值将应用程序移动到另一个端口,并监听另一个端口。

标签: node.js nginx location digital-ocean


【解决方案1】:

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

如果从 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/ 并更改服务器中的端口{}

【讨论】:

    猜你喜欢
    • 2018-03-01
    • 1970-01-01
    • 2018-06-21
    • 2016-12-28
    • 1970-01-01
    • 2014-12-20
    • 2013-10-14
    • 2022-11-22
    • 1970-01-01
    相关资源
    最近更新 更多