proxy_cache_path  {{ngx_cache_dir}}/{{ server.site_id }}  levels=1:2 keys_zone=cache{{ server.site_id }}:10m inactive=24h  max_size={{ ngx_cache_size}};


{% if server.upstream_ip == "" %}
    {% if upstreams.hash == "ip_hash" %}
        {% set hash = "ip_hash;" %}
    {% elif upstreams.hash == "rr"  %}
        {% set hash = "" %}
    {% elif upstreams.hash == "url_hash"  %}
        {% set hash = "hash $request_uri consistent;" %}
    {% endif %}

    upstream {{ "http_upstream_" + server.site_id }} {
        {{ hash }}
        zone upstream_dynamic 10M;
    {%- for ups in upstreams.upstream_data %}
        server {{ ups.upstream_backend }}:{{ server.http_port }} max_fails={{ upstreams.max_fails }} fail_timeout={{ upstreams.fail_timeout }} weight={{ ups.upstream_weight }} {{ ups.upstream_state }};
    {%- endfor %}
        keepalive 16;
    }

    {% if server.enable_https %}
    upstream {{ "https_upstream_" + server.site_id }} {
        {{ hash }}
        zone upstream_dynamic 10M;
    {%- for ups in upstreams.upstream_data %}
        server {{ ups.upstream_backend }}:{{ server.https_port }} max_fails={{ upstreams.max_fails }} fail_timeout={{ upstreams.fail_timeout }} weight={{ ups.upstream_weight }} {{ ups.upstream_state }};
    {%- endfor %}
        keepalive 16;
    }
    {% endif %}

{% endif %}


{% for map_var in map_data %}
map {{ map_var.val_string }} {{ map_var.val_name }} {
    default       "{{ map_var.val_default }}";
    {{ map_var.val_match }};
}
{% endfor %}

map $host $rhost_{{ server.site_id }} {
    default       "";
    {%- for rewrite in server.rewrite_data %}
    {{ rewrite.rewrite_domain_select }} {{ rewrite.rewrite_domain_redirect + rewrite.rewrite_redirect_code }};
    {%- endfor %}
}

server {
    listen 80;
    {% if server.upstream_ip == "" %}
        {% set upstream_name = "http://http_upstream_" + server.site_id %}
    {% else %}    
        {% set upstream_name = "http://" + server.upstream_ip + ":" + server.http_port %}
    {% endif %}

    {% if server.enable_https and server.crt and server.force_https %}
        {% set force_https = "True" %}
    {% else %}    
        {% set force_https = "False" %}
    {% endif %}

    server_name {{ server.domain }};
    set $rule_name {{ server.cc_rule }};
    set $allow_spider {{ server.allow_spider }};
    set $force_https {{ force_https }};
    set $rhost $rhost_{{ server.site_id }};
    set $site_id {{ server.site_id }};
    set $client_ip $remote_addr;
    {% if server.acl_rule %}
    set $acl_rule {{ server.site_id }};
    {% endif %}

    proxy_connect_timeout {{ server.proxy_connect_timeout }};
    proxy_send_timeout {{ server.proxy_send_timeout }};
    proxy_read_timeout {{ server.proxy_read_timeout }};
    proxy_next_upstream {{ server.proxy_next_upstream }};

    {% if server.enable_https and server.ca_type == "letsencrypt" %}
    location /.well-known/ {
        set $rule_name "close";
        set $acl_rule "";
        proxy_pass http://master-ip:8866;
    }
    {% endif %}

    {% if not server.location_root_dupl %}
    location / {
        proxy_pass              {{ upstream_name }};
        proxy_redirect          off;  
        proxy_set_header        Host            $host; 
        proxy_set_header        X-Real-IP       $remote_addr;
        proxy_set_header        client-ip       $client_ip;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_http_version     1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
    }  
    {% endif %}

    {% for cache in server.proxy_cache %}
    location {{ cache.op }} {{ cache.url }} {
        proxy_pass              {{ upstream_name }};
        proxy_redirect          off;  
        proxy_set_header        Host            $host; 
        proxy_set_header        X-Real-IP       $remote_addr;
        proxy_set_header        client-ip       $client_ip;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_http_version     1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;

        proxy_cache            cache{{ server.site_id }};
        proxy_cache_lock       on;
        proxy_cache_methods    GET HEAD;
        proxy_cache_use_stale  error timeout invalid_header updating
                               http_500 http_502 http_503 http_504;
        proxy_cache_revalidate on;
        proxy_cache_key        "{{ cache.cache_key }}"; 
        proxy_cache_valid 200 {{ cache.expire }};
        add_header X-Cache-Status $upstream_cache_status;            
        {% if cache.nocache %}
        proxy_no_cache {{ cache.nocache }};
        proxy_cache_bypass {{ cache.nocache }};
        {% endif %}
        {% if cache.ignore_headers %}
        proxy_ignore_headers {{ cache.ignore_headers }};
        {% endif %}            
    }
    {% endfor %}

    {% if server.error404 or server.error50x %}
    proxy_intercept_errors on;
    {% endif %}

    {% if server.error404 %}
    error_page 404             /{{ server.site_id }}.404.html;
    location = /{{ server.site_id }}.404.html {
        root /usr/local/openresty/nginx/conf/vhost/error_page;
        internal;
    }        
    {% endif %}

    {% if server.error50x %}
    error_page 500 502 503 504 /{{ server.site_id }}.50x.html;
    location = /{{ server.site_id }}.50x.html {
        root /usr/local/openresty/nginx/conf/vhost/error_page;
        internal;
    }        
    {% endif %}

    {% if server.hotlink %}
    valid_referers none blocked {{ server.hotlink }};

    if ($invalid_referer) {
        return 403;
    }
    {% endif %}

}

