繁体   English   中英

如何在 AWS 中使用 Nginx 来查看 serve.ejs?

[英]How to serve .ejs view with Nginx in AWS?

我的 Node 项目目录中有一个带有 EJS 视图 (/views/index.ejs) 的 Node 应用程序。 当我在本地使用 nodemon 运行文件 /server.js 时,它运行良好。

现在我正在尝试在 AWS 中部署 Node 应用程序。 所以我创建了 EC2 实例并安装了 Nginx 来反向代理到我的 Node 应用程序。 问题是我仍然看到 Nginx 欢迎页面,而不是 Node 应用程序。

这是/etc/nginx/sites-available/default

server {

    root ~/mynodeapp/views

    index index.ejs index.html index.htm 

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

这是~/mynodeapp/server.js

const app = require('./app');
const port = 3001;
const host = 'localhost/'

const server = app.listen(port, host, () => {
    console.log(`Node server listening to ${server.address().port}`);
})

我正在使用 PM2 运行 Node 应用程序,当我输入命令systemctl status pm2-ec2-user时,它显示active (running)

到目前为止,这是我尝试过的事情。

我试图将/etc/nginx/nginx.conf的根目录从/usr/share/nginx/html更改为~/mynodeapp/views但随后显示404

我尝试使用以下命令启用该站点

sudo ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled/

我该如何解决这个问题?

这可能会帮助你。 没有额外的工作,但要确保

// set the view engine to ejs
app.set('view engine', 'ejs');

// use res.render to load up an ejs view file

// index page
app.get('/', function(req, res) {
  res.render('pages/index');
});

这两行代码。

https://www.digitalocean.com/community/tutorials/how-to-use-ejs-to-template-your-node-application

当节点容器由 nginx 负载均衡器提供服务时,您是否找到了提供 .ejs 文件的任何方法

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM