
Build 10x products in minutes by chatting with AI - beyond just a prototype.
Does Next.js Use Nginx?
Is Next.js Better Than React?
Is Next.js Better Than Angular?
What is Next.js Static Export?
Deploying a Next.js application with Nginx is an effective way to serve your app efficiently. Nginx acts as a powerful web server and reverse proxy, allowing you to handle static and dynamic requests seamlessly.
This guide will walk you through each step of the deployment process, ensuring you have a robust and scalable Next.js application running on your Nginx server.
Before we start, make sure you have the following:
• A Next.js application ready for deployment.
• A Linux server (e.g., Ubuntu) with sudo privileges.
• Node.js and npm installed on your server.
• A domain name pointing to your server
First, you need to build your Next.js application for production. This process generates the necessary static files and optimizes your application.
cd your-nextjs-project
npm install
npm run build
npm run export
This process creates an out folder containing the static HTML files, JavaScript files, and other assets necessary for your Next.js application.
If Nginx is not already installed on your server, you can install it using the following command:
sudo apt update sudo apt install nginx
Next, you need to create a new Nginx configuration file for your Next.js application.
cd /etc/nginx/sites-available
sudo nano your-nextjs-app
In the newly created configuration file, add the following code to set up Nginx as a reverse proxy for your Next.js application:
server { listen 80; server_name your-domain.com; # Replace with your actual domain 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 ln -s /etc/nginx/sites-available/your-nextjs-app /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx
PM2 is a process manager for Node.js applications that makes it easy to keep your app running in the background.
To ensure your Next.js application automatically restarts if the server reboots, configure PM2 to start on boot:
Ensure Nginx serves your static files correctly by adding the following to your Nginx configuration file:
Congratulations! You've successfully deployed your Next.js application with Nginx. Your Nginx server is now acting as a powerful web server and reverse proxy, handling incoming requests efficiently and serving static and dynamic content.
If you encounter any issues, check the Nginx error logs for detailed error messages:
By following this guide, you ensure your Next.js application is deployed securely and efficiently, providing a robust solution for handling web traffic and optimizing performance.
sudo npm install -g pm2pm2 start npm --name "your-nextjs-app" -- startpm2 startup systemd
pm2 saveserver {
listen 80;
server_name your-domain.com; # Replace with your actual domain 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;
}
location /_next/static/ {
alias /var/www/your-nextjs-app/out/_next/static/;
expires 1y;
access_log off;
}
location /static/ {
alias /var/www/your-nextjs-app/out/static/;
expires 1y;
access_log off;
}
}sudo nginx -tsudo systemctl restart nginxsudo tail -f /var/log/nginx/error.log