Nginx 캐시는 "X-Proxy-Cache" 헤더를 설정하지 않습니다.

Nginx 캐시는 "X-Proxy-Cache" 헤더를 설정하지 않습니다.

그래서 저는 현재 NGINX 웹 서버에 캐싱을 구현하려고 합니다. 지금은 연결된 사이트 지원 디렉터리에 다음과 같은 단일 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"는 내가 쓰고 있는 서버 블록의 캐시_상태를 나타냅니다. 그렇다면 캐싱이 마침내 작동하도록 이 문제에 어떻게 접근할 수 있습니까?

또한 이 문제와 관련이 있는 경우를 대비해 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-Configuration Tool을 확인하셨나요?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/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;

캐시 폴더:

$ /data/nginx/cache# ls
4  6  8  a  b

관련 정보