Skip to content

Instantly share code, notes, and snippets.

@michalstala
Last active September 30, 2018 20:47
Show Gist options
  • Save michalstala/3a39b8e66879042c180c7a15d7d5087f to your computer and use it in GitHub Desktop.
Save michalstala/3a39b8e66879042c180c7a15d7d5087f to your computer and use it in GitHub Desktop.
Configure nginx as reverse proxy to containerized Docker applications

The best practice is to run one worker process per core - it is defined by worker_processes directive. To verify how many cores is your machine running execute the grep processor /proc/cpuinfo | wc -l command. The worker_connections however specify how many simultaneous connections can be opened by a worker process (default value for nginx is 512).

Each docker aplications are defined in upstream directives. The request with URI /app1/ will be proxied to docker-app1, however the request with URI /app2/ will be proxied to docker-app2.

worker_processes 1;

events { 

    worker_connections 1024; 
    
}

http {

    sendfile on;

    upstream docker-app1 {
        server 127.0.0.1:9001;
    }

    upstream docker-app2 {
        server 127.0.0.1:9002;
    }

    server {
        listen 80;
        server_name example.com;

        location /app1/ {
            proxy_pass         http://docker-app1;
            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_set_header   X-Forwarded-Host $server_name;
        }
        
        location /app2/ {
            proxy_pass         http://docker-app2;
            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_set_header   X-Forwarded-Host $server_name;
        }
    }

}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment