繁体   English   中英

Docker 中的 Nginx

[英]Nginx in Docker

我正在使用 Docker 容器,并希望 Nginx 提供前端资源或指向正确的后端服务,因此充当反向代理。 我该怎么做呢? 我是否需要为每个后端服务设置另一个 Nginx 服务器?

我也很困惑 Nginx 如何在 docker 中工作。 据我了解,您指定了设置到 Nginx 公共目录中的卷。 如果我提供 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由 Nginx 容器或 laravel 容器执行的计算?

这些可能是愚蠢的问题,但我现在很困惑。

要在 docker 容器中配置 Nginx,您必须在运行容器时包含 nginx.conf 文件。 要添加此文件,您可以使用-v参数。 这应该看起来像这样。

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

在您的 Nginx.conf 文件中,您可以指定 Nginx 的行为方式。 反向代理的示例可能如下所示:

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;
    }
  }
}

这会将请求重定向到 example.com/ 到 docker ip 172.17.0.1 端口 8080 上的 docker 容器的 docker 容器。

可以在此处找到配置 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

暂无
暂无

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

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