user                    root root;
worker_processes        16;
worker_rlimit_nofile    8192;
 
events {
  worker_connections    4096;
}

http {
    include       /opt/local/etc/nginx/mime.types;
    default_type  application/octet-stream;

    
    # When the back-end server returns a 502, look to the next upstream server
    proxy_next_upstream error timeout http_502;

    # Limit requests per second per remote address
    limit_req_zone  $binary_remote_addr  zone=addr_api:10m      rate=20r/s;
    limit_req_log_level error;

		proxy_connect_timeout 1s;

    ##################
    ##################
    ## GLOBAL ADMIN ##
    ##################
    ##################
    
    ####################
    ## LOAD BALANCING ##
    ####################

    upstream globaladminworkers {
                server 10.112.5.119:2000;
                server 10.112.6.140:2000;
                server 10.112.2.120:2000;
                server 10.112.6.226:2000;
            }
    
    server {
        listen       80;
                server_name  admin.mysite.org;

        gzip on;
        gzip_min_length 1000;
        gzip_http_version 1.1;
        gzip_comp_level 5;
        gzip_proxied any;
        gzip_types text/css text/plain text/xml application/xml application/xml+rss text/javascript application/x-javascript application/json;

        ##################
        ## STATIC FILES ##
        ##################
        
        ## Admin UI
        
        rewrite ^/$              /admin/index.html last;
        rewrite ^/tenant/(.*)$   /admin/index.html last;
        
        location /admin/ {
            alias /opt/3akai-ux/admin/;
            autoindex off;
            expires max;
        }
        
        ## Shared Resources
        
        location /shared/ {
            alias /opt/3akai-ux/shared/;
            autoindex off;
            expires max;
        }
        
        ## Main UI
        
        location /ui/ {
            alias /opt/3akai-ux/ui/;
            autoindex off;
            expires max;
        }

        #########################
        ## APP SERVER REQUESTS ##
        #########################

        location /api/ui/staticbatch {
            expires max;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_set_header X-NginX-Proxy true;
            proxy_pass http://globaladminworkers;
            proxy_redirect off;
        }
    
        location /api/config {
            expires 15m;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_set_header X-NginX-Proxy true;
            proxy_pass http://globaladminworkers;
            proxy_redirect off;
        }
        
        location /api/ui/widgets {
            expires 15m;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_set_header X-NginX-Proxy true;
            proxy_pass http://globaladminworkers;
            proxy_redirect off;
        }

        location /api/ {
            limit_req zone=addr_api burst=60;

            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_set_header X-NginX-Proxy true;
            proxy_pass http://globaladminworkers;
            proxy_redirect off;
        }
    }

    ###################
    ###################
    ## TENANT SERVER ##
    ###################
    ###################

    ####################
    ## LOAD BALANCING ##
    ####################

    upstream tenantworkers {
                server 10.112.5.119:2001 max_fails=3 fail_timeout=30s;
                server 10.112.6.140:2001 max_fails=3 fail_timeout=30s;
                server 10.112.2.120:2001 max_fails=3 fail_timeout=30s;
                server 10.112.6.226:2001 max_fails=3 fail_timeout=30s;
    }

    server {
        listen   80 default_server;
        
        gzip on;
        gzip_min_length 1000;
        gzip_http_version 1.1;
        gzip_comp_level 5;
        gzip_proxied any;
        gzip_types text/css text/plain text/xml application/xml application/xml+rss text/javascript application/x-javascript application/json;

        ##################
        ## STATIC FILES ##
        ##################

        ## Shared Resources
        
        location /shared/ {
            alias /opt/3akai-ux/shared/;
            autoindex off;
            expires max;
        }
        
        ## Main UI
        
        location /ui/ {
            alias /opt/3akai-ux/ui/;
            autoindex off;
            expires max;
        }

        location /node_modules/ {
            alias /opt/3akai-ux/node_modules/;
            autoindex off;
            expires max;
        }
        
        rewrite ^/content/(.*)      /ui/content.html last;
        rewrite ^/creategroup       /ui/creategroup.html last;
        rewrite ^/favicon.ico       /ui/favicon.ico last;
        rewrite ^/group/(.*)        /ui/group.html last;
        rewrite ^/index$            /ui/index.html last;
        rewrite ^/me$               /ui/me.html last;
        rewrite ^/me/(.*)           /ui/me.html last;
        rewrite ^/$                 /ui/index.html last;
        rewrite ^/search            /ui/search.html last;
        rewrite ^/user/(.*)         /ui/user.html last;
        
        rewrite ^/accessdenied      /ui/errors/accessdenied.html last;
        rewrite ^/maintenance       /ui/errors/maintenance.html last;
        rewrite ^/noscript          /ui/errors/noscript.html last;
        rewrite ^/notfound          /ui/errors/notfound.html last;
        rewrite ^/unavailable       /ui/errors/unavailable.html last;

        ## Admin UI

        location /admin/ {
            alias /opt/3akai-ux/admin/;
            autoindex off;
            expires max;
        }
        
        rewrite ^/admin$  /admin/index.html last;
        
        ## API Documentation
    
        location /docs/ {
            alias /opt/3akai-ux/docs/;
            autoindex off;
        }
        
        rewrite ^/docs$  /docs/index.html last;

        ## Tests
        location /tests/ {
            alias /opt/3akai-ux/tests/;
            autoindex off;
        }
    
        #################
        ## ERROR PAGES ##
        #################

        error_page      401     /ui/errors/accessdenied.html;
        error_page      404     /ui/errors/notfound.html;
        error_page      502     /ui/errors/unavailable.html;
        error_page      503     /ui/errors/maintenance.html;

        #########################
        ## APP SERVER REQUESTS ##
        #########################

        location /api/ui/staticbatch {
            expires max;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_set_header X-NginX-Proxy true;
            proxy_pass http://tenantworkers;
            proxy_redirect off;
        }

        # This can be cached indefinitely because we use signatures that change over time to control invalidation
        location /api/download/signed {
            expires max;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_set_header X-NginX-Proxy true;
            proxy_pass http://tenantworkers;
            proxy_redirect off;
        }

        location /api/config {
            expires 15m;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_set_header X-NginX-Proxy true;
            proxy_pass http://tenantworkers;
            proxy_redirect off;
        }

        location /api/ui/widgets {
            expires 15m;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_set_header X-NginX-Proxy true;
            proxy_pass http://tenantworkers;
            proxy_redirect off;
        }

        location /api/ui/skin {
            expires 15m;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_set_header X-NginX-Proxy true;
            proxy_pass http://tenantworkers;
            proxy_redirect off;
        }

        location /api/ {
            limit_req zone=addr_api burst=60;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_set_header X-NginX-Proxy true;
            proxy_read_timeout 300;
            proxy_pass http://tenantworkers;
            proxy_redirect off;
        }

        ####################
        ## FILE DOWNLOADS ##
        ####################

        # Determines the maximum filesize that a user can upload.
        client_max_body_size 128M;

        # An internal endpoint that is used by the local file storage backend.
        # Change the alias so that it points to the directory that will contain the file bodies.
        # This should match with the oae-content/storage/local-dir config value as configured
        # in the admin UI.
        location /files {
            internal;
            alias /shared/files;
        }
    }

    ####################
    # Etherpad Servers #
    ####################

    # Generate upstream groups with 1 main server and 1 fallback server.
    # The fallback server would be the main server of the next group.

    
    server {
        listen 80;
        server_name 0;

        location / {
            expires 15m;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_set_header X-NginX-Proxy true;
            proxy_pass http://127.0.0.1:9001;
            proxy_redirect off;
            proxy_buffering off;
        }

        location /socket.io/1/websocket/ {
            proxy_pass http://127.0.0.1:9001;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
        }
    }
    
    server {
        listen 80;
        server_name 1;

        location / {
            expires 15m;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_set_header X-NginX-Proxy true;
            proxy_pass http://127.0.0.2:9001;
            proxy_redirect off;
            proxy_buffering off;
        }

        location /socket.io/1/websocket/ {
            proxy_pass http://127.0.0.2:9001;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
        }
    }
    
}
