log_format apm '"$time_local" client=$remote_addr '
               'method=$request_method request="$request" '
               'request_length=$request_length '
               'status=$status bytes_sent=$bytes_sent '
               'body_bytes_sent=$body_bytes_sent '
               'referer=$http_referer '
               'user_agent="$http_user_agent" '
               'upstream_addr=$upstream_addr '
               'upstream_status=$upstream_status '
               'request_time=$request_time '
               'upstream_response_time=$upstream_response_time '
               'upstream_connect_time=$upstream_connect_time '
               'upstream_header_time=$upstream_header_time';

server {
    listen 80;
    server_name _;
    root /app/nocobase/node_modules/@nocobase/app/dist/client;
    index index.html;
    client_max_body_size 0;
    access_log /var/log/nginx/nocobase.log apm;

    gzip on;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    location ~* ^/storage/uploads/(.*\.(?:htm|html|svg|svgz|xhtml))$ {
        alias /app/nocobase/storage/uploads/$1;
        add_header Cache-Control "public";
        add_header Content-Disposition "attachment" always;
        add_header X-Content-Type-Options "nosniff" always;
        access_log off;
        autoindex off;
    }

    location /storage/uploads/ {
        alias /app/nocobase/storage/uploads/;
        add_header Cache-Control "public";
        add_header X-Content-Type-Options "nosniff" always;
        access_log off;
        autoindex off;
    }

    location /static/plugins/ {
        alias /app/nocobase/node_modules/;
        expires 365d;
        add_header Cache-Control "public";
        access_log off;
        autoindex off;

        location ~ ^/static/plugins/@([^/]+)/([^/]+)/dist/client/(.*)$ {
            allow all;
        }

        location ~ ^/static/plugins/([^/]+)/dist/client/(.*)$ {
            allow all;
        }

        location ~ ^/static/plugins/(.*)$ {
            deny all;
        }
    }

    location / {
        alias /app/nocobase/node_modules/@nocobase/app/dist/client/;
        try_files $uri $uri/ /index.html;
        add_header Last-Modified $date_gmt;
        add_header Cache-Control 'no-store, no-cache';
        add_header X-Robots-Tag "noindex, nofollow";
        if_modified_since off;
        expires off;
        etag off;
        location ~* \.(js|css)$ {
            expires 365d;
            add_header Cache-Control "public";
        }
    }

    

    location ^~ /api/ {
        proxy_pass http://127.0.0.1:13000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Host $host;
        proxy_set_header Referer $http_referer;
        proxy_set_header User-Agent $http_user_agent;
        add_header Cache-Control 'no-cache, no-store';
        proxy_cache_bypass $http_upgrade;
        proxy_connect_timeout 600;
        proxy_send_timeout 600;
        proxy_read_timeout 600;
        send_timeout 600;
    }

    location /ws {
      proxy_pass http://127.0.0.1:13000/ws;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "Upgrade";
      proxy_set_header Host $host;
    }
}
