# Expires map
map $sent_http_content_type $expires {
    default                    off;
    text/html                  epoch;
    ~image/                    max;
    ~font/                     max;
    #css and js are fingerprinted by hugo pipes
    text/css                   max;
    application/javascript     max;
}

server {
    listen       8081;
    server_name www.kicad-pcb.org;
    return 301 $scheme://kicad-pcb.org$request_uri;
}

server {
    listen       8081 default_server;
    server_name  _;
    server_tokens off;

    # avoid port changing between container and external ports on redirects
    # there is also port_in_redirect but it things trickier in development when you aren't binding to port 80 :/
    absolute_redirect off;

    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;

    gzip on;
    gzip_disable        "MSIE [1-6]\.";
    gzip_vary           on;

    gzip_comp_level 4;
    gzip_min_length 1100;
    gzip_buffers 16 8k;
    gzip_proxied expired no-cache no-store private auth;
    gzip_types
        # text/html is always compressed by HttpGzipModule
        text/css
        text/javascript
        text/xml
        text/plain
        text/x-component
        application/javascript
        application/json
        application/xml
        application/rss+xml
        font/truetype
        font/opentype
        application/vnd.ms-fontobject
        image/svg+xml;


    expires $expires;

    # Apply CSP policy to all requests
    # Fonts
    set $csp_font "font-src 'self'";
    # Allow google fonts
    set $csp_font "${csp_font} fonts.gstatic.com";

    set $csp_style "style-src 'self'";
    # Allow the numerous style="" attributes scattered everywhere :/
    set $csp_style "${csp_style} 'unsafe-inline'";
    # Allow google fonts
    set $csp_style "${csp_style} fonts.googleapis.com";

    set $csp_media "media-src 'self'";
    # Allow youtube embeds
    set $csp_media "${csp_media} *.youtube.com";
    set $csp_media "${csp_media} player.vimeo.com";

    set $csp_object "object-src 'self'";
    # Allow youtube embeds
    set $csp_object "${csp_object} *.youtube.com";

    set $csp_script "script-src 'self'";
    # Allow our webp inline script
    set $csp_script "${csp_script} 'unsafe-inline'";

    set $csp_frame "frame-src 'self'";
    # Allow youtube embeds
    set $csp_frame "${csp_frame} *.youtube.com";
    set $csp_frame "${csp_frame} player.vimeo.com";

    # Allow all external images
    set $csp_img "img-src *";
    # Allow youtube embeds
    set $csp_img "${csp_img} data:";

    add_header Content-Security-Policy "default-src 'self';${csp_img};${csp_font};${csp_style};${csp_media};${csp_object};${csp_script};${csp_frame}";


    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    # old website urls
    location ~ ^/help/documentation/(.*) {
        return 301 https://docs.kicad-pcb.org;
    }

    # from when we used jira, ewww
    location ~ ^/pages/viewpage.action/(.*) {
        return 301 https://docs.kicad-pcb.org;
    }
    
    location ~ ^/pages/viewrecentblogposts.action/(.*) {
        return 301 https://kicad-pcb.org/blog/;
    }

    location ~ ^/dashboard.action/(.*) {
        return 301 https://kicad-pcb.org/;
    }
    
    location ~ ^/plugins/servlet/mobile/(.*) {
        return 301 https://kicad-pcb.org/;
    }
    
    location ~ ^/display/DEV/(.*) {
        return 301 https://kicad-pcb.org/contribute/developers/;
    }
    
    location ~ ^/display/KICAD/Download(.*) {
        return 301 https://kicad-pcb.org/download/;
    }

    location ~ ^/display/KICAD/Windows(.*) {
        return 301 https://kicad-pcb.org/downloads/windows/;
    }
    
    location ~ ^/display/KICAD/Installing(.*) {
        return 301 https://kicad-pcb.org/downloads/;
    }

    location ~ ^/display/KICAD/Platforms(.*) {
        return 301 https://kicad-pcb.org/downloads/;
    }

    location ~ ^/display/KICAD/Tutorials(.*) {
        return 301 https://kicad-pcb.org/help/tutorials/;
    }

    location ~ ^/display/KICAD/Frequently+Asked+Questions(.*) {
        return 301 https://docs.kicad-pcb.org;
    }

    location ~ ^/display/KICAD/External+Tools(.*) {
        return 301 https://kicad-pcb.org/help/external-tools/;
    }

    location ~ ^/display/KICAD/KiCad+Documentation(.*) {
        return 301 https://docs.kicad-pcb.org;
    }
    
    location ~ ^/display/(.*) {
        return 301 https://kicad-pcb.org/;
    }

    error_page 404 /404.html;
    location = /404.html {
        root   /usr/share/nginx/html;
        internal;
    }

    # 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/html;
    }
}
