Skip to main content

Nginx rewrite by header value

Nginx 判断header中的value进行版本分离

当前有一个需求是为了平滑升级,需要老版本和新版本API共存,最简单的方法是从nginx上下手,建立两套代理机制。
大致来说就是使用nginx的map来进行头部数据的提取并转化为变量,然后在location中判断并使用rewrite。
nginx中if语句存在极大的不确定性,一定要仔细测试
if和后面括号以及变量等号这些元素都要有空格

map代码

    map $http_xiaocan $ver{
        default 3;
        2 2;
        3 3;
    }

location中判断

        location / {
            if ($ver == 2) {
                rewrite ^(.*)$ http://www.xiaocan.me
            }
            if ($ver == 3) {
                rewrite ^(.*)$ http://www.baidu.com
            }
        }

完整代码

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;  
worker_processes auto;  
error_log /var/log/nginx/error.log;  
pid /var/run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {  
    worker_connections  1024;
}

http {

    map $http_xiaocan $ver{
        default 3;
        2 2;
        3 3;
    }

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;
    server_names_hash_bucket_size 128;

    server {
        server_name test.rdev www.test.rdev;
        listen 192.74.244.97;
        root /home/test/public_html;
        index index.html index.htm index.php;
        access_log /var/log/virtualmin/test.rdev_access_log;
        error_log /var/log/virtualmin/test.rdev_error_log;
        fastcgi_param GATEWAY_INTERFACE CGI/1.1;
        fastcgi_param SERVER_SOFTWARE nginx;
        fastcgi_param QUERY_STRING $query_string;
        fastcgi_param REQUEST_METHOD $request_method;
        fastcgi_param CONTENT_TYPE $content_type;
        fastcgi_param CONTENT_LENGTH $content_length;
        fastcgi_param SCRIPT_FILENAME /home/test/public_html$fastcgi_script_name;
        fastcgi_param SCRIPT_NAME $fastcgi_script_name;
        fastcgi_param REQUEST_URI $request_uri;
        fastcgi_param DOCUMENT_URI $document_uri;
        fastcgi_param DOCUMENT_ROOT /home/test/public_html;
        fastcgi_param SERVER_PROTOCOL $server_protocol;
        fastcgi_param REMOTE_ADDR $remote_addr;
        fastcgi_param REMOTE_PORT $remote_port;
        fastcgi_param SERVER_ADDR $server_addr;
        fastcgi_param SERVER_PORT $server_port;
        fastcgi_param SERVER_NAME $server_name;
        fastcgi_param HTTPS $https;
        location / {
            if ($ver == 2) {
                rewrite ^(.*)$ http://www.xiaocan.me
            }
            if ($ver == 3) {
                rewrite ^(.*)$ http://www.baidu.com
            }
        }
        location ~ \.php$ {
            try_files $uri =404;
            fastcgi_pass unix:/var/php-nginx/14770418441345.sock/socket;
        }
        listen 192.74.244.97:443 ssl;
        ssl_certificate /home/test/ssl.cert;
        ssl_certificate_key /home/test/ssl.key;
    }
}