#How Do I Serve My Site With Nginx?

59 messages ยท Page 1 of 1 (latest)

narrow pine
#

I ran npm run build; and the output is confusing. there is no index.html...it's in build/server/pages/index.html. How do I serve this?

ivory sentinelBOT
#

๐Ÿ”Ž This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

๐Ÿ•ต๏ธ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

โœ… You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

narrow pine
#

in next.config.js, I have output: 'export', distDir: 'build', trailingSlash: true. There is build/{BUILD_ID,build-manifest.json,cache/,export-marker.json,images-manifest.json,server,static} and lots more JSON files.

#

In build/server, I see middleware-build-manifest.js, middleware*.js, middleware-manifest.json, font-manifest.json, chunks/, and some others.

supple crow
#

Are you hosting a static site? You would serve your site using nginx typically by running

npm run build
then
npm run start

narrow pine
#

I'm not sure what a static site is. The javascript, HTML, CSS, etc. don't change, but the site fetches data from a REST server.
I don't think npm run start is the right way because it will run webpack dev server (maybe) or some other server, not nginx.

supple crow
#

nginx is a service on the box that serves as your ssl reverse proxy

#

I use nginx in my production environment

#

on the box you should be able to do systemctl status nginx

narrow pine
#

I see what you're saying. I would like to serve a static site without using a reverse proxy to a Next.js server.

supple crow
#

Then you want to follow this

narrow pine
#

Reading!

#

After running next build, Next.js will generate the following files
/out/index.html
...
This is not my experience. As mentioned above, I see:
build/{BUILD_ID,build-manifest.json,cache/,export-marker.json,images-manifest.json,server,static} and lots more JSON files.
(build, not out because in the configuration, I have distDir: 'build'.) No index.html.

supple crow
#

did you follow this part in your next.config.js?

  output: 'export',
 
  // Optional: Change links `/me` -> `/me/` and emit `/me.html` -> `/me/index.html`
  // trailingSlash: true,
 
  // Optional: Prevent automatic `/me` -> `/me/`, instead preserve `href`
  // skipTrailingSlashRedirect: true,
 
  // Optional: Change the output directory `out` -> `dist`
  // distDir: 'dist',
}
narrow pine
#

Yes. As mentioned above, my config looks like this: output: 'export', distDir: 'build', trailingSlash: true.

supple crow
#

Okay. one sec, let me test it locally

#

I set it up just like you and I have a index.html inside the build folder

#

I have a meeting I gotta get to, will be a min before I can respond

#

Oh are you using pages dir?

#

๐Ÿ˜ฆ

narrow pine
#

No sweat, I'm not in a hurry.

#
build
โ”œโ”€โ”€ BUILD_ID
โ”œโ”€โ”€ build-manifest.json
โ”œโ”€โ”€ cache
โ”‚ย ย  โ”œโ”€โ”€ config.json
โ”‚ย ย  โ”œโ”€โ”€ next-server.js.nft.json
โ”‚ย ย  โ”œโ”€โ”€ swc
โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ plugins
โ”‚ย ย  โ”‚ย ย      โ””โ”€โ”€ v3
โ”‚ย ย  โ””โ”€โ”€ webpack
โ”‚ย ย      โ”œโ”€โ”€ client-production
โ”‚ย ย      โ”‚ย ย  โ”œโ”€โ”€ 0.pack
โ”‚ย ย      โ”‚ย ย  โ””โ”€โ”€ index.pack
โ”‚ย ย      โ””โ”€โ”€ server-production
โ”‚ย ย          โ”œโ”€โ”€ 0.pack
โ”‚ย ย          โ””โ”€โ”€ index.pack
โ”œโ”€โ”€ export-marker.json
โ”œโ”€โ”€ ... json files
โ”œโ”€โ”€ server
โ”‚ย ย  โ”œโ”€โ”€ chunks
โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ 491.js
โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ 582.js
โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ 656.js
โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ 664.js
โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ 676.js
โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ font-manifest.json
โ”‚ย ย  โ”œโ”€โ”€ font-manifest.json
โ”‚ย ย  โ”œโ”€โ”€ middleware-build-manifest.js
โ”‚ย ย  โ”œโ”€โ”€ ...
โ”‚ย ย  โ”œโ”€โ”€ pages
โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ 404.html
โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ 500.html
โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ admin
โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ open.html
โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ open.js.nft.json
โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ _app.js
โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ _app.js.nft.json
โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ _document.js
โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ _document.js.nft.json
โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ _error.js
โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ _error.js.nft.json
โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ index.html
โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ index.js.nft.json
โ”‚ย ย  โ”œโ”€โ”€ pages-manifest.json
โ”‚ย ย  โ””โ”€โ”€ webpack-runtime.js
โ”œโ”€โ”€ static
โ”‚ย ย  โ”œโ”€โ”€ 2sku5TNySjGyDuFk2_Epn
โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ _buildManifest.js
โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ _ssgManifest.js
โ”‚ย ย  โ”œโ”€โ”€ chunks
โ”‚ย ย  โ”‚ย ย  ...
โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ main-be43d357da86e0af.js
โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ pages
โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ admin
โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ open-9e8231543f6df1ed.js
โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ _app-5fb3e62960cc86ec.js
โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ _error-a4ba2246ff8fb532.js
โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ index-91ca2e3e527e8844.js
โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ polyfills-c67a75d1b6f99dc8.js
โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ webpack-14a891bf35ecde1d.js
โ”‚ย ย  โ””โ”€โ”€ css
โ”‚ย ย      ...
โ”‚ย ย      โ””โ”€โ”€ f2d4fdccce5c1d99.css
โ””โ”€โ”€ trace
#

