nginx에서 캐싱 활성화

nginx에서 캐싱 활성화

CentOS 6 및 nginx를 사용하는 자체 VPS가 있고 캐싱을 활성화하고 싶습니다. 테스트하기 위해 성공적으로 활성화되면 Google PageSpeed ​​Insight를 사용합니다. 내 문제는 캐싱을 활성화해야 하는 곳과 이미지가 캐시되는 기간 등을 설정할 수 있는 곳이 많지 않다는 것입니다. 이것이 내가 인터넷에서 찾은 것이고 지금까지 시도한 것입니다.

  1. 디렉토리 생성: /etc/nginx/sites-available그리고 /etc/nginx/sites-enabled디렉토리가 어떻게든 존재하지 않았기 때문입니다.
  2. 생성된 디렉터리를 여기에 연결: 파일 끝에 /etc/nginx/nginx.conf추가 하고 마지막 디렉터리 앞에 추가include /etc/nginx/sites-enabled/*;}
  3. 파일 만들기 /etc/nginx/sites-available/my-site.com.conf:

    server {
    listen       80;
    server_name  localhost;
    
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
    
    location ~*  \.(jpg|jpeg|png|gif|ico|css|js)$ {
        expires 15d;
    }
    
    location ~*  \.(pdf)$ {
        expires 30d;
    }
    

    }

  4. conf 파일 연결:ln -s /etc/nginx/sites-available/my-site.com.conf /etc/nginx/sites-enabled/my-site.com.conf

  5. 하다service nginx restart

저는 WordPress용 웹사이트를 사용합니다.

따라서 PageSpeed ​​Insight 또는 기타 페이지 속도 도구를 사용하여 페이지를 테스트할 때마다 header.png, javascript 등에 캐싱을 사용하지 않는다는 메시지가 나타납니다. 그러나 nginx -t다음을 보여주는 구성 파일을 확인하더라도 일부 오류는 발생하지 않습니다 .

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

제가 뭔가 잊어버린 걸까요?

이것은 내 완전한 nginx 구성입니다.http://pastebin.com/wxnzzePT

default.conf폴더 에서 conf.d:http://pastebin.com/KUH2tSrD

답변1

파일 에 캐싱 지시어를 추가 default.conf하고 생성한 새 파일을 제거해야 합니다.

새 파일은 사용자가 를 사용하여 사이트를 방문할 때만 사용됩니다 http://localhost. 또한 새 파일 구성은 파일과 다른 경로를 사용합니다 default.conf.

또한 블록 root내부의 지시문은 location나쁜 습관입니다.

따라서 default.conf다음과 같아야 합니다.

#
# The default server
#
server {
    listen       80 default_server;
    server_name  213.165.xx.xx;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;

    root   /var/www/wordpress;

    location / {
        index  index.html index.htm index.php;

        try_files $uri $uri/ /index.php?q=$request_uri;

    }

    location ~*  \.(jpg|jpeg|png|gif|ico|css|js)$ {
        expires 15d;
    }

    location ~*  \.(pdf)$ {
        expires 30d;
    }

    location /admin {
        auth_basic "Administrator Login";
        auth_basic_user_file /var/www/admin/.htpasswd;
    }

    #!!! IMPORTANT !!! We need to hide the password file from prying eyes
    # This will deny access to any hidden file (beginning with a .period)
    location ~ /\. { deny  all; }

    error_page  404              /404.html;
    location = /404.html {
        root   /usr/share/nginx/html;
    }

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }


    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        root           /var/www/wordpress;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

관련 정보