我在使用Nginx反向代理(docker容器)到angularjs docker容器进行端口转发时遇到困难。这是我使用的命令的Dockerfile和Nginx配置。
Dockerfile:
FROM nginx:latest
COPY nginx-reverseproxy.conf /etc/nginx/conf.d/
RUN mkdir /etc/nginx/ssl
COPY chained.crt /etc/nginx/ssl
COPY private.key /etc/nginx/ssl
RUN chown -R root:root /etc/nginx/ssl
RUN chmod -R 644 /etc/nginx/ssl
EXPOSE 80
EXPOSE 443
Nginx配置:
server {
listen 80;
listen 443 ssl;
server_name mywebserver.com;
if ($scheme = http) {
return 301 https://$server_name$request_uri;
}
ssl on;
ssl_certificate /etc/nginx/ssl/chained.crt
ssl_certificate_key /etc/nginx/ssl/private.key
location / {
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://127.0.0.1:4200;
proxy_read_timeout 180;
proxy_redirect off;
proxy_redirect http://127.0.0.1:4200 $scheme://mywebserver.com;
proxy_http_version 1.1;
proxy_request_buffering off;
}
}
创建Nginx容器的命令:
docker run -d -p 80:80 -p 443:443 --name "container-nginx" nginx-reverse-proxy-image:latest
创建myserver.com容器的命令:
docker run -d -p 4200:4200 --name "container-mywebserver" angularjs-image:latest ng serve --host 0.0.0.0 --disable-host-header
浏览时出现“502 Bad Gateway”错误消息。但是,当我 curl localhost:4200时,我可以看到内容。任何帮助,将不胜感激。
最佳答案
nginx和您的 Angular 应用程序服务器都是不同的容器,因此无法使用127.0.0.1从nginx容器调用 Angular 应用程序容器。您缺少链接它们两者的链接,请使用链接&然后在nginx中进行代理传递。以下是解决此问题的方法-
angular
应用程序- proxy_pass http://angular:4200;
proxy_redirect http://angular:4200 $scheme://mywebserver.com;
$ docker run -d -p 4200:4200 --name "angular" angularjs-image:latest ng serve --host 0.0.0.0 --disable-host-header
angular
解析为应用程序容器IP-$ docker run -d -p 80:80 -p 443:443 --link angular:angular --name "container-nginx" nginx-reverse-proxy-image:latest
关于docker - 对Docker容器应用程序使用Docker Nginx反向代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48350110/