monecchi

Nginx + WP installed on custom directory + Agressive Caching

Feb 5th, 2014
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. # You may add your server blocks inside "server brackets" { }
  2.  
  3. server {
  4. listen 80;
  5.  
  6. root /var/www/example.com/public_html;
  7. index index.php index.html index.htm;
  8.  
  9. server_name www.example.com example.com;
  10.  
  11. #The location block below will make sure if a some other file/directory exist, it will be served first.
  12. #If that fails, request will be checked inside /custom-wp-directory/ i.e. /blog/ directory.
  13. #So, if you install WP inside a /custom/ dir. instead of the root dir. it will work out of the box!
  14. #More info: https://rtcamp.com/support/topic/accessing-non-wordpress-directory-in-website-root/
  15.  
  16. location / {
  17. try_files $uri $uri/ /custom-wp-directory/index.php?q=$uri&$args;
  18. }
  19.  
  20. # Define default caching of 24h
  21. expires 86400s;
  22. add_header Pragma public;
  23. add_header Cache-Control "max-age=86400, public, must-revalidate, proxy-revalidate";
  24.  
  25. # Add trailing slash to */wp-admin requests.
  26. rewrite /wp-admin$ $scheme://$host$uri/ permanent;
  27.  
  28. # deliver a static 404
  29. error_page 404 /404.html;
  30.  
  31. error_page 500 502 503 504 /50x.html;
  32. location = /50x.html {
  33. root /usr/share/nginx/www;
  34. }
  35.  
  36. # Don't log robots.txt requests
  37. location = /robots.txt {
  38. allow all;
  39. log_not_found off;
  40. access_log off;
  41. }
  42.  
  43. # Rewrite for versioned CSS+JS via filemtime
  44. location ~* ^.+\.(css|js)$ {
  45. rewrite ^(.+)\.(\d+)\.(css|js)$ $1.$3 last;
  46. expires 31536000s;
  47. access_log off;
  48. log_not_found off;
  49. add_header Pragma public;
  50. add_header Cache-Control "max-age=31536000, public";
  51. }
  52.  
  53.  
  54. # Aggressive caching for static files
  55. # If you alter static files often, please uncomment the add_header Cache-Control and use it
  56. add_header Cache-Control "max-age=31536000, public, must-revalidate, proxy-revalidate";
  57. location ~* \.(asf|asx|wax|wmv|wmx|avi|bmp|class|divx|doc|docx|eot|exe|gif|gz|gzip|ico|jpg|jpeg|jpe|mdb|mid|midi|mov|qt|mp3|m4a|mp4|m4v|mpeg|mpg|mpe|mpp|odb|odc|odf|odg|odp|ods|odt|ogg|ogv|otf|pdf|png|pot|pps|ppt|pptx|ra|ram|svg|svgz|swf|tar|t?gz|tif|tiff|ttf|wav|webm|wma|woff|wri|xla|xls|xlsx|xlt|xlw|zip)$
  58. {
  59. expires 31536000s;
  60. access_log off;
  61. log_not_found off;
  62. add_header Pragma public;
  63. add_header Cache-Control "max-age=31536000, public";
  64. }
  65.  
  66.  
  67. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  68. location ~ \.php$ {
  69. try_files $uri =404;
  70. #fastcgi_pass 127.0.0.1:9000;
  71. # With php5-fpm:
  72. fastcgi_pass unix:/var/run/php5-fpm.sock;
  73. fastcgi_index index.php;
  74. include fastcgi_params;
  75. }
  76.  
  77.  
  78. }
Advertisement
Add Comment
Please, Sign In to add comment