#   This file is included strictly as an example of how Nginx can be configured
#   to work with CryptPad. This example WILL NOT WORK AS IS. For best results,
#   compare the sections of this configuration file against a working CryptPad
#   installation (http server by the Nodejs process). If you are using CryptPad
#   in production, contact sales@cryptpad.fr

server {
    listen 443 ssl http2;

    server_name cryptpad.fr www.cryptpad.fr beta.cryptpad.fr;

    ssl_certificate         /home/cryptpad/.acme.sh/alpha.cryptpad.fr/fullchain.cer;
    ssl_certificate_key     /home/cryptpad/.acme.sh/alpha.cryptpad.fr/alpha.cryptpad.fr.key;
    ssl_trusted_certificate /home/cryptpad/.acme.sh/alpha.cryptpad.fr/ca.cer;

    ssl_dhparam /etc/nginx/dhparam.pem;
    ssl_session_timeout 5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # omit SSLv3 because of POODLE
    # ECDHE better than DHE (faster)  ECDHE & DHE GCM better than CBC (attacks on AES)  Everything better than SHA1 (deprecated)
    ssl_ciphers 'ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA';
    ssl_prefer_server_ciphers on;

    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options nosniff;
    # add_header X-Frame-Options "SAMEORIGIN";

    root /home/cryptpad/cryptpad;
    index index.html;

    if ($args ~ ver=) {
        set $cacheControl max-age=31536000;
    }
    # Will not set any header if it is emptystring
    add_header Cache-Control $cacheControl;

    set $styleSrc   "'unsafe-inline' 'self'";
    set $scriptSrc  "'self'";
    set $connectSrc "'self' wss://cryptpad.fr wss://api.cryptpad.fr";
    set $fontSrc    "'self'";
    set $imgSrc     "data: * blob:";
    set $frameSrc   "'self' beta.cryptpad.fr";

    if ($uri = /pad/inner.html) {
        set $scriptSrc "'self' 'unsafe-eval' 'unsafe-inline'";
    }
    add_header Content-Security-Policy "default-src 'none'; style-src $styleSrc; script-src $scriptSrc; connect-src $connectSrc; font-src $fontSrc; img-src $imgSrc; frame-src $frameSrc;";

    location = /cryptpad_websocket {
        proxy_pass http://localhost:3000;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

#       add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
        # WebSocket support (nginx 1.4)
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection upgrade;
    }

    location ^~ /customize.dist/ {
        # This is needed in order to prevent infinite recursion between /customize/ and the root
    }
    location ^~ /customize/ {
        rewrite ^/customize/(.*)$ $1 break;
        try_files /customize/$uri /customize.dist/$uri;
    }

    location = /api/config {
        proxy_pass http://localhost:3000;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
        
    location ^~ /blob/ {
        add_header Cache-Control max-age=31536000;
        try_files $uri =404;
    }

    location ^~ /datastore/ {
        add_header Cache-Control max-age=0;
        try_files $uri =404;
    }

    ## TODO fix in the code so that we don't need this
    location ~ ^/(register|login|settings|user|pad|drive|poll|slide|code|whiteboard|file|media)$ {
        rewrite ^(.*)$ $1/ redirect;
    }

    try_files /www/$uri /www/$uri/index.html /customize/$uri;
}