Configure NGINX to host my Next.js application on Ubuntu?

itsbambi asked this question 21 days ago
itsbambi 21 days ago

I'm trying to deploy my Next.js application to a cloud server running the Ubuntu 20.04 operating system.

On my server, the Next.js application is running on localhost:3000.

How do I configure NGINX to host that application? I need the correct code for the configuration file inside the /etc/nginx/sites-available directory.

1 suggested answers
looper003 12 days ago

You want to configure NGINX to run your application using a reverse proxy.

Open your configuration file in the /etc/nginx/sites-available directory and update the location {} section to this:

. . .

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;

. . .

This tells NGINX to serve your Next.js application whenever a request is made to the URL you have configured for your website. If your application is running on a different port, change 3000 to whatever that port is.

When you're done making that change, test your NGINX configuration for errors:

sudo nginx -t

And then restart NGINX to put the changes into effect:

sudo systemctl restart nginx
0 replies