簡體   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