Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

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'm using nginx's upstream directive to do some load balancing. But i want to handle the POST requests and the requests for /upload directory by my main server.

how can i tell nginx "dont proxy pass if the request method is POST or the url contains /admin or /upload"?

my current upstreams looks like this:

upstream appcluster {

        upstream admincluster {
                server unix:/tmp/php5-fpm.sock;


and my sites-enabled/default

location ~ \.php(.*)$ {
                try_files $uri =404;

                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

                #fastcgi_pass unix:/tmp/php5-fpm.sock;
                fastcgi_index index.php;

                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_param APPLICATION_ENV production;

                include fastcgi_params;
                fastcgi_pass appcluster;

share|improve this question

You should be able to add something like this in front of the other location block:

location /upload {

location /admin {

if ($request_method = POST ) {


nginx first searches for the most specific location given by literal strings regardless of the listed order. In the configuration above the only literal location is “/” and since it matches any request it will be used as a last resort. Then nginx checks locations given by regular expression in the order listed in the configuration file. The first matching expression stops the search and nginx will use this location. If no regular expression matches a request, then nginx uses the most specific literal location found earlier.

So, the /upload and /admin location blocks are literals and are processed after the regex you're using for the proxy.

Try "location ~ ^/admin" and so on.

share|improve this answer
id does not work. i think i have to wrap the fastcgi_pass part in if block. but i dont know how to do it for locations. i can do it for request method. – Cnkt Jan 19 '12 at 13:26
Updated with more info. – cjc Jan 19 '12 at 13:57

Your Answer


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.