log_format access '$remote_addr [$time_local] '
                  '"$request" $status $bytes_sent '
                  '"$http_referer" "$http_user_agent" $request_time $request_id';

server {
  listen 80;

  access_log /dev/stdout access;

  location / {
    add_header X-Request-ID $request_id;
    proxy_set_header X-Request-ID $request_id;
    
    # add_header trace-id $request_id;
    proxy_set_header trace-id $request_id;

    proxy_pass http://photolist:8080/;
  }

  location /notexist {
    return 404 "Page not found";
  }

  location ~* ^/images/(\d+)/(.*) {
    auth_request /auth;

    add_header X-Request-ID $request_id always;
    add_header trace-id $request_id always;

    rewrite ^/images/(\d+)/(.*) /photolist/$2 break;

    proxy_http_version 1.1;
    proxy_buffering off;
    proxy_read_timeout 15m; # Default value is 60s which is not sufficient for MinIO.
    proxy_send_timeout 15m; # Default value is 60s which is not sufficient for MinIO.
    proxy_request_buffering off; # Disable any internal request bufferring.
    proxy_set_header Host $http_host;

    proxy_hide_header      x-amz-request-id;
    proxy_hide_header      x-amz-meta-user-id;
    proxy_set_header       X-Request-ID $request_id;

    proxy_pass http://minio:9000;
  }

  location = /auth {
      internal;
      proxy_pass http://photoauth:8080/api/v1/internal/images/auth;
      proxy_pass_request_body off;
      proxy_set_header Content-Length "";
      proxy_set_header X-Request-ID $request_id;
      proxy_set_header trace-id $request_id;
      proxy_set_header X-Original-URI $request_uri;
  }

}
