乐闻世界logo
搜索文章和话题

How to deploy NextJS with NGINX?

1个答案

1

Deploying a Next.js application to NGINX involves several key steps, primarily including building the application, configuring NGINX, and maintenance and monitoring. Here are the detailed steps:

1. Building the Next.js Application

First, ensure that your Next.js application has been developed and can run locally. Next, execute the build command to prepare the application for production.

shell
pm run build

This command creates a .next folder containing optimized files for production.

2. Preparing the Production Server

  • Installing Node.js: Ensure that your production server has Node.js installed, as Next.js is a Node.js framework.
  • Installing PM2: It is recommended to use PM2 to manage your Node.js application, as it helps manage logs, monitor the application, and automatically restart it after crashes.
shell
pm install pm2 -g
  • Starting the Application with PM2:
shell

3. Configuring NGINX

  • Installing NGINX: Ensure that NGINX is installed on the server.
  • Configuring Reverse Proxy: Edit the NGINX configuration file (typically located at /etc/nginx/sites-available/default), setting up a reverse proxy to forward requests from NGINX to your Next.js application.
nginx
server { listen 80; server_name your-domain.com; location / { proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_set_header X-NginX-Proxy true; proxy_pass http://localhost:3000; proxy_redirect off; } }
  • Restarting NGINX:
shell
sudo systemctl restart nginx

4. Maintenance and Monitoring

  • Monitoring the Application: Use PM2's monitoring features to view the application's performance and logs.
  • SSL Configuration: For security, it is recommended to configure SSL for your website using Let's Encrypt.

These steps cover the entire process from application building to deployment, ensuring stable operation in the production environment. I hope this information is helpful to you. If you have any questions, I am happy to discuss further.

2024年6月29日 12:07 回复

你的答案