diff --git a/Dockerfile-cache b/Dockerfile-cache new file mode 100644 index 0000000..dd25492 --- /dev/null +++ b/Dockerfile-cache @@ -0,0 +1,7 @@ +FROM nginx + +RUN mkdir -p /var/lib/nginx/cache && \ + chown www-data /var/lib/nginx/cache && \ + chmod 700 /var/lib/nginx/cache + +COPY nginx.conf /etc/nginx/nginx.conf diff --git a/aws_push.sh b/aws_push.sh index 52e5192..cfa03cb 100644 --- a/aws_push.sh +++ b/aws_push.sh @@ -2,5 +2,7 @@ eval $(aws ecr get-login --no-include-email) docker tag "sefaria-docker_web:latest" "058331968334.dkr.ecr.eu-central-1.amazonaws.com/shitufta-sefaria-api:latest" docker tag "sefaria-docker_mongo:latest" "058331968334.dkr.ecr.eu-central-1.amazonaws.com/shitufta-sefaria-db:latest" +docker tag "sefaria-docker_cache:latest" "058331968334.dkr.ecr.eu-central-1.amazonaws.com/shitufta-cache-api:latest" docker push "058331968334.dkr.ecr.eu-central-1.amazonaws.com/shitufta-sefaria-api:latest" docker push "058331968334.dkr.ecr.eu-central-1.amazonaws.com/shitufta-sefaria-db:latest" +docker push "058331968334.dkr.ecr.eu-central-1.amazonaws.com/shitufta-cache-api:latest" diff --git a/docker-compose.cloud.yml b/docker-compose.cloud.yml index 468c8e1..fb14122 100644 --- a/docker-compose.cloud.yml +++ b/docker-compose.cloud.yml @@ -20,3 +20,15 @@ services: depends_on: - mongo - redis + cache: + restart: always + image: 058331968334.dkr.ecr.eu-central-1.amazonaws.com/shitufta-cache-api + volumes: + - api-cache:/var/lib/nginx/cache + ports: + - "8080:80" + depends_on: + - web + +volumes: + api-cache: diff --git a/docker-compose.yml b/docker-compose.yml index 756a313..3aff6f4 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -19,3 +19,11 @@ services: depends_on: - mongo - redis + cache: + build: + context: . + dockerfile: Dockerfile-cache + ports: + - "8080:80" + depends_on: + - web diff --git a/nginx.conf b/nginx.conf new file mode 100644 index 0000000..fb4474f --- /dev/null +++ b/nginx.conf @@ -0,0 +1,97 @@ +# daemon off; +error_log /dev/stdout info; +user nginx; +worker_processes 1; + +error_log /var/log/nginx/error.log warn; +pid /var/run/nginx.pid; + +events { + worker_connections 1024; +} + + +http { + include /etc/nginx/mime.types; + default_type application/octet-stream; + + log_format main '$remote_addr - $remote_user [$time_local] "$request" ' + '$status $body_bytes_sent "$http_referer" ' + '"$http_user_agent" "$http_x_forwarded_for"'; + + # access_log /var/log/nginx/access.log main; + access_log /dev/stdout; + + + sendfile on; + #tcp_nopush on; + + keepalive_timeout 65; + + + proxy_cache_path /var/lib/nginx/cache levels=1:2 keys_zone=backcache:8m max_size=1g; + + + server { + listen 80; + + proxy_cache_key "$host$request_uri$cookie_user"; + proxy_cache_valid 200 302 10h; + proxy_cache backcache; + proxy_cache_bypass $cookie_nocache $arg_nocache$arg_comment; + proxy_ignore_headers Set-Cookie; + + + chunked_transfer_encoding off; +# Enable Gzip compressed. + gzip on; + + # Enable compression both for HTTP/1.0 and HTTP/1.1 (required for CloudFront). + gzip_http_version 1.0; + + # Compression level (1-9). + # 5 is a perfect compromise between size and cpu usage, offering about + # 75% reduction for most ascii files (almost identical to level 9). + gzip_comp_level 5; + + # Don't compress anything that's already small and unlikely to shrink much + # if at all (the default is 20 bytes, which is bad as that usually leads to + # larger files after gzipping). + gzip_min_length 256; + + # Compress data even for clients that are connecting to us via proxies, + # identified by the "Via" header (required for CloudFront). + gzip_proxied any; + + # Tell proxies to cache both the gzipped and regular version of a resource + # whenever the client's Accept-Encoding capabilities header varies; + # Avoids the issue where a non-gzip capable client (which is extremely rare + # today) would display gibberish if their proxy gave them the gzipped version. + gzip_vary on; + + gzip_buffers 16 8k; + + # Compress all output labeled with one of the following MIME-types. + gzip_types + application/atom+xml + application/javascript + application/json + application/rss+xml + application/vnd.ms-fontobject + application/x-font-ttf + application/x-web-app-manifest+json + application/xhtml+xml + application/xml + font/opentype + image/svg+xml + image/x-icon + text/css + text/plain + text/x-component; + location / { + proxy_pass http://web:8000/api/; + } + } + + # include /etc/nginx/conf.d/*.conf; +}