NGINX configuration for WordPress 3.2 Single and Multiple Sites


Using WordPress with NGINX will boost page load. Configuration NGINX needed for enabling permalinks / rewrite URL in WordPress. There are 2 NGINX configuration for WordPress depend on what mode will used. If you have single WordPress only, then you should use this configuration :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
server {
         server_name lanuka.com;
         rewrite ^ http://www.lanuka.com$request_uri? permanent;
}

server {
        listen   80;
        server_name www.lanuka.com;
        root    /var/yodi/wordpress/lanuka.com;

        client_max_body_size 10m;
        access_log  /var/log/nginx/access.log;
        index  index.php index.html index.htm;

        location = /favicon.ico {
                log_not_found off;
                access_log off;
        }
 
        location = /robots.txt {
                allow all;
                log_not_found off;
                access_log off;
        }
 
        location / {
                # This is cool because no php is touched for static content
                try_files $uri $uri/ /index.php?$args;
        }


        # Add trailing slash to */wp-admin requests.
        rewrite /wp-admin$ $scheme://$host$uri/ permanent;


        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ .php$ {
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                include fastcgi_params;
        }

        location ~* .(js|css|png|jpg|jpeg|gif|ico)$ {
                expires max;
                log_not_found off;
        }

        # deny access to .htaccess files, if Apache’s document root
        # concurs with nginx’s one
        #
        location ~ /.ht {
                deny  all;
        }
}


For multiple sites, here are NGINX Configuration :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
server {

        listen   80; ## listen for ipv4

        server_name .yourdomain-here; #put dot in first of your domain name
        root        /var/www/wordpress/;
        access_log  /var/log/nginx/wordpress.access.log;
        error_log  /var/log/nginx/wordpress.error.log;

        index  index.php index.html index.htm;

        #on server block
        ##necessary if using a multi-site plugin
        server_name_in_redirect off;
        ##necessary if running Nginx behind a reverse-proxy
        port_in_redirect off;

        rewrite ^.*/files/(.*)$ /wp-includes/ms-files.php?file=$1 last;
        if (!-e $request_filename) {
             rewrite ^.+?(/wp-.*) $1 last;
             rewrite ^.+?(/.*.php)$ $1 last;
             rewrite ^ /index.php last;
        }
 
        # catch all
        error_page 404 /index.php;

        location ~ .php$ {
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                #fastcgi_pass   127.0.0.1:9000;
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME  /var/www/wordpress/$fastcgi_script_name;
                include fastcgi_params;
        }

         location ~ /.ht {
                 deny  all;
         }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.