WordPress on a subdirectory with Nginx: access to dashboard?

javipas asked:

I want to setup a WordPress blog, but not with a typical configuration:

  • The main site is at www.example.com.
    As of now, it just has a static
    index.html file with an image, we
    will use probably an index.php to
    show some information and access to a
    premium part of the site

  • The blog, with WordPress, at
    www.example.com/blog

I am setting this up under Nginx, but although I have been able to see both my static homepage at www.example.com and my blog at www.example.com/blog, I can’t access the WordPress’ admin panel, so I can’t login or write new posts.

This is the /etc/nginx/sites-enabled/www.example.com configuration file:

server {
    server_name  www.example.com;
    rewrite ^(.*) http://example.com$1 permanent;
}

server {
   listen   80;
   server_name  example.com;

   access_log  /var/www/www.example.com/log/access.log;
   error_log      /var/www/www.example.com/log/error.log info;

   index index.php;

   location / {
        set $php_root /var/www/www.example.com;
        try_files $uri $uri/ /index.php?q=$uri&$args;
        }

   location /blog {
        set $php_root /var/www/www.example.com;
        try_files $uri $uri/ /blog/index.php?q=$uri&$args;
        }

   ## Images and static content is treated differently

   location ~* ^.+.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|
rtf|js)$ {
         access_log        off;
         expires           30d;
         root /var/www/www.example.com;
    }

    location ~* .php$ {
        fastcgi_split_path_info ^(.+.php)(.*)$;
        fastcgi_pass   backend;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $php_root$fastcgi_script_name;
        include fastcgi_params;
        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_intercept_errors        on;
        fastcgi_ignore_client_abort     on;
        fastcgi_read_timeout 180;

    }


    ## Disable viewing .htaccess & .htpassword
    location ~ /.ht {
        deny  all;
    }
}
upstream backend {
              server 127.0.0.1:9000;

}

What must I do to be able to access the admin panel? I guess it has something to do with the php location, but now sure what to touch 🙁

My answer:


To fix this configuration, set a root directive in the server block:

root /var/www/www.example.com;

Then use the $document_root in your fastcgi_param:

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

The set $php_root calls can be removed. As @MartinFjordvald noted, they don’t do anything anyway.


View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.