Code Monkey home page Code Monkey logo

nextjs-nginx's Introduction

Nginx example

Example of configuring nginx as a reverse proxy of nextjs.

nginx takes care of serving all the static assets, including the static generated websites (html files). The setup will compress all assets at build time to save cpu and gzip the nextjs responses on the fly, as per the nextjs recommendation since nginx is more efficient for that work than node.

How to use

clone the repo then run to check the site in development mode (without nginx):

yarn
yarn dev

create docker image that contains nginx and all the configuration:

yarn docker:image
yarn docker:start

open localhost:3000

The idea behind the example

Documentation recommends having a proxy in front of next to handle compression in a more efficient way. This example creates a docker image with nginx and the app running inside it. If you don't want to use docker you can check the nginx configuration in .docker/nginx.

Note that next.config.js has nextjs compression disabled, since it's going to be managed by nginx.

How it works

Docker image uses (s6 overlay)[https://github.com/just-containers/s6-overlay] to run nginx and nextjs in the same container. The image is self contained, you could modify it if you want to volume mount the app code.

At build time all generated static assets like js bundles and html pages are gzipped. Also since nextjs creates a random route for some of those assets (see (build_id)[https://github.com/zeit/next.js#configuring-the-build-id]), the generated BUILD_ID is passed as environment variable to nginx so it has the proper paths to the files.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.