Search code examples
phpnginxnginx-locationphp-5.4

Nginx downloading files


I have a server (nginx/1.6.0 PHP 5.5.14) with these settings that works correctly. The problem is when I use the location ^~ /sys/ When I try to access the sys folder it downloads the index. If I remove location ^~ / sys / back to working normally. he error only happens with php files. html files works normally. Where is the error?

server {
    server_name  site.com;
    root /home/www/site.com;
    index index.php index.html index.htm;

    location / { 
        index index.php index.html;
        try_files $uri $uri/ /index.php; 
    }

    location ~ [^/]\.php(/|$) {
        fastcgi_pass unix:/data/php5514/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_read_timeout 300;
        include fastcgi_params;
    }

    location ^~ /sys/ {
    }
}

I'm using a script and need to do this setup in nginx to protect a folder from unauthorized access.

location ^~ /FOLDER/ {
    if ($cookie_amember_nr !~* [a-zA-Z0-9]+) { #not authorized
        rewrite ^(.*)$ http://EXAMPLE.COM/AMEMBER/protect/new-rewrite?f=FOLDERID&url=$request_uri?$args redirect;
    }
    set $file $document_root/AMEMBER/data/new-rewrite/$cookie_amember_nr-FOLDERID;
    if (!-f $file) { #have not access
        rewrite ^(.*)$ http://EXAMPLE.COM/AMEMBER/no-access/folder/id/FOLDERID?url=$request_uri?$args redirect;
    }    
    #everything is ok
}

But this problem with location ^ ~ does not work.


Solution

  • You should explain more detailed what you actually expect.

    Assuming you want to serve php content at /sys/, you must also put the fastcgi_pass block in this location context like so:

    location ^~ /sys/ {
         fastcgi_pass unix:/data/php5514/var/run/php5-fpm.sock;
         fastcgi_index index.php;
         fastcgi_read_timeout 300;
         include fastcgi_params;
    }
    

    If that's what you want, you'd propably want to use upstream:

    upstream php { server unix:/data/php5514/var/run/php5-fpm.sock; }
    

    and reference it as

    location ^~ /sys/ {
         fastcgi_pass http://php;
    }
    

    on both/all location blocks serving php.

    Remember, exact location matches win over less exact/almost matches. Maybe your GET request to /sys/something.php does not get matched by the php-location block, but by the /sys-location block. What's the use of the sys-location block anyway, if you don't put something in there like a different root?