2016-10-25 27 views
0

NginxでWordPressを使用していますが、静的ファイルのキャッシュを有効にしようとすると、404が見つからないことがあります。Nginxのキャッシュ404

これは私の/etc/nginx/conf.d/default.confファイルです:

server { 
    listen 80; 
    server_name _; 

# SSL configuration 
listen 443 ssl default_server; 
ssl_certificate /etc/letsencrypt/live/shivampaw.com/fullchain.pem; 
ssl_certificate_key /etc/letsencrypt/live/shivampaw.com/privkey.pem; 
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; 
ssl_prefer_server_ciphers on; 
ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH'; 
ssl_dhparam /etc/nginx/ssl/dhparams.pem; 
ssl_session_timeout 30m; 
ssl_session_cache shared:SSL:10m; 
ssl_buffer_size 8k; 
add_header Strict-Transport-Security max-age=31536000; 

    location/{ 
     root /home/shivam/sites/shivampaw.com; 
     index index.php index.html index.htm; 
     try_files $uri $uri/ /index.php?$args; 
    } 

    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   /home/shivam/sites/shivampaw.com; 
     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; 
    #} 

gzip on; 
gzip_vary on; 
gzip_proxied any; 
gzip_comp_level 9; 
gzip_buffers 16 8k; 
gzip_http_version 1.1; 
gzip_types text/plain text/css application/json applicationx-javascript text/xml application/xml application/xml+rss text/javascript; 

include wordpress/wordpress.conf; 

そして、これがWordPress.confファイルです:それと

location = /favicon.ico { 
     log_not_found off; 
     access_log off; 
} 

location = /robots.txt { 
     allow all; 
     log_not_found off; 
     access_log off; 
} 

# Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac). 
# Keep logging the requests to parse later (or to pass to firewall utilities such as fail2ban) 
location ~ /\. { 
     deny all; 
} 

# Deny access to any files with a .php extension in the uploads directory 
# Works in sub-directory installs and also in multisite network 
# Keep logging the requests to parse later (or to pass to firewall utilities such as fail2ban) 
location ~* /(?:uploads|files)/.*\.php$ { 
     deny all; 
} 


# Add trailing slash to */wp-admin requests. 
rewrite /wp-admin$ $scheme://$host$uri/ permanent; 

、それが正常に動作します。私はwordpress.confの目的を達成するために

# Media: images, icons, video, audio, HTC 
location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ { 
    expires 1M; 
    access_log off; 
    add_header Cache-Control "public"; 
} 

# CSS and Javascript 
location ~* \.(?:css|js)$ { 
    expires 1y; 
    access_log off; 
    add_header Cache-Control "public"; 
} 

を追加し、nginxのを再起動し、すべての静的ファイルが404

を思い付く、私のサイトをリロードした場合でも、だから私は、そのキャッシュをコメントアウトする必要があり、それが正常に動作します。

アイデア?

404sSingle 404

これは私が(私はそれを使用)WP-rocket.confを含む場合WP-rocket.confがそれにものをキャッシュしているので、それはまた、404を与えることを意味します。

基本的に、なぜ私は静的ファイルをキャッシュできないのですか?

+0

ルートロケーションブロックにルートを定義しているからです。だから、他の場所ブロックはそれについて知りません。これは悪い習慣です([here](https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/)参照)。これらのロケーションブロックは、ファイルを探す場所を知らない。 'location/{'ブロックの上に 'root /home/shivam/sites/shivampaw.com;'を置き、それぞれの静的ファイルに 'try_files $ uri $ uri /index.php?$ args'を追加する必要がありますブロック。これを行う別の方法は、あなたの設定を保持するが、各ブロックに 'alias/path/to/static/files /; 'を追加することです。 –

+0

ありがとう@KeenanLawrence - 答えとして投稿すること自由に感じてください:) – Shiv

+0

それはお願いします。答えを受け入れてください:) –

答えて

2

ルートロケーションブロックにルートを定義しているからです。だから、他の場所ブロックはそれについて知りません。これは悪い習慣です(here参照)。これらのロケーションブロックは、ファイルを探す場所を知らない。

ブロックlocation/{の上にroot /home/shivam/sites/shivampaw.com;ブロックを置き、次にそれぞれの静的ブロックにtry_files $uri $uri/ /index.php?$argsを追加する必要があります。

もう1つの方法は設定を保存することですが、各ブロックにalias /path/to/static/files/;を追加してください。