用Nginx缓存给ProcessWire项目优化加速

相关资源:

Nginx的缓存加速在《用Nginx fastcgi_cache缓存为你的PHP网站加速》一文中已经讲过,实际上优化ProcessWire项目也很简单,只需要.conf文件修改如下:

(轻注意修改相关域名和配置)

fastcgi_cache_path /mnt/www/yourdomain.com/nginx_cache levels=1:2 keys_zone=yourdomain:100m inactive=30d max_size=10g;

server {
	listen 80;
	#listen 443 ssl;

	root /mnt/www/yourdomain.com/public_html;
	server_name yourdomain.com www.yourdomain.com;
	#ssl_certificate /etc/pki/tls/certs/example.com.crt;
	#ssl_certificate_key /etc/pki/tls/private/example.com.key;

	client_max_body_size 50m;
	#access_log /mnt/www/yourdomain.com/_logs/access.log;
	#error_log /mnt/www/yourdomain.com/_logs/error.log;

	# -----------------------------------------------------------------------------------------------
	# Set default directory index files
	# -----------------------------------------------------------------------------------------------

	index index.php index.html index.htm;

	# -----------------------------------------------------------------------------------------------
	# Optional: Redirect users to the 'www.' version of the site (uncomment to enable).
	# For example: http://processwire.com/ would be redirected to http://www.processwire.com/
	# -----------------------------------------------------------------------------------------------

	if ($host !~* ^www\.) {
		rewrite ^(.*)$ $scheme://www.$host$1 permanent;
	}

	# -----------------------------------------------------------------------------------------------
	# Access Restrictions: Protect ProcessWire system files
	# -----------------------------------------------------------------------------------------------

	# Block access to ProcessWire system files
	location ~ \.(inc|info|module|sh|sql)$ {
		deny all;
	}

	# Block access to any file or directory that begins with a period
	location ~ /\. {
		deny all;
	}

	# Block access to protected assets directories
	location ~ ^/(site|site-[^/]+)/assets/(cache|logs|backups|sessions|config|install|tmp)($|/.*$) {
		deny all;
	}

	# Block acceess to the /site/install/ directory
	location ~ ^/(site|site-[^/]+)/install($|/.*$) {
		deny all;
	}

	# Block dirs in /site/assets/ dirs that start with a hyphen
	location ~ ^/(site|site-[^/]+)/assets.*/-.+/.* {
		deny all;
	}

	# Block access to /wire/config.php, /site/config.php, /site/config-dev.php, and /wire/index.config.php
	location ~ ^/(wire|site|site-[^/]+)/(config|index\.config|config-dev)\.php$ {
		deny all;
	}

	# Block access to any PHP-based files in /templates-admin/
	location ~ ^/(wire|site|site-[^/]+)/templates-admin($|/|/.*\.(php|html?|tpl|inc))$ {
		deny all;
	}

	# Block access to any PHP or markup files in /site/templates/
	location ~ ^/(site|site-[^/]+)/templates($|/|/.*\.(php|html?|tpl|inc))$ {
		deny all;
	}

	# Block access to any PHP files in /site/assets/
	location ~ ^/(site|site-[^/]+)/assets($|/|/.*\.php)$ {
		deny all;
	}

	# Block access to any PHP files in core or core module directories
	location ~ ^/wire/(core|modules)/.*\.(php|inc|tpl|module)$ {
		deny all;
	}

	# Block access to any PHP files in /site/modules/
	location ~ ^/(site|site-[^/]+)/modules/.*\.(php|inc|tpl|module)$ {
		deny all;
	}

	# Block access to any software identifying txt files
	location ~ ^/(COPYRIGHT|INSTALL|README|htaccess)\.(txt|md)$ {
		deny all;
	}

	# Block all http access to the default/uninstalled site-default directory
	location ~ ^/site-default/ {
		deny all;
	}

	# -----------------------------------------------------------------------------------------------
	# If the request is for a static file, then set expires header and disable logging.
	# Give control to ProcessWire if the requested file or directory is non-existing.
	# -----------------------------------------------------------------------------------------------

	location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|eot|woff|ttf)$ {
		expires 24h;
		log_not_found off;
		access_log off;
		try_files $uri $uri/ /index.php?it=$uri&$args;
	}

        # -----------------------------------------------------------------------------------------------
        # Wordpress
        # -----------------------------------------------------------------------------------------------

        location /old_version/ {
            try_files $uri $uri/ /old_version/index.php?$args;
        }	

	# -----------------------------------------------------------------------------------------------
	# This location processes all other requests. If the request is for a file or directory that
	# physically exists on the server, then load the file. Else give control to ProcessWire.
	# -----------------------------------------------------------------------------------------------

	set $skip_cache 0;

        # POST requests and urls with a query string should always go to PHP
        if ($request_method = POST) {
            set $skip_cache 1;
        }  

        #if ($query_string != "") {
        #    set $skip_cache 1;
        #}  
 
        # Don't cache uris containing the following segments
        #这里需要修改不缓存的目录,比如后台
        if ($request_uri ~* "/admin/") {
            set $skip_cache 1;
        }  
 
        # Don't use the cache for logged in users or recent commenters
        #登陆状态不需要缓存
        if ($http_cookie ~* "wire_challenge") {
            set $skip_cache 1;
        }


	location / {
		try_files $uri $uri/ /index.php?it=$uri&$args;
	}

	# -----------------------------------------------------------------------------------------------
	# Pass .php requests to fastcgi socket
	# -----------------------------------------------------------------------------------------------

	location ~ \.php$ {

		# Check if the requested PHP file actually exists for security
		try_files $uri =404;

		# Fix for server variables that behave differently under nginx/php-fpm than typically expected
		#fastcgi_split_path_info ^(.+\.php)(/.+)$;

		# Set environment variables
		#include fastcgi_params;
		#fastcgi_param PATH_INFO $fastcgi_path_info;
		#fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

		# Pass request to php-fpm fastcgi socket
		#fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;

                fastcgi_connect_timeout      180;
                fastcgi_read_timeout            600;
                fastcgi_send_timeout            600;
               
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;

                #fastcgi_read_timeout = 300;            
   
		# Caching Content
		fastcgi_cache_bypass $skip_cache;
		fastcgi_no_cache $skip_cache;

		fastcgi_cache yourdomain;
		
		# Params
		fastcgi_cache_valid 200 301 302 12h;
		fastcgi_cache_valid 404 500 502 503 504 0s;
		fastcgi_cache_valid any 1m;
		fastcgi_cache_min_uses 1;
		fastcgi_cache_use_stale error timeout invalid_header http_500 http_503 updating;
		
                fastcgi_pass_header Set-Cookie;
                fastcgi_pass_header Cookie;
                fastcgi_ignore_headers Cache-Control Expires Set-Cookie;

		add_header X-Cache "$upstream_cache_status - $upstream_response_time";
		fastcgi_cache_key "$scheme$request_method$host$request_uri";


	}
}

Post Comment