I had to trim because Discord won't let me send the whole thing.

#

I do have pages/ , it's just in the wrong spot.

narrow pine
#

Oh are you using pages dir?
I had read that as, "do you have a pages dir [in the output directory]?", and thought I answered it. In the repository is a pages/ with several tsx files: index.tsx, login.tsx, _app.tsx, etc. and some .scss files.

supple crow
#

You have middleware in your project? Its unsupported.

#
/** @type {import('next').NextConfig} */
const nextConfig = {
    output: 'export',
    trailingSlash: true,
    distDir: 'build',
  };

export default nextConfig;

This is what your next.config.mjs should look like.

#

In your nextjs project are you using page router or app router? I was testing using the app router.

#

@narrow pine

narrow pine
#

good question!

supple crow
#

If your using the 'page router' im going to have a little more difficulty helping lol I dont have any test projects using the pages router as its I would say obsolete, though still supported.

narrow pine
#

No sweat

supple crow
#

With all that being said, your static files are just

โ”‚   โ”œโ”€โ”€ pages
โ”‚   โ”‚   โ”œโ”€โ”€ 404.html
โ”‚   โ”‚   โ”œโ”€โ”€ 500.html
โ”‚   โ”‚   โ”œโ”€โ”€ admin
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ open.html
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ open.js.nft.json
โ”‚   โ”‚   โ”œโ”€โ”€ _app.js
โ”‚   โ”‚   โ”œโ”€โ”€ _app.js.nft.json
โ”‚   โ”‚   โ”œโ”€โ”€ _document.js
โ”‚   โ”‚   โ”œโ”€โ”€ _document.js.nft.json
โ”‚   โ”‚   โ”œโ”€โ”€ _error.js
โ”‚   โ”‚   โ”œโ”€โ”€ _error.js.nft.json
โ”‚   โ”‚   โ”œโ”€โ”€ index.html

Slap everything in the pages dir and I think you would have your static site.

narrow pine
#

I have a lot of import { useRouter } from "next/router"; does that answer the question?

supple crow
#

Negative. What does this look like on your side

#

mine says app, does yours say pages?

narrow pine
#

Yes, I have pages.

#

I don't have src.

supple crow
#

Yeah thats fine, thats optional for organization

supple crow
#

You can disregard all the other stuff I think

narrow pine
#

I tried something like rm -r *.json ...etc...; mv server/pages/* . and serve that. I got the index.html, but no CSS, and nothing worked.

supple crow
#

Gotcha.... is your project have any secrets in it? If you can share it and I can build on locally I might be able to help I just dont have any projects that use the page router.

#

You can sanitize your project if you want as well, and send that

narrow pine
#

Sorry, bank account woes distracting me! The project is sensitive...

#

I have another project that uses page routing and serves static assets after npm run build. I have been having difficulty finding what the difference between that project and this one is. I think I'll dig deeper in that direction.

supple crow
#

It almost looks like its still trying to build the project normally like it would in the .next folder. Can you verify there is nothing else in next.config.mjs and looks exactly like this?

const nextConfig = {
    output: 'export',
    trailingSlash: true,
    distDir: 'build',
  };

export default nextConfig;
narrow pine
#

Thank you for checking back, and sorry for the delay! I had module.exports = {... I changed to exactly what you have, and I get, "SyntaxError: Unexpected token 'export'" when building.

#

If I use /** @type {import('next').NextConfig} */module.exports={output:'export',trailingSlash:true,distDir:'build'}; I get the same problematic build directory.

#

(that's just an adjustment of your example.)

#

WHOAA!! I read the output of npm run build more carefully:
warn - Invalid next.config.js options detected:
- The value at .output must be one of: "standalone".
Reading!!

#

package.json has "next": "^12.0.7"...this app is like three months old...why on earth?

narrow pine
#

Okay, we'll update this and see where it lands. I assume that will fix everything. @supple crow u da man.

narrow pine
#

Yeah, I didn't set this one up... ๐Ÿคท๐Ÿปโ€โ™‚๏ธ

supple crow
#

๐Ÿ˜‚ I guess that should have been my question really early on is what version of nextjs you running xD

#

rip