##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

# from http://piston.readthedocs.io/en/stable/public-api.html#nginx-webserver
# from https://steemit.com/steem/@jesta/building-a-high-availability-steemd-node-for-web-apis
{% if is_api_node %}

# api_node config (websocket forwarding)

upstream websockets {
  server 127.0.0.1:5090;
  server 127.0.0.1:5091;
}

server {

{% if nginx.get('ssl_key') and nginx.get('ssl_cert') -%}
    listen 443 ssl;

    ssl_certificate /etc/nginx/certs/{{ nginx['ssl_cert_basename'] }};
    ssl_certificate_key /etc/nginx/certs/{{ nginx['ssl_key_basename'] }};
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

    #ssl_prefer_server_ciphers on;
    #ssl_dhparam /etc/ssl/certs/dhparam.pem;
    #ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA';
    #ssl_session_timeout 1d;
    #ssl_session_cache shared:SSL:50m;
    #ssl_stapling on;
    #ssl_stapling_verify on;
    add_header Strict-Transport-Security max-age=15768000;
{% else %}
    listen 80;
{%- endif %}

    server_name {{ nginx['server_name'] }};
    root /var/www/html/;

    keepalive_timeout 65;
    keepalive_requests 100000;
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;

    location ~ ^(/|/ws) {
        limit_req zone=ws burst=5;
        access_log off;
        proxy_pass http://websockets;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_next_upstream error timeout invalid_header http_500;
        proxy_connect_timeout 2;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
}

{% else %}

{% if nginx.get('ssl_key') and nginx.get('ssl_cert') %}

server {
        listen  80;
        server_name {{ nginx['server_name'] }};
        rewrite ^ https://$server_name$request_uri? permanent;
}

server {
        listen 443;
        listen [::]:443;

        ssl on;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_certificate /etc/nginx/certs/{{ nginx['ssl_cert_basename'] }};
        ssl_certificate_key /etc/nginx/certs/{{ nginx['ssl_key_basename'] }};

{% else %}

server {
        listen  80;
        listen [::]:80;

{% endif %}

        server_name {{ nginx['server_name'] }};
        charset utf-8;
        location / { try_files $uri @bts_tools; }
        location @bts_tools {
                # optional password protection
                #auth_basic "Restricted";
                #auth_basic_user_file /home/{{ unix_user }}/.htpasswd;
                include uwsgi_params;
                uwsgi_pass unix:/run/uwsgi/app/bts_tools/socket;
        }
}

{% endif %}


