简体   繁体   English

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

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

I have a Node app with an EJS view (/views/index.ejs) in my Node project directory.我的 Node 项目目录中有一个带有 EJS 视图 (/views/index.ejs) 的 Node 应用程序。 It is working well when I run the file /server.js using nodemon in my local.当我在本地使用 nodemon 运行文件 /server.js 时,它运行良好。

Now I'm trying to deploy the Node app in AWS.现在我正在尝试在 AWS 中部署 Node 应用程序。 So I created the EC2 instance and installed Nginx to reverse the proxy to my Node app.所以我创建了 EC2 实例并安装了 Nginx 来反向代理到我的 Node 应用程序。 The problem is I'm still seeing the Nginx welcome page instead of the Node app.问题是我仍然看到 Nginx 欢迎页面,而不是 Node 应用程序。

Here is the /etc/nginx/sites-available/default这是/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;
    }
}

Here is the ~/mynodeapp/server.js这是~/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}`);
})

I'm using PM2 to run the Node app and when I put the command systemctl status pm2-ec2-user , it says active (running)我正在使用 PM2 运行 Node 应用程序,当我输入命令systemctl status pm2-ec2-user时,它显示active (running)

Here are the things that I have tried so far.到目前为止,这是我尝试过的事情。

I tried to change the root of the /etc/nginx/nginx.conf from /usr/share/nginx/html to ~/mynodeapp/views but then it shows 404我试图将/etc/nginx/nginx.conf的根目录从/usr/share/nginx/html更改为~/mynodeapp/views但随后显示404

I tried to enable the site using the following command我尝试使用以下命令启用该站点

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

How can I solve this?我该如何解决这个问题?

This might help you.这可能会帮助你。 there is no extra work for this but making sure that没有额外的工作,但要确保

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

this two line of code.这两行代码。

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

Did you found any way to serve the.ejs files while the node container is served by nginx load balancer当节点容器由 nginx 负载均衡器提供服务时,您是否找到了提供 .ejs 文件的任何方法

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

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