简体   繁体   English

使用 NGINX 和 Docker 进行反向代理

[英]Reverse Proxy with NGINX and Docker

I have an application running on http://localhost:8000 using a docker image that was made by me.我有一个应用程序在http://localhost:8000上运行,使用我制作的 docker 镜像。

Now I want to use NGINX as reverse proxy listening on port 80, to redirect to the localhost:8000.现在我想使用 NGINX 作为侦听端口 80 的反向代理,重定向到 localhost:8000。

Here my nginx.conf file这是我的 nginx.conf 文件

#user  nobody;
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    server {
        listen 80;
        location / {
            proxy_pass http://localhost:8000;
        }
    }
}

Here my Dockerfile:这是我的 Dockerfile:

FROM nginx

RUN rm /etc/nginx/conf.d/default.conf

COPY index.html /usr/share/nginx/html
COPY nginx.conf /etc/nginx

CMD nginx

To build the image I use the command要构建图像,我使用命令

docker build --no-cache -t mynginx .

To run it, I use要运行它,我使用

docker run -p 80:80 -d mynginx 

Now, if I test from my local computer with curl localhost:8000 everything works, but if I try with curl localhost , I get a Bad Gateway error.现在,如果我使用curl localhost:8000从本地计算机进行测试,一切正常,但是如果我尝试使用curl localhost ,则会收到 Bad Gateway 错误。

Moreover, I tried to serve static content and it works, but with the reverse proxy settings it does not work.此外,我尝试提供静态内容并且它可以工作,但是使用反向代理设置它不起作用。

The reason you are getting a bad gateway is that from within your nginx container, localhost resolves to the container itself and not to the host system.你得到一个坏网关的原因是在你的 nginx 容器中, localhost解析到容器本身而不是主机系统。

If you want to access your application container from your reverse proxy container, you need to put both containers into a network.如果要从反向代理容器访问应用程序容器,则需要将两个容器放入网络。

docker network create my-network
docker network connect --alias application my-network <application container id/name>
docker network connect --alias reverse-proxy my-network <reverse proxy container id/name>

--network can be an arbitrary name and --alias should be the hostnames you want to resolve to your containers. --network可以是任意名称,-- --alias应该是您要解析到容器的主机名。 In fact you do not need to provide an alias if you already assigned a (host)name ( docker run --name ... ) to your containers.事实上,如果您已经为容器分配了(主机)名称( docker run --name ... ),则不需要提供别名。

You can then change your proxy_pass directive to the (alias) name of your application container:然后,您可以将proxy_pass指令更改为应用程序容器的(别名)名称:

proxy_pass http://application:8000;

Also refer to the documentation:Container networking另请参阅文档:容器网络

curl localhost:8000 works => your application is running curl localhost:8000有效 => 您的应用程序正在运行

curl localhost returns bad gateway means proxy_pass http://localhost:8000; curl localhost返回错误的网关意味着proxy_pass http://localhost:8000; does not work.不起作用。 This is because "localhost" is relative to its caller, in this case the nginx container, which does not have anything running on port 8000.这是因为“localhost”相对于它的调用者,在本例中是 nginx 容器,它没有在端口 8000 上运行任何东西。

You need to point it to your application using proxy_pass http://your_app:8000;您需要使用proxy_pass http://your_app:8000;将其指向您的应用程序proxy_pass http://your_app:8000; where "your_app" is the service name (docker-compose) or the container name or its network alias.其中“your_app”是服务名称(docker-compose)或容器名称或其网络别名。 Make sure they are in the same network.确保它们在同一网络中。

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

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