Symfony, WordPress and nginx – configuration details and setup
After several hours of trial and error, we finally figured out a configuration that works for running Symfony and WordPress together on the same web site using the nginx web server. This post had a lot of useful information, but none of it worked perfectly for us, so this is what we came up with:
##
## domain.com
##
server {
listen 80;
server_name domain.com;
access_log /home/logs/domain/access.log;
error_log /home/logs/domain/error.log notice;
root /home/www/sfprojects/domain/web;
index index.php;
charset utf-8;
## configuration for the WordPress blog with the WP-SuperCache plugin
## (remove if not using WordPress)
location /blog {
# if the requested file exists, return it immediately
if (-f $request_filename) {
break;
}
set $supercache_file '';
set $supercache_uri $request_uri;
if ($request_method = POST) {
set $supercache_uri '';
}
# Using pretty permalinks, so bypass the cache for any query string
if ($query_string) {
set $supercache_uri '';
}
if ($http_cookie ~* "comment_author_|wordpress|wp-postpass_" ) {
set $supercache_uri '';
}
# if we haven't bypassed the cache, specify our supercache file
if ($supercache_uri ~ ^(.+)$) {
set $supercache_file /blog/wp-content/cache/supercache/$http_host/$1index.html;
}
# only rewrite to the supercache file if it actually exists
if (-f $document_root$supercache_file) {
rewrite ^(.*)$ $supercache_file break;
}
# all other requests go to WordPress
if (!-e $request_filename) {
rewrite ^(.*)$ /blog/index.php?q=$1 last;
break;
}
}
location / {
# If the file exists as a static file serve it directly without
# running all the other rewite tests on it
if (-f $request_filename) {
expires max;
break;
}
if ($request_filename !~ "\.(js|htc|ico|gif|jpg|png|css)$") {
rewrite ^(.*) /index.php last;
}
}
location ~ "^(.+\.php)($|/)" {
set $script $uri;
set $path_info "";
if ($uri ~ "^(.+\.php)($|/)") {
set $script $1;
}
if ($uri ~ "^(.+\.php)(/.+)") {
set $script $1;
set $path_info $2;
}
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /home/www/sfprojects/domain/web$script;
fastcgi_param SCRIPT_NAME $script;
fastcgi_param PATH_INFO $path_info;
}
location /sf/ {
root /home/www/sfprojects/domain/lib/vendor/symfony/data/web/;
}
error_page 404 /404.html;
location = /404.html {
root /usr/local/nginx/html;
internal;
}
}
And this is the content of our fastcgi_params file which we include above:
fastcgi_param QUERY_STRING $query_string; fastcgi_param REQUEST_METHOD $request_method; fastcgi_param CONTENT_TYPE $content_type; fastcgi_param CONTENT_LENGTH $content_length; fastcgi_param SCRIPT_NAME $fastcgi_script_name; fastcgi_param REQUEST_URI $request_uri; fastcgi_param DOCUMENT_URI $document_uri; fastcgi_param DOCUMENT_ROOT $document_root; fastcgi_param SERVER_PROTOCOL $server_protocol; fastcgi_param GATEWAY_INTERFACE CGI/1.1; fastcgi_param SERVER_SOFTWARE nginx/$nginx_version; fastcgi_param REMOTE_ADDR $remote_addr; fastcgi_param REMOTE_PORT $remote_port; fastcgi_param SERVER_ADDR $server_addr; fastcgi_param SERVER_PORT $server_port; fastcgi_param SERVER_NAME $server_name; # PHP only, required if PHP was built with --enable-force-cgi-redirect fastcgi_param REDIRECT_STATUS 200; fastcgi_connect_timeout 60; fastcgi_send_timeout 180; fastcgi_read_timeout 180; fastcgi_buffer_size 128k; fastcgi_buffers 4 256k; fastcgi_busy_buffers_size 256k; fastcgi_temp_file_write_size 256k; fastcgi_intercept_errors on;
Hope this proves helpful to someone going through the same problems!