{% if server.enable_https and server.crt %}

server {
    listen 443 ssl {{ server.http2 }};
    listen 8888 ssl {{ server.http2 }};
    ssl_session_cache shared:SSL:10m;
    ssl_certificate /usr/local/openresty/nginx/conf/vhost/certs/{{ server.site_id }}.crt;
    ssl_certificate_key /usr/local/openresty/nginx/conf/vhost/certs/{{ server.site_id }}.key;

    {% if server.http_back_to_source %}
        {% if server.upstream_ip == "" %}
            {% set upstream_name = "http://http_upstream_" + server.site_id %}
        {% else %}    
            {% set upstream_name = "http://" + server.upstream_ip + ":" + server.http_port %}
        {% endif %}
    {% else %}
        {% if server.upstream_ip == "" %}
            {% set upstream_name = "https://https_upstream_" + server.site_id %}
        {% else %}    
            {% set upstream_name = "https://" + server.upstream_ip + ":" + server.https_port %}
        {% endif %}    
    {% endif %}


    {% if server.enable_https and server.crt and server.force_https %}
        {% set force_https = "True" %}
    {% else %}    
        {% set force_https = "False" %}
    {% endif %}

    server_name {{ server.domain }};
    set $rule_name {{ server.cc_rule }};
    set $allow_spider {{ server.allow_spider }};
    set $force_https {{ force_https }};
    set $rhost $rhost_{{ server.site_id }};
    set $site_id {{ server.site_id }};
    set $client_ip $remote_addr;
    {% if server.acl_rule %}
    set $acl_rule {{ server.site_id }};
    {% endif %}
    
    
    proxy_connect_timeout {{ server.proxy_connect_timeout }};
    proxy_send_timeout {{ server.proxy_send_timeout }};
    proxy_read_timeout {{ server.proxy_read_timeout }};
    proxy_next_upstream {{ server.proxy_next_upstream }};
    proxy_ssl_protocols {{ server.proxy_ssl_protocols }};

    {% if server.enable_https and server.ca_type == "letsencrypt" %}
    location /.well-known/ {
        set $rule_name "close";
        set $acl_rule "";
        proxy_pass http://master-ip:8866;
    }
    {% endif %}

    if ($http_user_agent ~* (spider|bot)) {
    return 403; 
    }
    
    if ($http_referer ~* "Baiduspider") {
    return 403; 
    }
    {% if not server.location_root_dupl %}
    location / {
        proxy_pass              {{ upstream_name }};
        proxy_redirect          off;  
        proxy_set_header        Host            $host; 
        proxy_set_header        X-Real-IP       $remote_addr;
        proxy_set_header        client-ip       $client_ip;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_http_version     1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
        {% if server.hsts %}
        add_header Strict-Transport-Security "max-age=31536000;" always;
        {% endif %}                 
    }  
    {% endif %}
    
    {% for cache in server.proxy_cache %}
    location {{ cache.op }} {{ cache.url }} {
        proxy_pass              {{ upstream_name }};
        proxy_redirect          off;  
        proxy_set_header        Host            $host; 
        proxy_set_header        X-Real-IP       $remote_addr;
        proxy_set_header        client-ip       $client_ip;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_http_version     1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;

        proxy_cache            cache{{ server.site_id }};
        proxy_cache_lock       on;
        proxy_cache_methods    GET HEAD;
        proxy_cache_use_stale  error timeout invalid_header updating
                               http_500 http_502 http_503 http_504;
        proxy_cache_revalidate on;
        proxy_cache_key        "{{ cache.cache_key }}"; 
        proxy_cache_valid 200 {{ cache.expire }};
        add_header X-Cache-Status $upstream_cache_status;            
        {% if cache.nocache %}
        proxy_no_cache {{ cache.nocache }};
        proxy_cache_bypass {{ cache.nocache }};
        {% endif %}
        {% if cache.ignore_headers %}
        proxy_ignore_headers {{ cache.ignore_headers }};
        {% endif %}       
        {% if server.hsts %}
        add_header Strict-Transport-Security "max-age=31536000;" always;
        {% endif %}                           
    }
    {% endfor %}
    {% if server.error404 or server.error50x %}
    proxy_intercept_errors on;
    {% endif %}

    {% if server.error404 %}
    error_page 404             /{{ server.site_id }}.404.html;
    location = /{{ server.site_id }}.404.html {
        root /usr/local/openresty/nginx/conf/vhost/error_page;
        internal;
    }        
    {% endif %}

    {% if server.error50x %}
    error_page 500 502 503 504 /{{ server.site_id }}.50x.html;
    location = /{{ server.site_id }}.50x.html {
        root /usr/local/openresty/nginx/conf/vhost/error_page;
        internal;
    }        
    {% endif %}
    {% if server.hotlink %}
    valid_referers none blocked  {{ server.hotlink }};

    if ($invalid_referer) {
        return 403;
    }
    {% endif %}

}
{% endif %}
