简体   繁体   中英

Nginx in Docker

I am using Docker containers and would like to have Nginx serve frontend resources or else point to the right backend service therefore acting as a reverse proxy. How do I do this? Do I need to setup another Nginx server for each one of the backend services?

Also I am confused as to how Nginx works in docker. As far as I understand you specify volumes which get set into Nginx public directory. If I am serving up static resources that isn't a problem. If however I download all the php dependencies and necessary dependencies to run my app in a seperate container (call it laravel container) and then reference those files in the Nginx public directory, how is Nginx then able to run this code and is all the backend computation carried out by the Nginx container or the laravel container?

These are probably silly questions, but I am very confused at the moment.

To configure Nginx in a docker container, you will have to include a nginx.conf file when running your container. To add this file you can use the -v argument. This should look something like this.

docker run -d -v $PWD/nginx.conf:/etc/nginx/nginx.conf nginx

In your Nginx.conf file you can specify how Nginx should behave. An example for an reverse proxy could look like this:

events {
  worker_connections  4096;  ## Default: 1024
}

http {
  server {
    listen          80;
    listen          [::]:80;
    server_name     example.com;

    location / {
      proxy_pass http://172.17.0.1:8080;
    }
  }
}

This redirect the requests to example.com/ to the docker container with the docker ip 172.17.0.1 on port 8080.

The full description for configuring an Nginx file can be found here: https://www.nginx.com/resources/wiki/start/topics/examples/full/

Also the Nginx page on the docker hub can help you get started on serving static content: https://hub.docker.com/_/nginx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM