Education
Senior Software Engineer
Last updated onJul 31, 2024
Last updated onJun 13, 2024
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.
1cd your-nextjs-project
1npm install
1npm run build
1npm 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:
1sudo apt update 2sudo apt install nginx
Next, you need to create a new Nginx configuration file for your Next.js application.
1cd /etc/nginx/sites-available
1sudo 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:
1server { 2 listen 80; 3 server_name your-domain.com; # Replace with your actual domain name 4 5 location / { 6 proxy_pass http://localhost:3000; 7 proxy_http_version 1.1; 8 proxy_set_header Upgrade $http_upgrade; 9 proxy_set_header Connection 'upgrade'; 10 proxy_set_header Host $host; 11 proxy_cache_bypass $http_upgrade; 12 } 13}
1sudo ln -s /etc/nginx/sites-available/your-nextjs-app /etc/nginx/sites-enabled/
1sudo nginx -t
1sudo systemctl restart nginx
PM2 is a process manager for Node.js applications that makes it easy to keep your app running in the background.
1sudo npm install -g pm2
1pm2 start npm --name "your-nextjs-app" -- start
To ensure your Next.js application automatically restarts if the server reboots, configure PM2 to start on boot:
1pm2 startup systemd 2pm2 save
Ensure Nginx serves your static files correctly by adding the following to your Nginx configuration file:
1server { 2 listen 80; 3 server_name your-domain.com; # Replace with your actual domain name 4 5 location / { 6 proxy_pass http://localhost:3000; 7 proxy_http_version 1.1; 8 proxy_set_header Upgrade $http_upgrade; 9 proxy_set_header Connection 'upgrade'; 10 proxy_set_header Host $host; 11 proxy_cache_bypass $http_upgrade; 12 } 13 14 location /_next/static/ { 15 alias /var/www/your-nextjs-app/out/_next/static/; 16 expires 1y; 17 access_log off; 18 } 19 20 location /static/ { 21 alias /var/www/your-nextjs-app/out/static/; 22 expires 1y; 23 access_log off; 24 } 25}
1sudo nginx -t
1sudo systemctl restart nginx
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:
1sudo tail -f /var/log/nginx/error.log
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.
Tired of manually designing screens, coding on weekends, and technical debt? Let DhiWise handle it for you!
You can build an e-commerce store, healthcare app, portfolio, blogging website, social media or admin panel right away. Use our library of 40+ pre-built free templates to create your first application using DhiWise.