Nginx give access to subdirectory

We’re running a WordPress site on a Nginx server, I am now trying to install Piwik there in the /stats folder.

This is the default Nginx configuration that came with the install, I added the “/stats” block myself, but it doesn’t work – it gets rendered by WordPress whenever I go to mysite.com/stats instead of going to that folder.

Read More

Desired behavior would be that the /stats subdirectory (and all files and directories in it) is just parsed by PHP as would be on a default install without Nginx rules

Any clue what I’m missing?

server_name  _;
port_in_redirect off;

client_header_buffer_size 4k;
client_body_buffer_size 128k;
client_max_body_size 16m;

root   /var/www/html;
index  index.html index.php;

charset utf-8;

log_not_found off;

gzip_static on;
gzip_types text/css application/javascript text/xml;
gzip_vary on;
gzip on;


# redirect server error pages to the static page /50x.html
#
error_page   500 503 504 /50x.html;
location = /50x.html {
    root   /usr/share/nginx/html;
}

location @rewrite {
    rewrite ^.*$ /index.php?$args;
}

error_page  404 @rewrite;

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

# WP Multisite rewrites
rewrite /([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) /$2 last;
rewrite /([_0-9a-zA-Z-]+/)?(.*.php)$ /$2 last;

location / {
    try_files $uri $uri.gz $uri/ @rewrite;
}

location ~ .sql$ {
    rewrite ^.*$ /index.php?$args;
}

# We do not want to run php from wp uploads
location ~* /(?:uploads|files)/.*.php$ {
    rewrite ^.*$ /index.php?$args;
}

location ~ .php$ {
    fastcgi_split_path_info  ^(.+.php)(/.+)$;
    if (!-f $document_root$fastcgi_script_name) {
        return 404;
    }
    fastcgi_pass             127.0.0.1:9000;
    fastcgi_index            index.php;
    include                  fastcgi_params;
    fastcgi_param            SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_intercept_errors off;
}

location /stats {
    try_files $uri $uri/ /index.php?$args;
    index index.php;
}

location = /favicon.ico {
    access_log off;
    expires 2w;
    add_header Cache-Control public;
    try_files $uri @rewrite;
}

location ~* .(js|css|jpg|jpeg|png|gif|ico|woff|woff2|ttf|otf|eot|pdf|xml|mp4|ogg|mp3|mov|wmv|avi|cur|rtf|txt|swf)$ {
    add_header Cache-Control public;
    add_header Access-Control-Allow-Origin *;
    expires 2w;
    try_files $uri $uri.gz;
}

Related posts

1 comment

  1. The rules for multi-site WordPress, particularly this one: rewrite /([_0-9a-zA-Z-]+/)?(.*.php)$ /$2 last; will redirect any /stats/index.php URI back to WordPress’s /index.php.

    If you are not using a multi-site WordPress, you can safely delete the redundant rewrite rules.

    If you are using a multi-site WordPress, some redesign is required.

Comments are closed.