因此,我目前正在尝试在 NGINX Web 服务器上实现缓存。目前,我在 sites-enabled 目录中链接了一个 conf 文件,如下所示:
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 不会在该配置上抛出任何错误,但“X-Proxy-Cache”标头不会设置,这表明缓存显然不起作用(我使用的服务器计算机上的目录也保持为空)。我读过多个线程,每个人都在做几乎相同的事情。不过,我在其中一个线程中读到,问题在于“$upstream_cache_status”,它保持为空,因为我没有使用任何将请求代理到的上游,这完全说得通(起初我以为“$upstream_cache_status”是指我正在写入的服务器块的 cache_status)。那么我该如何解决这个问题,以便缓存最终能够正常工作?
这里是我的 nginx.conf 文件,以防它与这个问题有关:
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/*;
}
干杯!
答案1
首先,您是否检查过 nginx 配置工具?https://www.digitalocean.com/community/tools/nginx?global.app.lang=de
它对于复杂的配置场景非常方便。
其次,禁用弱芯片
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
通过替换
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;
关于您的缓存问题(文档在此处找到):
你按照文档操作了吗?我的基本示例开箱即用:
nginx -v
nginx version: nginx/1.18.0
标头设置如下:
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/测试.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;
缓存文件夹:
$ /data/nginx/cache# ls
4 6 8 a b