如何使用 NGINX 部署 NextJS?

Obl*_*der 12 nginx reactjs next.js

所以我知道如何在服务器上部署 React 应用程序。

  • npm 运行构建
  • 创建一个服务器块并将根指向我的反应应用程序文件夹构建(root /var/www/xfolder/build;
  • systemctl 重启 nginx
  • 运行我的节点服务器(nohup 节点服务器 &&)并完成。

我对 NextJS 不理解这一点感到有点愚蠢。我运行 npm run build 在此处输入图片说明

我期待像构建文件夹这样的东西。我已经尝试将服务器块根设置为 /var/www/xfolder/.next但该页面仍然禁止 403。我需要运行 npm run start 吗?我对如何正确部署应用程序感到困惑。我在 DigitalOcean 中使用 Ubuntu、NginX(1gb droplet)。

Hus*_*ain 20

检查这个: https: //gist.github.com/iam-hussain/2ecdb934a7362e979e3aa5a92b181153

HTTP/HTTPS 参考:https://gist.github.com/iam-hussain/2ecdb934a7362e979e3aa5a92b181153

在端口 8080 启动 PM2 nextJS 服务:

  • cd PROJECT_DIRECTORY
  • pm2 start "npm run start -- -p 8080" --name contractverifier

配置 Nginx:

将此文件替换为以下代码/etc/nginx/sites-available/default

    server {
        server_name www.DOMAINNAME.com DOMAINNAME.com;

        index index.html index.htm;
        root /home/ubuntu/PROJECT_FOLDER; #Make sure your using the full path

        # Serve any static assets with NGINX
        location /_next/static {
            alias /home/ubuntu/PROJECT_FOLDER/.next/static;
            add_header Cache-Control "public, max-age=3600, immutable";
        }

        location / {
            try_files $uri.html $uri/index.html # only serve html files from this dir
            @public
            @nextjs;
            add_header Cache-Control "public, max-age=3600";
        }

        location @public {
            add_header Cache-Control "public, max-age=3600";
        }

        location @nextjs {
            # reverse proxy for next server
            proxy_pass http://localhost:8080; #Don't forget to update your port number
            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;
        }

        listen 80 default_server;
        listen [::]:80;
}
Run Code Online (Sandbox Code Playgroud)


Obl*_*der 15

我设法让它发挥作用。问题出在我的 Nginx 服务器块上。我只是添加这个块

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;
    }
Run Code Online (Sandbox Code Playgroud)

然后运行

npm start
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


Tan*_*yen 7

我更喜欢pm2启动nextJs服务并Nginx发布它

pm2命令:

pm2 start yarn --name nextjs --interpreter bash -- start
pm2 show nextjs
Run Code Online (Sandbox Code Playgroud)

您可以将该配置推送到/etc/nginx/conf.d/your-file.config /etc/nginx/nginx.config

server {
    listen 80; # you can use 443 and letsencrypt to get SSL for free
    server_name dicom-interactive.com; # domain name
    access_log /var/log/dicom-interactive/access.log; # mkdir dir first
    error_log /var/log/dicom-interactive/error.log error;

    # for public asset into _next directory
    location _next/ {
        alias /srv/udemii-fe/.next/;
        expires 30d;
        access_log on;
    }

    location / {
        # reverse proxy for next server
        proxy_pass http://localhost:8000; # your nextJs service and port
        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;

        # we need to remove this 404 handling
        # because next's _next folder and own handling
        # try_files $uri $uri/ =404;
    }
}
Run Code Online (Sandbox Code Playgroud)