简体   繁体   中英

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. It is working well when I run the file /server.js using nodemon in my local.

Now I'm trying to deploy the Node app in AWS. So I created the EC2 instance and installed Nginx to reverse the proxy to my Node app. The problem is I'm still seeing the Nginx welcome page instead of the Node app.

Here is the /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

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)

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

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

Did you found any way to serve the.ejs files while the node container is served by nginx load balancer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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