【问题标题】:Node Express routes working locally but not on remote serverNode Express 路由在本地工作但不在远程服务器上
【发布时间】:2017-05-15 10:49:44
【问题描述】:

我有一个在本地运行并使用 Express 进行路由的节点应用程序。我尝试使用their guide 在数字海洋上进行设置。当我导航到 / 时,我可以看到我的应用程序的主页,但我无法访问任何内页。

为了调试,我停止了通过 pm2 运行的应用程序,并通过在终端中调用“node app.js”简单地运行它,以便我可以看到控制台日志消息。

我把 cmets 放在 Express 路由上进行测试:

app.get('/', function (req, res) {
console.log('/ route requested');
if (req.isAuthenticated()) {
  res.render('home',
    {
      user: req.user
    });
} else {
  res.render('home',
    {
      user: null
    });
}
});

app.get('/signup', function (req, res) {
    console.log('/signup route requested');
    res.render('signup');
});

当我请求“/”时,会打印控制台日志消息。但是,尝试访问“/注册”时,终端中没有显示任何内容。我被带到一个默认的 404 页面 (nginx/1.10.0 (Ubuntu)。

我尝试将 Nginx 设置为 detailed here 的反向代理。这是我来自 etc/nginx/sites-available/default 的 nginx 配置

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

        # SSL configuration
        #
        # listen 443 ssl default_server;
        # listen [::]:443 ssl default_server;
        #
        # Note: You should disable gzip for SSL traffic.
        # See: https://bugs.debian.org/773332
        #
        # Read up on ssl_ciphers to ensure a secure configuration.
        # See: https://bugs.debian.org/765782
        #
        # Self signed certs generated by the ssl-cert package
        # Don't use them in a production server!
        #
        # include snippets/snakeoil.conf;

        root /var/www/html;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;

        server_name _;

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

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #       include snippets/fastcgi-php.conf;
        #

此外,索引页面也没有从我的公共目录中加载资源,例如样式。

如果能帮助我将 Express 路由和资源加载到远程服务器上,我将不胜感激。如果他们有更多有助于调试的信息,请告诉我。

【问题讨论】:

  • 这部分看起来很可疑:# First attempt to serve request as file, then
  • 好点@freshnode。它不在数字海洋示例中,所以我不太确定我从哪里得到的:$ 我会尝试不使用并报告
  • @freshnode 删除配置文件的# First attempt to serve request as file, then 部分就可以了。谢谢。想把它写成答案,这样我就可以将其标记为正确?
  • [编辑] 参考这个 StackOverflow 答案stackoverflow.com/a/49964108/4270123 这个解决了我的问题。

标签: node.js express digital-ocean


【解决方案1】:

从您发布的 nginx 配置来看,您似乎在使用try_files 指令和proxy_pass,这可能会导致竞争条件。 nginx 可能正在尝试将您的 URI 作为文件提供服务,但它几乎肯定无法做到。

Location 指令中删除以下行应该可以解决问题:

# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;

【讨论】:

    【解决方案2】:

    由于您从 nginx 收到默认的 404 页面,这可能意味着您的 node.js 应用程序无法直接访问,而是在一些 nginx 安装之后。真的吗?因为在这种情况下,你可能需要正确设置nginx,而express与它无关。

    也许您应该尝试直接连接到 nodejs 应用程序进行验证(在大多数示例中为 [host]:3000)。

    【讨论】:

    • 谢谢 - 我可以通过包含端口号来访问该应用程序。因此,在按照以下说明设置 nginx 作为反向代理时,我似乎犯了一些错误:digitalocean.com/community/tutorials/…我将尽快发布 nginx 配置代码
    猜你喜欢
    • 1970-01-01
    • 2018-11-30
    • 2017-07-04
    • 2016-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-23
    相关资源
    最近更新 更多