From a011f9c803501489c49fe66ffea648d42b2682b5 Mon Sep 17 00:00:00 2001 From: Zsolt Ero Date: Mon, 8 Jan 2024 19:16:32 +0100 Subject: [PATCH] nginx, added timeout to requests --- scripts/http_host/host_manager.py | 2 +- scripts/http_host/http_host_lib/nginx.py | 11 ++++++---- .../http_host_lib/set_tileset_versions.py | 2 +- .../http_host_lib/templates/nginx_cf.conf | 21 +++++++++++++++++-- scripts/http_host/http_host_lib/utils.py | 2 +- 5 files changed, 29 insertions(+), 9 deletions(-) diff --git a/scripts/http_host/host_manager.py b/scripts/http_host/host_manager.py index 830b3bb..334061e 100755 --- a/scripts/http_host/host_manager.py +++ b/scripts/http_host/host_manager.py @@ -48,7 +48,7 @@ def download_tileset(area: str, version: str, list_versions: bool, runs_dir: Pat if area not in {'planet', 'monaco'}: sys.exit(' please specify area: "planet" or "monaco"') - r = requests.get(f'https://{area}.openfreemap.com/dirs.txt') + r = requests.get(f'https://{area}.openfreemap.com/dirs.txt', timeout=30) r.raise_for_status() versions = sorted(r.text.splitlines()) diff --git a/scripts/http_host/http_host_lib/nginx.py b/scripts/http_host/http_host_lib/nginx.py index def8260..37aaf18 100644 --- a/scripts/http_host/http_host_lib/nginx.py +++ b/scripts/http_host/http_host_lib/nginx.py @@ -71,19 +71,21 @@ def create_version_location(area: str, version: str, subdir: Path) -> str: alias {tilejson_path}; # no trailing slash default_type application/json; + expires 1d; # TODO target 1w + add_header 'Access-Control-Allow-Origin' '*' always; add_header Cache-Control public; - expires 1d; # TODO target 1w }} location /{area}/{version}/ {{ # trailing slash alias {subdir}/tiles/; # trailing slash try_files $uri @empty; - add_header Content-Encoding gzip; + + expires 1d; # TODO target 10y + add_header 'Access-Control-Allow-Origin' '*' always; add_header Cache-Control public; - expires 1d; # TODO target 10y }} """ @@ -107,9 +109,10 @@ def create_latest_locations() -> str: alias {tilejson_path}; # no trailing slash default_type application/json; + expires 1d; + add_header 'Access-Control-Allow-Origin' '*' always; add_header Cache-Control public; - expires 1d; }} """ diff --git a/scripts/http_host/http_host_lib/set_tileset_versions.py b/scripts/http_host/http_host_lib/set_tileset_versions.py index a9167b4..48df50b 100644 --- a/scripts/http_host/http_host_lib/set_tileset_versions.py +++ b/scripts/http_host/http_host_lib/set_tileset_versions.py @@ -9,7 +9,7 @@ def set_tileset_versions(): need_nginx_sync = False for area in ['planet', 'monaco']: - r = requests.get(f'https://assets.openfreemap.com/versions/deployed_{area}.txt') + r = requests.get(f'https://assets.openfreemap.com/versions/deployed_{area}.txt', timeout=30) r.raise_for_status() remote_version = r.text.strip() print(f' remote version for {area}: {remote_version}') diff --git a/scripts/http_host/http_host_lib/templates/nginx_cf.conf b/scripts/http_host/http_host_lib/templates/nginx_cf.conf index 7f7a4bc..7dac234 100644 --- a/scripts/http_host/http_host_lib/templates/nginx_cf.conf +++ b/scripts/http_host/http_host_lib/templates/nginx_cf.conf @@ -27,13 +27,27 @@ server { location /fonts/ { # trailing slash + alias /data/ofm/http_host/assets/fonts/; # trailing slash try_files $uri =404; default_type application/x-protobuf; + expires 1d; # target 1w + + add_header 'Access-Control-Allow-Origin' '*' always; + add_header Cache-Control public; + } + + location /ne/ { + # trailing slash + + alias /data/ofm/http_host/assets/natural_earth/tiles; # trailing slash + try_files $uri =404; + + expires 1d; # target 10y + add_header 'Access-Control-Allow-Origin' '*' always; add_header Cache-Control public; - expires 1d; # target 1w } # we need to handle missing tiles as valid request returning empty string @@ -41,9 +55,10 @@ server { default_type application/vnd.mapbox-vector-tile; return 200 ''; + expires 10y; + add_header 'Access-Control-Allow-Origin' '*' always; add_header Cache-Control public; - expires 10y; } location = / { @@ -53,5 +68,7 @@ server { # catch-all block to deny all other requests location / { deny all; + + error_log /data/ofm/http_host/logs_nginx/tiles-org-error.log error; } } diff --git a/scripts/http_host/http_host_lib/utils.py b/scripts/http_host/http_host_lib/utils.py index 02b7f02..0a9e311 100644 --- a/scripts/http_host/http_host_lib/utils.py +++ b/scripts/http_host/http_host_lib/utils.py @@ -33,7 +33,7 @@ def download_if_size_differs(url: str, local_file: Path) -> bool: def get_remote_file_size(url: str) -> int | None: - r = requests.head(url) + r = requests.head(url, timeout=30) size = r.headers.get('Content-Length') return int(size) if size else None