无法使用NPM运行构建运行NextJS

发布于 2025-02-01 08:52:53 字数 506 浏览 1 评论 0原文

我最近在Digitalocean服务器上部署了一个React静态应用程序。我所做的只是在nginx配置文件中显示构建文件的根目录。而且运行良好。

现在,我试图在服务器上托管一个NextJS应用程序。我显示了root目录root/var/www/html/nextjstestapp/.next我什至尝试了root/var/var/var/www/html/nextjstestapp/node_modules 当我运行npm run build完成构建时,我尝试到达服务器,它显示“ 502 BAD GATEWAY”,

但是当我运行npm start站点图像 该应用程序正在显示

我可以一直在运行它的操作吗?我正在使用digitalocean液滴。

I recently deployed a react static app on my digitalocean server. All I did was show the root directory of the build file in the Nginx config file. And it's running well.

Now I am trying to host a NextJS app on my server. I showed the root directory root /var/www/html/NextJSTestApp/.next I even tried with root /var/www/html/NextJSTestApp/node_modules
When I am running the npm run build the build is done then I tried to reach my server it's showing "502 Bad Gateway"

But when I am running npm start
site image
The app is showing

What I can do to run it all the time? I am using digitalocean droplet.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

唱一曲作罢 2025-02-08 08:52:53

当运行NextJS作为节点应用程序(例如下一个启动)时,您需要将NGINX设置为使用proxy_pass而不是将其指向根目录(省略根指令)。

示例nginx位置块

server {
    server_name example.com;
    listen 443 http2 ssl;
    listen [::]:443 http2 ssl;

    ssl_certificate /etc/nginx/certs/fullchain.cer;
    ssl_certificate_key /etc/nginx/certs/fullchain.key;

    location / {

            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $host;
            proxy_http_version 1.1;
            proxy_pass http://127.0.0.1:8000; //your next app
    }
}

When running NextJS as a node application (e.g. next start), you need to set nginx to use proxy_pass instead of pointing it to a root directory (omit root directive).

Example nginx location block

server {
    server_name example.com;
    listen 443 http2 ssl;
    listen [::]:443 http2 ssl;

    ssl_certificate /etc/nginx/certs/fullchain.cer;
    ssl_certificate_key /etc/nginx/certs/fullchain.key;

    location / {

            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $host;
            proxy_http_version 1.1;
            proxy_pass http://127.0.0.1:8000; //your next app
    }
}
坏尐絯℡ 2025-02-08 08:52:53

首先,一旦您关闭终端或留下bash,手动运行下一个启动就不会持续存在。为了让其在后台运行,您可以使用&&在开始命令的末尾(下一个启动&&)或创建一个SystemD文件(建议)。

其次,一旦您完成了在后台运行的nextJ,就可以继续并设置代理通行证到您的Web服务器(nginx,apache ...)

First, Manually running next start does not persist the process once you close the terminal or leave the bash. In order to let it run in the background, you may use && at the end of start command (next start &&) or create a systemd file (recommended).

Second, Once you are done with a nextJs running on background, you can go ahead and setup a proxy pass to your web server(nginx, apache ...)

浅笑轻吟梦一曲 2025-02-08 08:52:53

我找到了这个解决方案。为了运行这个项目背景,我使用了PM2。

  • sudo npm install -G pm2
    然后转到我的项目文件夹CD网站/

pm2开始-name = nebs npm -start

然后pm2 startup startup systemd

我遵循此 tutorial

I found this solution. To run this project background I used pm2.

  • sudo npm install -g pm2
    Then gone to my project folder cd website/

pm2 start --name=website npm -- start

then pm2 startup systemd

I followed this tutorial

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文