【问题标题】:How to configure Nginx with PM2 and Nuxt on an Ubuntu server?如何在 Ubuntu 服务器上使用 PM2 和 Nuxt 配置 Nginx?
【发布时间】:2021-03-14 13:27:22
【问题描述】:

Nuxt

我正在使用在 hos mynuxt.dev 端口 3001 上配置的 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 ?我的配置有什么问题?

如何调试这个问题?

【问题讨论】:

    标签: ubuntu nginx nuxt.js pm2


    【解决方案1】:

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

    127.0.0.1 mynuxt.dev
    

    然后重新启动 pm2 就可以了。

    【讨论】:

      【解决方案2】:

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

      Nuxt

      http://localhost:3000/ 上运行(nuxt.congig.js 中的默认设置)

      等/主机

      (我没用过)

      pm2

      (类似于你的命令)

      Nginx

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

      【讨论】:

        猜你喜欢
        • 2021-10-12
        • 1970-01-01
        • 2021-09-06
        • 1970-01-01
        • 2020-10-16
        • 2020-12-27
        • 2016-08-21
        • 2017-07-08
        • 2018-05-30
        相关资源
        最近更新 更多