2

I'm trying to get all request to: http://example.com/downloads/* redirect to http://example.com/downloads/index.php except if the requested file exist in /downloads/

ex:

  • http://example.com/downloads => /downloads/index.php
  • http://example.com/downloads/unknowfile => /downloads/index.php
  • http://example.com/downloads/existingfile => /downloads/existingfile

My current problem is I have either the redirection to php working but static files not served or the opposite.

Here is my current vhost conf: (which redirect fine but static files are send to php and fail)

server {
    listen   80; ## listen for ipv4; this line is default and implied
    server_name domain.com;
    root /data/www;
    index index.php index.html;

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

    error_page 404 /404.html;

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

    location ^~ /downloads {
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_index index.php;
        include fastcgi_params;
        try_files $uri @downloads;
    }

    location @downloads {
        rewrite ^ /downloads/index.php;
    }

    # pass the PHP scripts to FastCGI server
    #
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }
}

Precision: static files are symlinks created by /downloads/index.php

Edit: possible duplicate, but this solution doesn't seems to work for me.

Cyrbil
  • 123
  • 1
  • 7

1 Answers1

1

Use the index.php as a custom error page in your downloads location (and don't send a 404 header):

location ^~ /downloads {
 ...
 fastcgi_intercept_errors on;
 error_page 404 =200 /downloads/index.php;

}
HBruijn
  • 72,524
  • 21
  • 127
  • 192
  • I'm failing to have php interpreting index.php, currently it just return index.php. – Cyrbil Nov 06 '13 at 15:38
  • added the line ` fastcgi_intercept_errors on` after a quick search found this post [http://stackoverflow.com/questions/13162998](http://stackoverflow.com/questions/13162998) – HBruijn Nov 06 '13 at 15:47
  • Finally solved problem by using [symfony similar conf](http://wiki.nginx.org/Symfony). I apply these rules in another vhost with a new subdomain (download.mydomain.com) instead of a subdir. I put your answer as valid, I didn't tried with fastcgi_intercept_errors but seems like the answer. – Cyrbil Nov 07 '13 at 11:03