Assets: how they work
There are two types of asset files.
Static assets are stylesheets (CSS), JavaScript (JS) and image files which make GOV.UK look the way it does.
Uploaded assets - also called attachments - are files like PDFs, CSVs and images which are uploaded via the publishing apps and attached to documents.
Static assets
These assets are served from https://www.gov.uk/assets with a path associated
with the application that is serving them. For example files within
https://www.gov.uk/assets/frontend are assets for the
Frontend application. These files
are cached by Fastly.
Asset files are stored in an S3 bucket (e.g. govuk-app-assets-production in
production) with object prefixes (e.g. /assets/frontend) for each application.
Network flow for static assets
- User makes a request for
https://www.gov.uk/assets/frontend/govuk_publishing_components/vendor/example.jswhich hits Fastly - Fastly on a cache miss makes a request to the
govuk-app-assets-productionS3 bucket forhttps://govuk-app-assets-production.s3.eu-west-1.amazonaws.com/assets/frontend/example.js - The
govuk-app-assets-productionS3 bucket retrievesassets/frontend/example.jsand returns it back to Fastly which caches it - Fastly returns the
assets/frontend/example.jsasset back to the user
sequenceDiagram
actor User
participant Fastly
participant S3
User->>Fastly:
activate Fastly
Fastly->>S3:
S3-->>Fastly:
Fastly-->>User:
deactivate Fastly
Network flow for static assets on the draft stack
The network flow for the draft stack bypasses Fastly and goes to authenticating proxy.
- User makes a request for
https://draft-origin.publishing.service.gov.uk/assets/frontend/example.jswhich hits authenticating-proxy’s location/assetsdirective - authenticating-proxy proxies the request to the
govuk-app-assets-productionS3 bucket forhttps://govuk-app-assets-production.s3.eu-west-1.amazonaws.com/assets/frontend/example.js - The
govuk-app-assets-productionS3 bucket retrievesassets/frontend/example.jsand returns it back to authenticating-proxy’s nginx - authenticating-proxy’s nginx returns the
assets/frontend/example.jsasset back to the user
sequenceDiagram
actor User
participant proxy as authenticating proxy
participant S3
User->>proxy:
activate proxy
proxy->>S3:
S3-->>proxy:
proxy-->>User:
Network flow for static assets served by publishing apps
Publishing apps (e.g. whitehall-admin) don’t use Router nor Fastly so their static assets are served from their nginx instance proxying to S3 for both the live and draft stacks.
- User makes a request for
https://whitehall-admin.publishing.service.gov.uk/assets/whitehall/admin/example.jswhich hits whitehall-admin’s nginx location/assetsdirective - whitehall-admin’s nginx makes a request to the
govuk-app-assets-productionS3 bucket forhttps://govuk-app-assets-production.s3.eu-west-1.amazonaws.com/assets/whitehall/example.js - The
govuk-app-assets-productionS3 bucket retrievesassets/whitehall/example.jsand returns it back to whitehall-admin’s nginx - whitehall-admin’s nginx returns the
assets/whitehall/example.jsasset back to the user
sequenceDiagram
actor User
participant nginx as whitehall-admin nginx
participant S3
User->>nginx:
activate nginx
nginx->>S3:
S3-->>nginx:
nginx-->>User:
deactivate nginx
Uploaded assets
Asset Manager is an API that is called internally
by GOV.UK publishing applications to manage
their uploads. It serves the uploaded assets on
assets.publishing.service.gov.uk. These files
are cached by Fastly.
How uploaded assets are stored and served
Asset files are stored in an S3 bucket (e.g. govuk-assets-production in
production) and Asset Manager instructs nginx to proxy requests to them.
Proxying was chosen, rather than linking directly to S3, to support the
following features:
- Assets are not served until they have been virus scanned; a placeholder image or page is shown for assets that are not finished scanning.
- Assets can be access-limited so that only authorised users can see them.
- Asset files can be replaced, and a request to the original path redirects to the replacement. Currently only Whitehall and Specialist Publisher support this.
Network flow for live assets
Retrieving an uploaded asset involves requests between asset-manager’s nginx proxy, asset-manager’s Rails app and S3.
- User makes a request for
https://assets.publishing.service.gov.uk/media/6ae90b0e5b14ba0182a272a4/example.pdfwhich hits Fastly - Fastly on a cache miss makes a request to asset manager’s nginx for
example.pdf - nginx performs an internal redirect to
location @appwhich proxies a request to asset-manager forexample.pdf - asset-manager on successful lookup returns
X-Accel-Redirect: cloud-storage-proxy/...which contains the presigned S3 URL (e.g.govuk-assets-production.s3.us-east-1.amazonaws.com/example.pdf?X-Amz-Algorithm=AWS4-HMAC-SHA256...) - nginx performs an internal redirect to
location ~ /cloud-storage-proxy/(.*)which proxies a request to S3 forhttps://govuk-assets-production.s3.us-east-1.amazonaws.com/example.pdf?X-Amz-Algorithm=AWS4-HMAC-SHA256... - The
govuk-assets-productionS3 bucket retrievesexample.pdfand returns it back to nginx - nginx returns the response containing the
example.pdfasset back to Fastly which caches it - Fastly returns the
example.pdfasset back to the user
sequenceDiagram
actor User
participant Fastly
participant nginx as asset-manager nginx
participant asset-manager
participant S3
User->>Fastly:
activate Fastly
Fastly->>nginx:
activate nginx
nginx->>nginx: location @app
nginx->>asset-manager:
asset-manager-->>nginx:
Note over nginx, asset-manager: Response Headers:
X-Accel-Redirect:...
nginx->>nginx: location ~ /cloud-storage-proxy/(.*)
nginx->>S3:
S3-->>nginx:
nginx-->>Fastly:
deactivate nginx
Fastly-->>User:
deactivate Fastly
Additional assets
Some uploaded assets have /government/uploads in their path (e.g. https://www.gov.uk/government/uploads/uploaded/hmrc/payetools-rti-17.3.17151.465-win.zip).
sequenceDiagram
actor User
participant Fastly
participant Router
participant Frontend
User->>Fastly:
activate Fastly
Fastly->>Router:
activate Router
Router->>Frontend:
Frontend-->>Router:
deactivate Router
Router-->>Fastly:
deactivate Fastly
Fastly-->>User:
- User makes a request for
https://www.gov.uk/government/uploads/uploaded/example.pdfwhich hits Fastly - Fastly on a cache miss makes a request to Router for
https://www.gov.uk/government/uploads/uploaded/example.pdf - Router proxies this request to a backend (e.g. frontend)
- Frontend returns a
301redirect forhttps://assets.publishing.service.gov.uk/government/uploads/uploaded/department/example.pdf - Router returns this back to Fastly
- Fastly returns this back to the user who is redirected to
https://assets.publishing.service.gov.uk/government/uploads/uploaded/department/example.pdfto begin the network flow for live assets
File size limit
There is a hard limit of 500 MB for assets.
Publishers may see timeouts if they attempt to upload very large attachments. In the past, we’ve worked around this by uploading a small file (in Whitehall) and then replacing the file in Asset Manager.