简体   繁体   English

Docker 中的 Nginx

[英]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.我正在使用 Docker 容器,并希望 Nginx 提供前端资源或指向正确的后端服务,因此充当反向代理。 How do I do this?我该怎么做呢? Do I need to setup another Nginx server for each one of the backend services?我是否需要为每个后端服务设置另一个 Nginx 服务器?

Also I am confused as to how Nginx works in docker.我也很困惑 Nginx 如何在 docker 中工作。 As far as I understand you specify volumes which get set into Nginx public directory.据我了解,您指定了设置到 Nginx 公共目录中的卷。 If I am serving up static resources that isn't a problem.如果我提供 static 资源,这不是问题。 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? 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由 Nginx 容器或 laravel 容器执行的计算?

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.要在 docker 容器中配置 Nginx,您必须在运行容器时包含 nginx.conf 文件。 To add this file you can use the -v argument.要添加此文件,您可以使用-v参数。 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.在您的 Nginx.conf 文件中,您可以指定 Nginx 的行为方式。 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.这会将请求重定向到 example.com/ 到 docker ip 172.17.0.1 端口 8080 上的 docker 容器的 docker 容器。

The full description for configuring an Nginx file can be found here: https://www.nginx.com/resources/wiki/start/topics/examples/full/可以在此处找到配置 Nginx 文件的完整说明: https://www.nginx.com/resources/wiki/start/topics/examples

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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