比如在微信或者QQ内打开,就开启重定向

    location /i {

       if ($http_user_agent ~* "(MicroMessenger|MQQBrowser)") {

           return 301 https://zzzmvasijbpw.bja.sealos.run$uri;

        }

        proxy_pass http://127.0.0.1:8232;

        proxy_set_header Host www.klgeek.com;

        proxy_set_header X-Real-IP $remote_addr;

        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        proxy_set_header REMOTE-HOST $remote_addr;

        add_header X-Cache $upstream_cache_status;


        proxy_connect_timeout 30s;

        proxy_read_timeout 86400s;

        proxy_send_timeout 30s;

        proxy_http_version 1.1;

        proxy_set_header Upgrade $http_upgrade;

        proxy_set_header Connection "upgrade";

    }

配置中常用的语句

if判断语句 :在location中使用if语句可以实现条件判断,其通常有一个return语句,且一般与有着last或break标记的rewrite规则一同使用

判断条件: 正则表达式匹配: ~:与指定正则表达式模式匹配时返回“真”,判断匹配与否时区分字符大小写; ~*:与指定正则表达式模式匹配时返回“真”,判断匹配与否时不区分字符大小写; !~:与指定正则表达式模式不匹配时返回“真”,判断匹配与否时区分字符大小写; !~*:与指定正则表达式模式不匹配时返回“真”,判断匹配与否时不区分字符大小写; 文件及目录匹配判断: -f, !-f:判断指定的路径是否为存在且为文件; -d, !-d:判断指定的路径是否为存在且为目录; -e, !-e:判断指定的路径是否存在,文件或目录均可; -x, !-x:判断指定路径的文件是否存在且可执行; 实例: location / { if ($request_method == “PUT”) { proxy_pass http://test.wyx.com:8080; } if ($request_uri ~ "\.(jpg|gif|jpeg|png)$") { proxy_pass http://image; break; } } upstream imageservers { server 192.168.0.172:80 weight 2; server 192.168.0.171:80 weight 3; }

nginx中常用的配置变量

$args, 请求中的参数 $content_length, HTTP请求信息里的”Content-Length”; $content_type, 请求信息里的”Content-Type”; $document_root, 针对当前请求的根路径设置值; $document_uri, 与$uri相同; $host, 请求信息中的”Host”,如果请求中没有Host行,则等于设置的服务器名; $limit_rate, 对连接速率的限制; $request_method, 请求的方法,比如”GET”、”POST”等; $remote_addr, 客户端地址; $remote_port, 客户端端口号; $remote_user, 客户端用户名,认证用; $request_filename, 当前请求的文件路径名 $request_body_file, ?? $request_uri, 请求的URI,带参数; $query_string, 与$args相同; $scheme, 所用的协议,比如http或者是https,比如rewrite  ^(.+)$  $scheme://example.com$1  redirect; $server_protocol, 请求的协议版本,”HTTP/1.0″或”HTTP/1.1″; $server_addr, 服务器地址,如果没有用listen指明服务器地址,使用这个变量将发起一次系统调用以取得地址(造成资源浪费); $server_name, 请求到达的服务器名; $server_port, 请求到达的服务器端口号; $uri, 请求的URI,可能和最初的值有不同,比如经过重定向之类的。