Home > Software engineering >  nginx container proxy_pass to another docker container
nginx container proxy_pass to another docker container

Time:12-22

nginx.conf:

user  nginx;
worker_processes  auto;
error_log  /dev/null;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
    multi_accept on;
    use epoll;
}
http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    sendfile on;
    log_format compression '$remote_addr - $remote_user [$time_local] '
                           '"$request" $status $body_bytes_sent '
                           '"$http_referer" "$http_user_agent" "$gzip_ratio"';
    access_log off;
    error_log /dev/null;
    log_not_found off;

    server_tokens off;
    server_names_hash_bucket_size 64;
    server_names_hash_max_size 2048;
    server_name_in_redirect off;

    client_body_timeout   300s;
    client_header_timeout 300s;
    keepalive_timeout     0;
    send_timeout          300s;

    client_body_buffer_size 64m;
    client_header_buffer_size 2m;
    client_max_body_size 48m;
    proxy_connect_timeout 70s;
    proxy_send_timeout 90s;
    proxy_read_timeout 90s;
    large_client_header_buffers 4 8k;

    ##
    # Gzip Settings
    ##
    gzip on;
    gzip_http_version 1.1;
    gzip_disable "msie6";
    gzip_static on;
    gzip_vary on; #vary header
    gzip_min_length 0; # compress though it's volume
    gzip_proxied any;
    gzip_comp_level 3; #low performance server: 2 and good server: 6
    gzip_buffers 16 8k;
    gzip_types text/plain text/css text/javascript text/xml application/javascript application/json application/x-javascript application/xml application/xml rss image/svg xml image/svg;
    limit_conn_zone $binary_remote_addr zone=ddos_conn:10m;
    limit_req_zone $binary_remote_addr zone=ddos_req:10m rate=4r/s;
    limit_req_status 500;
    server {
        listen       8010;
        server_name  0.0.0.0;
        client_max_body_size 48m;
        client_body_timeout   5s;
        client_header_timeout 5s;
        location / {
            proxy_pass http://0.0.0.0:8002;
            proxy_http_version  1.1;
            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_set_header   X-NginX-Proxy true;
            #proxy_ignore_client_abort  on; 
            proxy_redirect     off;
            proxy_connect_timeout 70s;
            proxy_send_timeout 90s;
            proxy_read_timeout 90s;
            keepalive_timeout 90;
            limit_req zone=ddos_req burst=4 nodelay;
            limit_conn ddos_conn 8;
        }
    }

    server {
        listen       8011;
        location / {
            return 200 'pong';
            keepalive_timeout 90;
        }
    }
}

and commands:

for nginx container:

docker run --name nginx_con -d -p 0.0.0.0:8011:8011 -p 0.0.0.0:8010:8010 --restart always -v /Users/mac/docker/conf_files/nginx.conf:/etc/nginx/nginx.conf:ro -v /Users/mac/docker/conf_files/error_log:/var/log/nginx/error_log -v /Users/mac/dcoker/conf_files/access_log:/var/log/nginx/access_log nginx:1.15.8-alpine

for api container:

sudo docker run --name was_con -d -p 0.0.0.0:8002:8002 --net=host --restart always -e MODE='deploy' -v /Users/mac/docker/conf_files/uwsgi.ini:/uwsgi.ini apiaccount/apiproject:latest

When I go to url 0.0.0.0:8011, It works. (returns 'pong' well)

and I go to url 0.0.0.0:8002 It works well

but I go to url 0.0.0.0:8010 It not works.

return 502 bad gateway.

How to solve it?

I'm running it on mac OS..

CodePudding user response:

nginx can't find 0.0.0.0:8002 in docker. You have to provide the ip address or dns name of the api container in docker. so e.g. proxy_pass http://was_con:8002. The IP you can find with the docker inspect was_con command

"Networks": {
                "name": {
                    IPAddress": "172.18.0.9"

But you're running the api on host network, then you need the host IP as seen from docker, default is 172.17.0.1

  • Related