Search code examples
phpnginxfastcgi

nginx, how to serve all static files and forward all other traffic to a fastcgi


I want to directly serve all existent files (except .php files)

And the rest of ALL requests (not only .php), forward them to a fastcgi server.

I can redirect all .php files to a fastcgi with:

location ~ \.php$ {
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
}

And serve other static files with:

location / {
}

But I want to serve first "all static files that exists and don't end with .php" And then, serve "all other requests" (not necessary ending with php) to a fastcgi,

Any idea to accomplish it?

Thanks!


Solution

  • You can use try_files for that.

    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }
    
    location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
    
        // other CGI parameters
    }
    

    Make sure you're aware of common pitfalls.