Skip to content

Commit

Permalink
* Added setup for serving PHP with Apache and a cache for it.
Browse files Browse the repository at this point in the history
  • Loading branch information
António P. P. Almeida committed Oct 30, 2011
1 parent d4bd108 commit 80f148c
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 14 deletions.
25 changes: 17 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,10 @@
assumes the loopback `127.0.0.1` interface on port
`8080`. Adjust accordingly to reflect your setup.

Comment out **all** `fastcgi_pass` directives in either
`drupal_boost.conf` or `drupal_boost_drush.conf`, depending
which config layout you are using. Uncomment out all the
`proxy_pass` directives. They have a comment around them,
stating these instructions.
Comment out **all** `fastcgi_pass` directives in
`stats.example.com.conf` Uncomment out all the `proxy_pass`
directives. They have a comment around them, stating these
instructions.

+ FastCGI process using php-cgi. In this case an
[init script](https://github.com/perusio/php-fastcgi-debian-script
Expand Down Expand Up @@ -103,10 +102,20 @@
`php-fpm` and that you should **change** to reflect your setup
by editing `upstream_phpcgi.conf`.

5. Create the `/var/cache/nginx/fcgicache` directory if you're
serving PHP with php-fpm or php cgi. This directory must be
owned by the unpriveleged nginx user. In debian it's `www-data`.
5. Setup the cache for `piwik.php`. It depends if you use either
FastCGI or Apache for processing PHP.

+ **FastCGI**: Create the `/var/cache/nginx/fcgicache` directory
if you're serving PHP with php-fpm or php cgi. This directory
must be owned by the unpriveleged nginx user. In debian it's
`www-data`.

+ **Apache**: Create the `/var/cache/nginx/proxycache` directory
if you're serving PHP with Apache. This directory must be owned
by the unpriveleged nginx user. In debian it's
`www-data`. Comment out all the lines where `fcgi_cache` is
referenced. You must uncomment the line `include
proxy_cache_zone.conf;` on `nginx.conf`.

6. Create the `/etc/nginx/sites-enabled` directory and enable the
virtual host using one of the methods described below.
Expand Down
10 changes: 5 additions & 5 deletions nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,10 @@ http {
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;

## For the filefield_nginx_progress module to work. From the
## README. Reserve 1MB under the name 'uploads' to track uploads.
upload_progress uploads 1m;

## Enable clickjacking protection in modern browsers. Available in
## IE8 also. See
## https://developer.mozilla.org/en/The_X-FRAME-OPTIONS_response_header
add_header X-Frame-Options sameorigin;
add_header X-Frame-Options SAMEORIGIN;

## Include the upstream servers for PHP FastCGI handling config.
include upstream_phpcgi.conf;
Expand All @@ -106,6 +102,10 @@ http {
#include reverse_proxy.conf;
#include upstream_phpapache.conf;

## Proxy cache zone definition.
## Uncomment to enable.
#include proxy_cache_zone.conf;

## Include the php-fpm status allowed hosts configuration block.
## Uncomment to enable if you're running php-fpm.
#include php_fpm_status_allowed_hosts.conf;
Expand Down
16 changes: 16 additions & 0 deletions proxy_cache_zone.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# -*- mode: nginx; mode: flyspell-prog; ispell-local-dictionary: "american" -*-

### Defining the proxy cache zone for Piwik's piwik.php file. That's
### the file that the JS bug calls.

## If youre using a Nginx version greater than 1.1.1 then you can
## tweak the Tweaking of the cache loader parameters.
## Cf. http://forum.nginx.org/read.php?21,213197,213209#msg-213209 for
## rationale. If you're using a Nginx version lower than 1.1.1 then
## comment the line below and use the cache zone configuration below this one.
proxy_cache_path /var/cache/nginx/proxycache levels=1:2 keys_zone=proxycache:100k max_size=10M inactive=3h loader_threshold=2592000000 loader_sleep=1 loader_files=100000;

## If you're not using a Nginx version greater or equal to 1.1.1 then
## comment the above configuration and use this one. No cache loader
## tweaking.
#proxy_cache_path /var/cache/nginx/proxycache levels=1:2 keys_zone=proxycache:100k max_size=10M inactive=3h;
File renamed without changes.
30 changes: 30 additions & 0 deletions sites-available/proxy_cache.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# -*- mode: nginx; mode: flyspell-prog; ispell-local-dictionary: "american" -*-

### Implementation of the microcache concept as presented here:
### http://fennb.com/microcaching-speed-your-app-up-250x-with-no-n

## The cache zone referenced.
proxy_cache proxycache;
## The cache key.
proxy_cache_key $host$request_uri;

## For 200 and 301 make the cache valid for 2 hours.
proxy_cache_valid 200 301 2h;
## For 302 make it valid for 30 minutes.
proxy_cache_valid 302 30m;
## For 404 make it valid 10 minutes.
proxy_cache_valid 404 10m;
## If there are any upstream errors or the item has expired use
## whatever it is available.
proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504;
## The Cache-Control and Expires headers should be delivered untouched
## from the upstream to the client.
proxy_ignore_headers Cache-Control Expires;
## If we have a cookie we should bypass the cache. The same if we have a
proxy_cache_bypass $no_cache;
proxy_no_cache $no_cache;
## Add a cache miss/hit status header.
add_header X-Piwik-Cache $upstream_cache_status;
## To avoid any interaction with the cache control headers we expire
## everything on this location immediately.
expires epoch;
10 changes: 9 additions & 1 deletion sites-available/stats.example.com.conf
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ server {
tcp_nodelay off;
## fell swoop.
## Set the OS file cache.
open_file_cache max=100 inactive=120s;
open_file_cache max=500 inactive=120s;
open_file_cache_valid 45s;
open_file_cache_min_uses 2;
open_file_cache_errors off;
Expand All @@ -70,13 +70,21 @@ server {
## Relay all index.php requests to fastcgi.
location = /index.php {
fastcgi_pass phpcgi;
## To use Apache for serving PHP uncomment the line bellow and
## comment out the above.
#proxy_pass http://phpapache;
}

## Relay all piwik.php requests to fastcgi.
location = /piwik.php {
fastcgi_pass phpcgi;
## FastCGI cache.
include sites-available/fcgi_cache.conf;
## To use Apache for serving PHP uncomment the line bellow and
## comment out the above.
#proxy_pass http://phpapache;
## Proxy cache.
#include sites-available/proxy_cache.conf;
}

## Any other attempt to access PHP files returns a 404.
Expand Down

0 comments on commit 80f148c

Please sign in to comment.