
CentOS 6 と nginx を搭載した独自の VPS があり、キャッシュを有効にしたいと考えています。テストするには、Google PageSpeed Insight を使用して、キャッシュが正常に有効になっているかどうかを確認します。問題は、キャッシュを有効にする場所や、たとえば画像をキャッシュする期間などを設定できる場所について、あまり経験がないことです。インターネットで見つけて、これまでに試したのは次のとおりです。
- ディレクトリを作成しています。
/etc/nginx/sites-available
何らかの/etc/nginx/sites-enabled
理由で存在しなかったためです。 - ここで作成されたディレクトリをリンクします。ファイルの末尾に最後
/etc/nginx/nginx.conf
のinclude /etc/nginx/sites-enabled/*;
}
ファイルの作成
/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; }
}
conf ファイルのリンク:
ln -s /etc/nginx/sites-available/my-site.com.conf /etc/nginx/sites-enabled/my-site.com.conf
- する
service nginx restart
私は自分のウェブサイトをWordPressで使用しています。
そのため、PageSpeed Insight やその他のページスピード ツールを使用してページをテストするたびに、header.png、javascripts などにキャッシュを使用していないと表示されます。ただし、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;
#}
}