Yanc0
Yanc0

Reputation: 201

Nginx different root in locations

http://example.com/ works fine but http://example.com/piwik give me a 404. I can't understand why this configuration doesn't work in Nginx. Here is the sites I want to serve:

/srv
  /blog
    index.php
  /piwik
    index.php

Then the config file:

server {
    charset utf8;

    index index.php;
    server_name example.com;
    root /srv/blog;
    client_max_body_size 10M;

    # Deny all ht file useless in nginx
    location ~ /\.ht* {
            deny all;
    }

    location /images {
            alias /srv/blog/images;
    }

    location /piwik {
            index index.php;
            alias /srv/piwik;
    }

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

    location ~ ^(.+?\.php)(/.*)?$ {
        try_files $1 = 404;
        include fastcgi_params;
        fastcgi_param PATH_INFO $2;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
    }
}

When I activate debug log in nginx, I can see this strange lines:

2014/05/18 14:27:08 [debug] 19676#0: *1 open index "/srv/piwik/index.php"
2014/05/18 14:27:08 [debug] 19676#0: *1 internal redirect: "/piwik/index.php?"
2014/05/18 14:27:08 [debug] 19676#0: *1 rewrite phase: 1
[...]

Where are my errors?

Thanks ;)

Upvotes: 2

Views: 6257

Answers (1)

Yanc0
Yanc0

Reputation: 201

Ok, the problem seems to come with

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

I removed this section, clean the config and it works. All the config is here.

server {
    listen *:443 ssl;

    index index.php;
    server_name example.com;
    # We set the default root to /srv/blog
    root /srv/blog;

    location / {
        # Get beautiful urls for the blog
        try_files $uri $uri/ /index.php?$args;
    }

    location /piwik {
        # alias does not append location path to the filepath
        alias /srv/piwik;

        # catch piwik php files and pass it to php-fpm
        location ~ /piwik/(.*\.php)(/.*)?$ {
            include fastcgi_params;
            fastcgi_param HTTPS on;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
        }
    }

    # catch php files and pass it to php-fpm
    location ~ (.*\.php)(/.*)?$ {
        include fastcgi_params;
        fastcgi_param HTTPS on;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
    }
}

Upvotes: 3

Related Questions