Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(deployment): heroku with nginx #873

Merged
merged 1 commit into from
Jan 25, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions docs/content/2.deploy/providers/heroku.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,48 @@ Nitro supports deploying on [Heroku](https://heroku.com/) with minimal configura
"start": "node .output/server/index.mjs"
}
```


## With Nginx

1. Add the heroku Nginx buildpack [here](https://github.com/heroku/heroku-buildpack-nginx.git)

1. Change to the 'node' preset in your `nuxt.config`

```Json5
"nitro":{
"preset":"node",
}
```

1. From the **Existing app** section of buildpack doc, 2 key steps are required to get things running

Step 1: Listen on a socket at 'tmp/nginx.socket'
Step 2: Create a file '/tmp/app-initialized' when your app is ready to accept connections

1. Create custom app runner, eg: apprunner.mjs at the root of the project (or any other prefered location), in this file, create a server, using the listener generated by the node preset, then listen on the socket as detailed in the buildpack doc

```Js
import { createServer } from 'node:http'
import {listener} from './.output/server/index.mjs'
const server = createServer(listener)
server.listen('/tmp/nginx.socket') //following the buildpack doc
```

1. To create the 'tmp/app-initialized' file, use a nitro plugin, create file 'initServer.ts' at the root of the project (or any other prefered location)

```Js
import fs from "fs"
export default defineNitroPlugin((nitroApp) => {
if((process.env.NODE_ENV || 'development') != 'development')fs.openSync('/tmp/app-initialized', 'w')
})
```

1. Finally, create file 'Procfile' at the root of the project, with the Procfile, we tell heroku to start nginx and use the custom apprunner.mjs to start the server

web: bin/start-nginx node apprunner.mjs

1. Bonus: create file 'config/nginx.conf.erb' to customize your nginx config. With the node preset, by default, static files handlers will not be generated, you can use nginx to server static files, just add the right location rule to the server block(s), or, force the node preset to generate handlers for the static files by setting serveStatic to true