Nginx作为docker容器内的反向代理

Nginx作为docker容器内的反向代理,docker,nginx,docker-compose,nginx-reverse-proxy,Docker,Nginx,Docker Compose,Nginx Reverse Proxy,我试图使用nginx作为指向不同PHP应用程序容器的容器内的反向代理。 我的PHP容器从外部端口8080获取请求,并将其转发到内部端口8080。我希望我的nginx能够监听端口80,并将请求转发到端口8080上的PHP容器,但重定向请求时遇到问题 我的nginx Dockerfile: FROM nginx:latest COPY default.conf /etc/nginx/conf.d/default.conf My nginx default.conf: server { list

我试图使用nginx作为指向不同PHP应用程序容器的容器内的反向代理。 我的PHP容器从外部端口8080获取请求,并将其转发到内部端口8080。我希望我的nginx能够监听端口80,并将请求转发到端口8080上的PHP容器,但重定向请求时遇到问题

我的nginx Dockerfile:

FROM nginx:latest
COPY default.conf /etc/nginx/conf.d/default.conf
My nginx default.conf:

server {
  listen 80;
  error_page 497 http://$host:80$request_uri;
  client_max_body_size        32M;
  underscores_in_headers      on;

  location / {
    proxy_set_header    X-Forwarded-Host   $host; 
    proxy_set_header    X-Forwarded-Server $host; 
    proxy_set_header    X-Forwarded-For    $proxy_add_x_forwarded_for; 
    proxy_set_header    X-Forwarded-Proto  $scheme; 
    proxy_set_header    X-Real-IP          $remote_addr; 
    proxy_set_header    Host               $host;

    proxy_pass          http://php-container:8080;
    proxy_read_timeout  90;
    proxy_http_version  1.1;
    }
}
我已经尝试通过docker compose和上面的yml文件部署它,但是在CURL到nginx时遇到了相同的错误。

当CURL指向HTTP://localhost:8080(PHP应用程序)和HTTP://localhost:80(nginx)时,docker compose日志会输出一个日志。

但是当CURL到nginx时,我得到了上面的错误:

您的配置有误

nginx(主机:80->容器:8080)

php应用程序(主机:8080->容器:80)

Nginx无法访问另一个容器的“localhost”,因为它是另一个容器网络

我建议您创建一个docker网络
--network
,并将两个容器都放置到网络中。然后在Nginx配置中,您可以按名称引用php应用程序容器

    proxy_read_timeout      90;
    proxy_redirect          http://localhost:80/ http://php-container:8080/;

此外,您只能公开Nginx端口,您的后端将是安全的。

您的配置有误

nginx(主机:80->容器:8080)

php应用程序(主机:8080->容器:80)

Nginx无法访问另一个容器的“localhost”,因为它是另一个容器网络

我建议您创建一个docker网络
--network
,并将两个容器都放置到网络中。然后在Nginx配置中,您可以按名称引用php应用程序容器

    proxy_read_timeout      90;
    proxy_redirect          http://localhost:80/ http://php-container:8080/;

此外,您只能公开Nginx端口,您的后端将是安全的。

我已更新default.conf以重定向到php容器,而不是localhost,并且还公开了Nginx的端口,但是当CURL到我的localhost:80时仍然出现相同的错误。我更新了default.conf以重定向到php容器而不是localhost,并且还公开了nginx的端口,但是当CURL到我的localhost:80时仍然出现相同的错误。