8

I use nginx in the docker,this is my nginx configure

server {    listen  80;     server_name saber;

    location / {        
       root /usr/share/nginx;       
       index index.html;
}

    location /saber {       
        proxy_pass http://localhost:8080;   
        proxy_redirect off;     
        proxy_set_header Host $host;    
        proxy_set_header X-Real-IP $remote_addr;        
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;        
        proxy_buffer_size 4k;       
        proxy_buffers 4 32k;        
        proxy_busy_buffers_size 64k;    
        proxy_connect_timeout 90;   
    }

}

when I use "http://localhost/saber/blog/getBlog.do" in browser ,browser give me a error with "502". and nginx`s error.log have new.

2017/07/09 05:16:18 [warn] 5#5: *1 upstream server temporarily disabled while connecting to upstream, client: 172.17.0.1, server: saber, request: "GET /saber/blog/getBlog.do HTTP/1.1", upstream: "http://127.0.0.1:8080/saber/blog/getBlog.do", host: "localhost"

I can promise the "http://127.0.0.1:8080/saber/blog/getBlog.do" have response success in browser. I try search answer in other question,i find a answer is "/usr/sbin/setsebool httpd_can_network_connect true",this is question url "nginx proxy server localhost permission denied",but I use the docker in win10,the nginx container dont hava setsebool,because the container dont find SELinux. This all,Thank you in advance.

Z.Qng
  • 91
  • 1
  • 1
  • 2

1 Answers1

21

Localhost inside each container (like the nginx container) is different from localhost outside on your container. Each container gets its own networking namespace by default. Instead of pointing to localhost, you need to place your containers on the same docker network (not the default bridge network) and use the container or service name with Docker's built in DNS to connect. The target port will also be the container port, not the published port on your host.

BMitch
  • 231,797
  • 42
  • 475
  • 450
  • 1
    Oh,The reason I realized the problem was network .Finally,I change the localhost in "proxy_pass http://localhost:8080" to "10.2.8.158"(this is IPv4 in my computer).That's it.Thanks. – Z.Qng Jul 10 '17 at 02:25
  • 3
    That will connect you outside of docker and then back in. If you run things in a docker network, they can connect directly, container to container, which is much more portable. – BMitch Jul 10 '17 at 17:52
  • I found the solution [here](https://stackoverflow.com/questions/23948527/13-permission-denied-while-connecting-to-upstreamnginx). may work for you :-) – dinu0101 Jul 27 '19 at 19:11