Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a PHP file in my /batch/ folder I run with an hourly CRON job. Unfortunately this process can often take a few minutes to complete so I have had to increase my fastcgi_read_timeout to 300 seconds for the entire server.

I was wondering if it would be possible to change the fastcgi_read_timeout directive for only files in my /batch/ folder and not the entire server. For example something like this...

location ~ \.php$ {
    fastcgi_pass   localhost:9000;  
    fastcgi_read_timeout 5;

    location /batch/ {
                fastcgi_read_timeout 300;
        }

    include /usr/local/nginx/conf/fastcgi_params;
}

So basically all PHP files on the server would have a 5 second timeout except PHP files in my /batch/ folder. Is this possible?

share|improve this question
    
If you are running the script via cron, why is Nginx involved? If possible, call the PHP interpreter directly from cron and bypass nginx completely. (However, fastcgi_read_timeout is a valid directive for location context (as well as server and http contexts) and you can have multiple location blocks, each bearing that directive) – cyberx86 Apr 10 '12 at 2:18
    
I still like to manually run the script from a browser sometimes. – Hart Jones Apr 10 '12 at 2:23
    
In that case, have a second php script that forks a background process which runs your long running process directly in the php interpreter. Done right, the parent execution time is minimal and the child process execution time is unrelated to nginx. (What you are trying to do should work though, but by preventing nginx from being involved with the long running process itself, you avoid the need of hard-coding the time limit in nginx and have much more flexibility). – cyberx86 Apr 10 '12 at 2:29

Different things here:

  1. prefix locations cannot be nested in regular expression one since the former have precedence over the latter during checks. Read the location directive docs to understand how the block serving the request is chosen. You can (and you wish) doing the opposite
  2. fastcgi_pass activates FastCGI reverse-proxying for the current block, but is not inherited (what a mess it would be!)

You could use such a configuration snippet:

location /batch/ {
    location ~* \.php$ {
        include /usr/local/nginx/conf/fastcgi_params;
        fastcgi_read_timeout 300;
        fastcgi_pass localhost:9000;
    }
}

location ~* \.php$ {
    include /usr/local/nginx/conf/fastcgi_params;
    fastcgi_read_timeout 5;
    fastcgi_pass localhost:9000;
}

On a side note, be careful, though (quoting the docs): [fastcgi_param directives] are inherited from the previous level if and only if there are no fastcgi_param directives defined on the current level.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.