Node Express 路由在本地工作但不在远程服务器上
Node Express routes working locally but not on remote server
我有一个在本地运行并使用 Express 进行路由的节点应用程序。我尝试使用 their guide 在数字海洋上进行设置。当我导航到 / 时,我可以看到我的应用程序的主页,但是我无法访问任何内部页面。
为了调试,我通过 pm2 运行 停止了应用程序,并通过在终端中调用 'node app.js' 简单地 运行 它,这样我就可以看到控制台日志消息。
我对测试的快速路线发表评论:
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');
});
当我请求“/”时,打印控制台日志消息。但是,尝试访问“/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;
#
索引页面也没有从我的 public 目录加载资源,例如样式。
我将不胜感激任何试图让 Express 路由和资源加载到远程服务器上的帮助。如果他们是有助于调试的更多信息,请告诉我。
由于您收到来自 nginx 的默认 404 页面,这可能意味着您的 node.js 应用程序无法直接访问,但在一些 nginx 安装之后。真的吗?因为在这种情况下,你很可能要正确设置nginx,express与它无关。
也许您应该尝试直接连接到 nodejs 应用程序进行验证(在大多数示例中为 [host]:3000)。
从您发布的 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;
我有一个在本地运行并使用 Express 进行路由的节点应用程序。我尝试使用 their guide 在数字海洋上进行设置。当我导航到 / 时,我可以看到我的应用程序的主页,但是我无法访问任何内部页面。
为了调试,我通过 pm2 运行 停止了应用程序,并通过在终端中调用 'node app.js' 简单地 运行 它,这样我就可以看到控制台日志消息。
我对测试的快速路线发表评论:
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');
});
当我请求“/”时,打印控制台日志消息。但是,尝试访问“/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;
#
索引页面也没有从我的 public 目录加载资源,例如样式。
我将不胜感激任何试图让 Express 路由和资源加载到远程服务器上的帮助。如果他们是有助于调试的更多信息,请告诉我。
由于您收到来自 nginx 的默认 404 页面,这可能意味着您的 node.js 应用程序无法直接访问,但在一些 nginx 安装之后。真的吗?因为在这种情况下,你很可能要正确设置nginx,express与它无关。
也许您应该尝试直接连接到 nodejs 应用程序进行验证(在大多数示例中为 [host]:3000)。
从您发布的 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;