nginx redirect to varios directories

user1908375 asked:

I am looking for a way redirect the URLrequest to various directories.

For example the user calls

I want it randomly be redirected to, or or etc.

I know there is a way to use

upstream myapp1 {
        server weight=3;

for redirecting requests to various servers. But could I do the same with directories?

should it be this?:

location ~ \.php$ {
        try_files $variant$uri $variant$uri/ $variant/index.php;
        root           /usr/share/nginx/html;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
        include        fastcgi_params;


My answer:

You could do this more easily with nginx’s split clients module.

For instance, if you want a roughly three way split:

http {
    split_clients "${remote_addr}AAA" $variant {
        33%     "/a";
        33%     "/b";
        *       "/c";

Then in your server block, replace:

    try_files $uri $uri/ /index.php;


    try_files $variant$uri $variant$uri/ $variant/index.php;

View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.