如何在Ubuntu服务器上使用PM2和Nuxt配置Nginx?

xtfmy6hx  于 2023-06-21  发布在  Nginx
关注(0)|答案(3)|浏览(139)

Nuxt

我使用的是在端口3001上的hos mynuxt.dev上配置的Nuxt网站。nuxt.config.js

export default {
  server: {
    host: process.env.HOST,
    port: process.env.PORT,
  },
...
}

当我执行npm run dev时,我看到它正确地使用了这些主机和地址。

/etc/hosts

我也把它添加到/etc/hosts

127.0.0.1 mynuxt.dev

PM2

然后,我使用pm2如下:

npm run build
pm2 start
pm2 save

我可以看到网站运行pm2 monit(日志为空,没有错误)。

Nginx

然后,我按照here的说明配置了Nginx(我禁用了SSL配置):
/etc/nginx/sites-available/mynuxt.conf

upstream my_nodejs_upstream {
    server 127.0.0.1:3001;
    keepalive 64;
}

server {
    listen 80;
    #listen 443 ssl;
    
    server_name mynuxt.dev;
    #ssl_certificate_key /etc/ssl/main.key;
    #ssl_certificate     /etc/ssl/main.crt;
   
    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $http_host;
        
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        
        proxy_pass http://my_nodejs_upstream/;
        proxy_redirect off;
        proxy_read_timeout 240s;
    }
}

运行Nginx似乎一切正常,但我在/var/log/nginx/access.log/var/log/nginx/error.log中没有看到任何日志。

$ sudo  nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
$ sudo service nginx restart
$ sudo service nginx status
...active (running)...
$ sudo service php7.4-fpm status
...active (running)...

访问网站

为什么从mynuxt.dev的浏览器访问网站时,我会得到ERR_CONNECTION_REFUSED?我的配置有什么问题?
如何调试此问题?

uplii1fm

uplii1fm1#

也许它可以帮助你,我不使用“etc/hosts”配置,我一直在监听“localhost:3000”,正如你可以在下面的配置中看到的(nuxt/pm2/ubuntu)。它工作正常。

Nuxt

http://localhost:3000/上运行(作为nuxt.congig.js中的默认值)

etc/hosts

(我不使用它)

*pm2

(类似于您的命令)

Nginx

...

    location / {
        # without upstream on mine, but it's similar to yours
        proxy_pass http://localhost:3000/;
    }
bnlyeluc

bnlyeluc2#

您必须将mynuxt.dev添加到/etc/hosts

127.0.0.1 mynuxt.dev

然后重新启动pm2,它会工作。

vwkv1x7d

vwkv1x7d3#

    • 如果您有. output文件**

您的项目名称
1.后端
1.前端
.输出
ecosystem.config.js
package.json

    • NGINX**
sudo nano /etc/nginx/sites-available/your_app_name
server{
root /var/www/project_name/frontend/;
index index.html index.htm;
server_name your_server_name;
     location / {
            proxy_pass http://localhost:3000;
            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;
    }
}
    • 切换到站点启用**
sudo nano /etc/nginx/sites-enabled/your_app_name
sudo service nginx restart
sudo service nginx status
  • 状态应为活动状态 *
    • 和ecosystem. config. js**
module.exports = {
apps: [
    {
        name: 'yor_app_name', // example "MYAPP"
        port: '3000',
        exec_mode: 'cluster',
        instances: 'max',
        script: './.output/server/index.mjs',
    }
  ]

}

  • 和package. json *
{
   "name": "frontend",
   "version": "1.0.0",
    "description": "",
   "main": "ecosystem.config.js",
  "scripts": {
   "start": "node .output/server/index.mjs"
  },
"keywords": [],
"author": "",
"license": "ISC"

}

    • 完成**
pm2 start ecosystem.config.js
pm2 restart MYAPP

enter image description here,你应该让它工作。享受编码!!!

相关问题