nginx根据header参数值跳转

Nginx可以通过使用"if"语句和"$http_<header_field>"变量,根据请求中的header字段的值来实现根据header参数值的跳转。

下面是一个示例:

server {
    listen 80;
    server_name example.com;
    if ($http_user_agent ~* "mobile") {
        return 302 http://m.example.com;
    location / {
        root /var/www/example.com;
        index index.html;

在这个示例中,如果请求的User-Agent header字段中包含"mobile"字符串,则会返回302重定向到"m.example.com"。否则,请求将被路由到"/var/www/exa…

请注意,在这个例子中,我们使用了正则表达式匹配,但您也可以使用简单的字符串匹配。

  •