Answer a question

I am currently redirecting my root domain to a subfolder. However I want to exclude a query string url which is used for an ajax call (www.example.com/?act=12). I am however unsure of how to do this in nginx.

This is my current nginx config file

server {
        listen          80;
        server_name     example.com;
        return 301      http://www.example.com$request_uri;

}

server {
        listen 80;
        server_name www.example.com;
        root /var/www/example.com/public;

        index index.php index.html;

        location = / {
                return 301 http://www.example.com/it/;
        }

        location / {
                proxy_pass http://localhost:8080;
                include /etc/nginx/proxy_params;
        }

        location ~* \.(js|css|jpg|jpeg|gif|png|svg|ico|pdf|html|htm)$ {
                expires 30d;
        }

        location ~ /\.ht {
        deny  all;
    }
}

Any help will be appreciated.

Answers

That can be achieved by using the evil if block.

The query string arguments are presented as variables with a $arg_ prefix. If you just need to test that act is set, try this:

location = / {
    if ($arg_act) {
        rewrite ^ /my/ajax/call last;
    }
    return 301 http://www.example.com/it/;
}

If you need to test for a specific value of $arg_act use the = operator.

See this document for details.

Logo

开发云社区提供前沿行业资讯和优质的学习知识,同时提供优质稳定、价格优惠的云主机、数据库、网络、云储存等云服务产品

更多推荐