Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a little dilemma. I wrote a custom PHP MVC framework and built a CMS on top of it. I decided to give nginx+fpm a whirl too. Which is the root of my dilemma. I was asked to incorporate a wordpress blog into my website (yah.) It has much content and it's not feasible in the short amount of time I have to bring all the content into my CMS. Because of using Apache for years, I'm, admittedly, a little lost using nginx.

My website has the file path:

/opt/directories/mysite/public/

The wordpress files are located at:

/opt/directories/mysite/news/

I know I just need to setup location(s) that targets /news[/*] and then forces all matching URI's to the index.php within. Can someone point me in the right direction perhaps?

My configuration is below:

server {

        listen   80;
        server_name staging.mysite.com

        index   index.php;

        root /opt/directories/mysite/public;

        access_log  /var/log/nginx/mysite/access.log;
        error_log   /var/log/nginx/mysite/error.log;

        add_header  X-NodeName  directory01;

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

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

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

        location ~ /news {
                try_files $uri $uri/ @news;
        }

        location @news {
                fastcgi_pass unix:/tmp/php-fpm.sock;
                fastcgi_split_path_info ^(/news)(/.*)$;
                fastcgi_param SCRIPT_FILENAME /opt/directories/mysite/news/index.php;
                fastcgi_param PATH_INFO $fastcgi_path_info;
        }

        include fastcgi_params;
        include php.conf;

        location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|xml)$ {
                access_log        off;
                expires           30d;
        }

        ## Disable viewing .htaccess & .htpassword
        location ~ /\.ht {
                deny  all;
        }
}

My php.conf file:

location ~ \.php {
        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  SCRIPT_FILENAME    $document_root$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;

        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;

        fastcgi_pass unix:/tmp/php-fpm.sock;

    # If you must use PATH_INFO and PATH_TRANSLATED then add 
    # the following within your location block above 
    # (make sure $ does not exist after \.php or /index.php/some/path/ will not match):

    #fastcgi_split_path_info ^(.+\.php)(/.+)$;
    #fastcgi_param  PATH_INFO          $fastcgi_path_info;
    #fastcgi_param  PATH_TRANSLATED    $document_root$fastcgi_path_info;
}

fastcgi_params file:

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;

Thanks, in large part, to @Kromey, I have adjusted my location /news/ but I am still not getting the desired result.

I was able to learn to tack a ~ my /news location as I discovered that my php location was being matched first.

With this setup, I now get a 200 status, but the page is blank. Any ideas?

share|improve this question
    
I did not take this problem to completion. I had issues with the wordpress installation itself. They were of a php-fpm kind and had nothing to do with nginx. I ended up doing a load balanced proxy from two other apache hosts. This works quite well. –  Bretticus Aug 17 '11 at 20:23
add comment

1 Answer

This is actually quite simple, and a heckuva lot more compact than the similar setup for Wordpress on Apache (I run 2 Wordpress blogs, both previously on Apache and both recently migrated to nginx+PHP (not FPM, although I did have that running for a time before deciding it was overkill for my low-traffic sites)).

location /news/ {
    #Need a new root to point to your "aliased" directory
    root /opt/directories/news
    # This is cool because no php is touched for static content
    try_files $uri $uri/ /index.php;
}

As the comment implies, any static content (images, CSS, JavaScript, robots.txt, etc.) that lives inside your Wordpress path gets served directly, with no PHP involvement; everything else gets routed to index.php.

Note that the try_files directive is semi-new (0.8 I think?), so you may have to upgrade your nginx; when I installed from the Ubuntu apt repositories, it was too old a version to do this, and the configuration was so much more complex that I apt-get removed it and installed nginx from source (a rather simple operation).

share|improve this answer
    
Ah crud, I am using the Debian package. It shows... nginx -v nginx version: nginx/0.6.32 Thanks for your help @Kromey. You rock! Better figure out how to upgrade. –  Bretticus Apr 8 '11 at 18:39
    
Yeah, I very strongly suggest you remove it and then install from source. It's not hard to do at all, and keeps your Wordpress config quite simple. The alternative involves ugly if statements (which the nginx community tries to avoid like the plague); see this link, although you'll of course have to change the location and paths for your environment: joneslee85.wordpress.com/2010/03/13/… –  Kromey Apr 8 '11 at 18:42
    
Thanks, I was able to build a custom Debian package (which is nice because I can quickly deploy to my farm servers.) The link is debian-administration.org/articles/666 btw. I got it installed and I'm about to test it with the config you gave me. –  Bretticus Apr 8 '11 at 19:10
    
Just for documentation sake. After installing my new Debian package and trying to restart, I got the following error: directory01:~/src# /etc/init.d/nginx restart Restarting nginx: [emerg]: mkdir() "/usr/local/nginx/uwsgi_temp" failed (2: No such file or directory) nginx. I just had to make the folder mkdir -p /usr/local/nginx/uwsgi_temp and nginx restarted fine. –  Bretticus Apr 8 '11 at 19:19
    
@Kromey, any idea why I get: No input file specified. When I surf to: http://mysite.tld/news/ ? Just for fun, I entered the following in my console: cat /opt/directories/news/index.php The contents of the WP index.php file are shown. –  Bretticus Apr 8 '11 at 19:27
show 11 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.