在Docker上创buildNGINX逆向代理并向其他容器中的网站发送请求时出错

我目前正在尝试使用docker创build一个NGINX反向代理服务器,它将接受来自Web的代理服务器的请求,然后将请求redirect到一个容器,并与我想要在线的网站连接。 所以我目前有2个容器,一个用于Nginx的反向代理,另一个包含Nginx的LEMP和我的网站文件。 出于某种原因,我不断得到一个502错误的网关错误。 我已经单独testing了网站容器(直接连接到网站而不是通过代理),网站正确显示。 另外,我使用'wget'来testingterminal的连通性,而且网站容器从任何地方连接正确,但是当试图使用'wget'来访问Nginx代理容器时,它也显示502错误的网关错误。 我对web dev很新,所以我很感激任何帮助,因为我可能只是错过了一些简单的东西。

Nginx反向代理configuration文件

server { listen 80 default_server; listen [::]:80 default_server; # include snippets/snakeoil.conf; root /var/www/html; index index.html index.php index.htm index.nginx-debian.html; server_name 138.197.67.44; location / { # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. try_files $uri $uri/ =404; proxy_set_header X-Real-IP $remote_addr; proxy_pass http://172.17.0.2:80; } } 

反向代理的DockerFile

 ############################################################# #Dockerfile for creating and configuring a Nginx Reverse Proxy #Based in Ubuntu 16.04 ############################################################## #Set base image as existing image with nginx configuration (one shown above) FROM nginxproxyr #FileName + Author MAINTAINER NginxProxy Javier # Update the repository sources lsit RUN apt-get update #NOTE EVERYTHING IS ALREADY INSTALLED IN BASE IMAGE USED; just required to configure the image #EXPOSE AND ASSIGN PORTS #Expose default port EXPOSE 80 #Give access to the system to reach the script that runs the entrypoint RUN chmod +x /usr/bin/startup.sh #Default port to excecute entrypoint CMD service nginx start 

从Dockerfile创build的映像运行容器的命令

 sudo docker run -it -p 80:80 -d NginxReverseProxy 

当你有两个容器时,我build议你使用docker-compose并通过服务而不是通过ip来引用。 现在,如果你想redirect,build议使用这个很简单的图像来看看它是如何工作的。

https://github.com/jwilder/nginx-proxy