Ich versuche derzeit, Caching auf meinem NGINX-Webserver zu implementieren. Im Moment habe ich eine einzelne Conf-Datei in meinem Sites-enabled-Verzeichnis verlinkt, die so aussieht:
proxy_cache_path /var/cache/nginx levels=1:2 inactive=120s keys_zone=custom_cache:10m;
server {
root /var/www/html;
server_name _;
location / {
proxy_cache custom_cache;
proxy_cache_valid 60m;
add_header X-Proxy-Cache $upstream_cache_status;
try_files $uri $uri/ /index.html =404;
}
listen [::]:443 ssl http2 ipv6only=on; # managed by Certbot
listen 443 ssl http2; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/example.io/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/example.io/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = www.example.io) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = example.io) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
listen [::]:80;
server_name example.io www.example.io;
return 404; # managed by Certbot
}
NGINX wirft bei dieser Konfiguration keine Fehler aus, aber der Header „X-Proxy-Cache“ wird nicht gesetzt, was darauf hinweist, dass der Cache offensichtlich nicht funktioniert (das Verzeichnis bleibt auch auf dem Server-Rechner, den ich verwende, leer). Ich habe mehrere Threads gelesen, in denen alle so ziemlich dasselbe gemacht haben. In einem davon habe ich jedoch gelesen, dass das Problem beim „$upstream_cache_status“ liegt, der leer bleibt, weil ich keinen Upstream verwende, an den ich die Anfrage weiterleite, was absolut Sinn macht (zuerst dachte ich, der „$upstream_cache_status“ würde sich auf den Cache-Status des Serverblocks beziehen, in den ich schreibe). Wie kann ich also dieses Problem angehen, damit das Caching endlich funktioniert?
Hier ist auch meine nginx.conf-Datei, falls sie für dieses Problem irgendwie relevant ist:
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
# gzip on;
gzip on;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
gzip_vary on;
gzip_types text/plain text/css text/xml text/javascript image/svg+xml image/x-icon application/x-javascript application/javascript application/xml;
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
Prost!
Antwort1
Haben Sie zuerst das Nginx-Konfigurationstool überprüft?https://www.digitalocean.com/community/tools/nginx?global.app.lang=de
Es ist sehr praktisch für komplexe Konfigurationsszenarien.
Zweitens, deaktivieren Sie Ihre schwachen Chips
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
Durch Ersetzen
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;
Zu Ihrem Caching-Problem (Hier gefundene Dokumente):
Haben Sie die Anleitung befolgt? Mein Basisbeispiel funktioniert sofort:
nginx -v
nginx version: nginx/1.18.0
Der Header wird gesetzt:
wget -S -O - http://example.com
HTTP request sent, awaiting response...
HTTP/1.1 200 OK
Server: nginx/1.18.0
Date: Wed, 01 Mar 2023 14:26:13 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 865
Connection: keep-alive
X-Proxy-Cache: HIT
Accept-Ranges: bytes
/etc/nginx/conf.d/test.conf
server {
server_name example.com;
listen 80;
listen [::]:80;
location / {
# Reverse Proxy
proxy_pass http://127.0.0.1:3000;
index index.html index.htm index.php;
limit_except HEAD GET POST {deny all;}
proxy_ignore_headers "Set-Cookie";
proxy_hide_header "Set-Cookie";
proxy_cache STATIC;
add_header X-Proxy-Cache $upstream_cache_status;
proxy_cache_valid 200 302 60m;
proxy_cache_valid 404 1m;
}
}
/etc/nginx/nginx.conf
http {
##
# Basic Settings
##
proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=STATIC:10m;
Der Cache-Ordner:
$ /data/nginx/cache# ls
4 6 8 a b