2

Context: I have a simple Next.js and KeystoneJS app. I've made duplicate deployments on 2 AWS EC2 instances. Each instance also has an Nginx reverse proxy routing port 80 to 3000 (my apps port). The 2 instances are also behind an application load balancer.

Problem: When routing to my default url, my application attempts to fetch the buildManifest for the nextjs application. This, however, 404s most of the time.

My Guess: Because the requests are coming in so close together, my load balancer is routing the second request for the buildManifest to the other instance. Since I did a separate yarn build on that instance, the build ids are different, and therefore it is not fetching the correct build. This request 404s and my site is broken.

My Question: Is there a way to ensure that all requests made from instance A get routed to instance A? Or is there a better way to do my builds on each instance such that their ids are the same? Is this a use case for Docker?

Jamie S
  • 2,029
  • 2
  • 13
  • 19

2 Answers2

2

I have had a similar problem with our load balancer and specifying a custom build id seems to have fixed it. Here's the dedicated issue and this is how my next.config.js looks like now:

const execSync = require("child_process").execSync;

const lastCommitCommand = "git rev-parse HEAD";

module.exports = {
  async generateBuildId() {
    return execSync(lastCommitCommand).toString().trim();
  },
};
Stefano
  • 21
  • 2
1

If you are using a custom build directory in your next.config.js file, then remove it and use the default build directory.

Like:

distDir: "build"

Remove the above line from your next.config.js file.

Credits: https://github.com/serverless-nextjs/serverless-next.js/issues/467#issuecomment-726227243

Usama Majid
  • 1,103
  • 13
  • 13