Hello,
I have a static NextJS blog that I would like to host on the subdirectory of my main site. Like this: example.com/blog
.
I’ve tried uploading the files and creating a location block like this:
location /blog {
root /var/www;
}
// The inital blog page loads but the dynamic routes don't
However this doesn’t work when I try to visit something like exmaple.com/blog/post/post-title-here
.
How could I configure this, I’m pretty new to managing servers so apologies if this is an easy question.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Accepted Answer
Hi there,
With Next.js, you need to start your node service on a specific port like 3000 for example with npm run
. And then you could use Nginx as a reverse proxy to that port with the following configuration:
location /blog {
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;
}
Here is a good article that shows how to set up a server for Node.js application:
Let me know how this goes. Regards, Bobby
Hi @evalo01,
You’ve almost got it!
You can try and edit it to look like so:
location /blog {
alias /var/www;
index index.html index.htm;
try_files $uri $uri/ /blog;
}
That should be enough. That’s at least for projects where you can use Nginx’s port.
In the case of NextJS, your application is running with NodeJS which runs on specific port. You can take @bobbyiliev’s advice and set a reverse proxy location in your Nginx configuration:
location /blog {
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;
}
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.