Created
September 15, 2024 13:22
-
-
Save rickdaalhuizen90/2b98069c9a77a8d2f7f29322cf1c8505 to your computer and use it in GitHub Desktop.
Nginx Configuration with FastCGI Caching enabled
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fastcgi_cache_path /etc/nginx/cache levels=1:2 keys_zone=PHP_CACHE:200m max_size=10g inactive=2h use_temp_path=off; | |
fastcgi_cache_key "$scheme$request_method$host$request_uri"; | |
fastcgi_ignore_headers Cache-Control Expires Set-Cookie; | |
upstream backend { | |
server php-fpm:9000; | |
} | |
server { | |
listen 80; | |
server_name _; | |
return 301 https://$host:8443$request_uri; | |
} | |
server { | |
listen 443 ssl default_server; | |
server_name localhost; | |
# openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout certs/key.pem -out certs/cert.pem -subj "CN=localhost" | |
# cp certs/cert.pem /etc/pki/ca-trust/source/anchors/ && sudo update-ca-trust | |
ssl_certificate /etc/nginx/certs/cert.pem; | |
ssl_certificate_key /etc/nginx/certs/key.pem; | |
root /var/www/html; | |
index index.html index.php; | |
error_log /var/log/nginx/error.log; | |
access_log /var/log/nginx/access.log; | |
location / { | |
try_files $uri $uri/ /index.php?$query_string; | |
} | |
location ~ \.php$ { | |
include fastcgi_params; | |
fastcgi_pass backend; | |
fastcgi_index index.php; | |
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; | |
fastcgi_param PATH_INFO $uri; | |
fastcgi_cache PHP_CACHE; | |
fastcgi_cache_valid 200 301 302 2h; | |
fastcgi_cache_use_stale error; | |
fastcgi_cache_lock on; | |
add_header X-FastCGI-Cache $upstream_cache_status; | |
} | |
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ { | |
expires 30d; | |
add_header Cache-Control "public, no-transform"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment