Giter Club home page Giter Club logo

docker-nginx-reverse-proxy-example's Introduction

Cors Nginx Proxy Example

About

This project is a simple example of how to use nginx as a proxy for a api that doesn't support CORS. My use case was:

  • I had to develop an frontend that is decoupled from the back-end, but in production they both run in the same server.
  • Since they run under the same server, there is no need for the back-end to have CORS enabled.
  • I didn't want to run the back-end locally (because it would be a pain to setup the database and all the other dependencies), so I needed a way to proxy the requests to the remote back-end hmg server.
  • I achieved this by using nginx as a proxy, and this project is an example of how to do it. I made this project because I couldn't find a complete example of how it would be done.

I will explain how it works in an article. If for some reason the link is broken, try to find it on my blog.

Details

The project is composed by 3 services:

  • nginx
  • backend (express.js)
  • frontend (next.js)

Technologies used:

The backend is just a simple CRUD made with express.js and mongodb. It was added middlewares to handle: logging, error handling, req validation, and api key validation. Most of the implementation has interfaces to base the implementation on, so it can be easily extended. There is only 1 entity: User.

The frontend has 3 pages: Home, About, Users. The Users page is the most important page on this project, since it is the page that makes the requests to the backend through the nginx proxy. The Users page has a table that shows all the users.

How to run

Requirements

All the env variables needed to run the project are in the .env.development files in the root folder of each service. You can change them if you want, but it's not necessary.

Running

First way

  • Clone this repository
  • Go to the project root folder
  • Run docker-compose --env-file .env.development -f ./docker-compose.yml --profile full up -d --build

In this way you will run all the services (nginx, api and frontend) inside docker containers.

Second way

  • Clone this repository
  • Go to the project root folder
  • Run docker-compose --env-file .env.development -f ./docker-compose.yml --profile backend-proxy up -d --build
  • Open a new terminal
  • Go to the frontend folder cd ./frontend
  • Run npm install && npm run dev

In this way you will run the nginx and the api inside docker containers, and the frontend locally.

Conclusion

I hope this project along with the article can help you to have a better understand of CORS, Nginx and Docker containers.

Contributors

License

MIT

docker-nginx-reverse-proxy-example's People

Contributors

pietrobondioli avatar

Watchers

 avatar

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.