server {
    listen {{ http_port }};
    server_name {{ item.servername }};
    log_format forwarderLog '$remote_addr forwarded for $http_x_forwarded_for - $remote_user [$time_local]  '
    '"$request" $status $body_bytes_sent ' '"$http_referer" "$http_user_agent"';
    log_format shortForwarderLog '$http_x_forwarded_for - $remote_user [$time_local]  '
    '"$request" $status $body_bytes_sent "$http_referer" "$http_user_agent"';
    access_log /var/log/nginx/{{ item.servername }}_access.log shortForwarderLog;

    # This is the default for nginx on Ubuntu 12.04
    root /usr/share/nginx/www/;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/www;
    }
    
    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }

    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }

    location /logs {
        alias /var/log/nginx;
        allow {{ analytics_ip }}; # analytics.d4science.org
        deny all;
    }
    # don't send the nginx version number in error pages and Server header
    server_tokens off;

    # Proxy stuff
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_buffer_size {{ nginx_proxy_buffer_size }};
    proxy_buffers {{ nginx_proxy_buffers }};
    proxy_busy_buffers_size {{ nginx_proxy_busy_buffers_size }};
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Forwarded-Server $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_http_version 1.1;
    proxy_redirect {{ nginx_proxy_redirect }};
    proxy_buffering {{ nginx_proxy_buffering }};
    proxy_connect_timeout {{ nginx_proxy_connect_timeout }};
    proxy_read_timeout {{ nginx_proxy_read_timeout }};
    proxy_send_timeout {{ nginx_proxy_send_timeout }};

    {% if r_connector_install %}
    location /auth-sign-in {
        rewrite ^/auth-sign-in http://{{ item.servername }}/r-connector/gcube/service/disconnect;
    }
    {% endif %}

    {% for instance in tomcat_m_instances %}
    {% for context in instance.app_contexts %}
    location /{{ context }} {
        proxy_pass http://localhost:{{ item.http_port }}/{{ context }};
    }
    {% endfor %}
    {% endfor %}
    location {{ dataminer_52north_webapp_context }} {
        proxy_pass http://localhost:{{ item.http_port }}{{ dataminer_52north_webapp_context }};
    }
    {% if rstudio_install_server %}
    location / {
        proxy_pass http://localhost:8787/;
    }
    {% endif %}
}