gpt4 book ai didi

node.js - Node Express 路由在本地工作但不在远程服务器上

转载 作者:太空宇宙 更新时间:2023-11-04 00:22:43 26 4
gpt4 key购买 nike

我有一个在本地工作并使用 Express 进行路由的 Node 应用程序。我尝试使用 their guide 在 digital ocean 上进行设置。当我导航到/时,我可以看到应用程序的主页,但无法访问任何内部页面。

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

我在 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');
});

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

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

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 配置来看,您似乎将 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;

关于node.js - Node Express 路由在本地工作但不在远程服务器上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43977576/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com