I’m currently facing an issue while deploying a Next.js application on an AWS EC2 instance running Ubuntu. The application runs fine, and the main logic is working as expected. However, images (JPG, PNG, GIF) and styles (CSS) are not loading properly. Instead, I see broken images and unstyled content on the deployed site.
enter image description here
Deployment Steps:
Launched an EC2 instance (Ubuntu 22.04).
Installed Node.js, NPM, and PM2 to run the Next.js server.
Cloned the project repository from GitHub.
Ran npm install to install dependencies.
Built the app using npm run build
.
Started the server using npx next start
or pm2 start npm --name "next-app" -- run start
.
Checked File Permissions:
Verified that the image and CSS files exist in the .next/static folder.
Used chmod -R 755 .next/
to ensure files have read permissions.
Checked NGINX (if applicable):
Configured an NGINX reverse proxy.
Ensured location /_next/static/
is pointing to the correct path.
Environment Variables:
Double-checked the environment variables used in the next.config.js, especially assetPrefix
and basePath.
Current Behavior:
Images and GIFs are broken (not loaded) in the browser.
CSS styles are not being applied, and the site appears unstyled.
What I’ve Tried to Debug:
Network Requests:
Checked network requests in the browser dev tools. The request URLs for images and styles are returning 404 errors.
Next.js Config:
Verified that next.config.js does not have any misconfigurations related to basePath or assetPrefix.
Build Verification:
Verified that the .next/static folder contains the expected images, styles, and JavaScript files.
Logs:
No errors are appearing in the server logs.
Questions:
Are there any specific settings for AWS EC2 (like file permissions) that could be blocking access to .next/static files?
Should I explicitly configure assetPrefix in next.config.js when using an EC2 instance?
How do I ensure that the files in .next/static/ are served correctly?
Environment Details:
Node.js version: v18.x
Next.js version: 13.x
OS: Ubuntu 22.04
Server: AWS EC2 (Ubuntu) with NGINX (if relevant)
Any help would be greatly appreciated!
Aayanshi Sharma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2