feat: add nginx cache

This commit is contained in:
Rekryt
2026-01-06 15:27:22 +03:00
parent 753309e22e
commit c7d5830906
3 changed files with 151 additions and 0 deletions

41
docker-compose.prod.yml Normal file
View File

@@ -0,0 +1,41 @@
services:
app:
restart: unless-stopped
build:
context: .
env_file:
- ./.env
volumes:
- ./src/:/app/src/
- ./config/:/app/config/
- ./storage/:/app/storage/
- ./public/:/app/public/
logging:
driver: 'json-file'
options:
max-size: '50m'
# networks:
# - default
nginx:
image: 'nginx:latest'
restart: unless-stopped
ports:
- '8080:80'
volumes:
- ./public:/var/www/public/
- ./nginx/conf.d/:/etc/nginx/conf.d/
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
- ./cache/:/var/cache/
depends_on:
- app
logging:
driver: 'json-file'
options:
max-size: '50m'
# networks:
# - default
# - web
#networks:
# web:
# external: true
# name: web

77
nginx/conf.d/app.conf Normal file
View File

@@ -0,0 +1,77 @@
map $sent_http_content_type $expires {
"text/html" epoch;
"text/html; charset=utf-8" epoch;
"application/javascript" 365d;
"text/css" 365d;
"text/css; charset=utf-8" 365d;
default off;
}
map $args $skip_static {
"" 0;
default 1;
}
upstream app {
server app:8080;
}
server {
listen 80;
index index.php index.html;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/public;
client_max_body_size 20M;
gzip on;
gzip_comp_level 5;
gzip_disable "msie6";
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript;
location / {
location ~* ^.+\.(jpg|jpeg|gif|png|svg|js|css|mp3|ogg|mpe?g|avi|zip|gz|bz2?|rar|swf|woff|woff2|eot|txt)$ {
try_files $uri $uri/ =404;
expires 365d;
}
location /robots.txt {
try_files /robots.txt @fallback;
gzip_static on;
}
location / {
proxy_cache local_cache;
proxy_cache_valid 200 1h;
proxy_cache_lock on;
#proxy_cache_bypass $no_cache;
#proxy_no_cache $no_cache;
add_header X-Proxy-Cache $upstream_cache_status;
proxy_ignore_headers Cache-Control Set-Cookie Expires;
proxy_buffers 16 16k;
proxy_buffer_size 32k;
if ($args) {
proxy_pass http://app;
break;
}
try_files $uri $uri/ @fallback;
gzip_static on;
}
}
location @fallback {
expires $expires;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_read_timeout 1m;
proxy_connect_timeout 1m;
proxy_pass http://app;
}
}

33
nginx/nginx.conf Normal file
View File

@@ -0,0 +1,33 @@
user nginx;
worker_processes 5;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 2048;
}
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;
sendfile on;
keepalive_timeout 65;
real_ip_header X-Forwarded-For;
real_ip_recursive on;
set_real_ip_from 172.0.0.0/8;
proxy_cache_path /var/cache/nginx levels=1:2:2 keys_zone=local_cache:150m inactive=1h max_size=200m;
proxy_cache_key "$scheme$request_method$host$request_uri";
proxy_cache_use_stale error timeout invalid_header http_500;
proxy_ignore_headers Cache-Control Set-Cookie Expires;
include /etc/nginx/conf.d/*.conf;
}