nginx通过自定义http header 进行服务转发

##  场景

由于小程序上线的需要,后台服务需要多版本并存。这里我们通过使用自定义的HTTP Header头来实现。

nginx接收到的头部为:

miniversion: 1.0

接收到此请求将会跳转到新的url中。

核心:客户端自定义的http header,在nginx的配置文件里能直接读取到。

条件:header必须用减号“-”分隔单词,nginx里面会转换为对应的下划线“_”连接的小写单词。

这里面不建议使用“_”,会被nginx忽略掉。

所以我们为了省事,使用的是小写字母全拼。可以使用"-" 会被转化成“_”。多一事不如少一事,所以还是使用小写字母。

## 修改nginx配置

主要的配置文件如下:

       server
        {
        listen       80;
        server_name  camp.h5.cc camp.cc camp.boss.cc;
        index index.jsp index.htm index.html index.do login.vm;
        charset utf-8;
        underscores_in_headers on;
        location ~ / {
                if ($http_miniversion = "1.0") {
                        proxy_pass http://Tall;

}


                        proxy_set_header  Host $host;
                        proxy_set_header  X-Real-IP  $remote_addr;
                        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                        proxy_pass http://127.0.0.1:8080;
                }
        access_log  /data/logs/cc_com_access wwwlog;
        error_log   /data/logs/cc_com_error ;
        }

这里的自定义header前,需要加上http_下才能识别整个变量。

这里的Tall是我们的新版本,127.0.0.1,是我配置的一个配合测试的站点。

此站点的nginx配置文件如下:

cat mytest.com 
server {
    listen 8080;
    server_name localhost;

    root /var/www/html;
    index wx.html;

    location / {
        try_files $uri $uri/ =404;
    }
}

underscores_in_headers on:

nginx是支持读取非nginx标准的用户自定义header的,但是需要在http或者server下开启header的下划线支持:
比如我们自定义header为wx_unionid,获取该header时需要这样:$http_wx_unionid(一律采用小写,而且前面多了个http_)

这里必须强调的一点是我们必须配置:

proxy_set_header  Host $host;
proxy_set_header  X-Real-IP  $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

否则会导致自定义header头无效。

## 测试

echo "<h1>微信小程序测试平台</h1>" > /var/www/html/wx.html

这里如果我们直接使用浏览器,进行访问,不传递.http header;结果如下:

 这里我们使用postman进行参数传递的测试:

可以看到,访问到的是服务器的正常页面。

## 附录

nginx的主配置文件,仅供参考。

user www www;
worker_processes 8; 
error_log  /usr/local/nginx/logs/nginx_error.log  crit; 
pid        /usr/local/nginx/nginx.pid; 

#google_perftools_profiles /var/tmp/tcmalloc;

worker_rlimit_nofile 65535; 

events 
{  
         use epoll;  
         worker_connections 65535; 
}        

http 
{   
        include                                 mime.types;   
        default_type                            application/octet-stream;     
        charset                                 utf-8; 
    server_tokens                off;
        server_names_hash_bucket_size           128;
        client_header_buffer_size               4k; 
        large_client_header_buffers             4 4k;
        #client_max_body_size                    200m; 
        client_max_body_size                    1024m; 
        sendfile                                on;
        tcp_nopush                              on;
        tcp_nodelay                             on;
        keepalive_timeout                       60;
        open_file_cache                         max=204800 inactive=20s;
        open_file_cache_min_uses                1; 
        open_file_cache_valid                   30s;
        #proxy_connect_timeout                   300; 
        #proxy_send_timeout                      300; 
        #proxy_read_timeout                      300; 
    proxy_connect_timeout                   600;    
    proxy_send_timeout                      600;
    proxy_read_timeout                      600;    

        proxy_buffer_size                       16k; 
        proxy_buffers                           4 64k; 
        proxy_busy_buffers_size                 128k; 
        proxy_temp_file_write_size              128k; 
        gzip                                    on; 
        gzip_min_length                         1k; 
        gzip_buffers                            4 16k; 
        gzip_http_version                       1.1; 
        gzip_comp_level                         2; 
        gzip_types                              text/plain application/x-javascript text/css application/xml; 
        gzip_vary                               on; 
        # fastcgi_connect_timeout                 300;
        # fastcgi_send_timeout                    300;
        # fastcgi_read_timeout                    300;
    fastcgi_connect_timeout                 600;
    fastcgi_send_timeout                    600;
    fastcgi_read_timeout                    600;    

        fastcgi_buffer_size                     4k;
        fastcgi_buffers 8                       4k;
        fastcgi_busy_buffers_size               8k;
        fastcgi_temp_file_write_size            8k;
        fastcgi_cache_valid                     200 302 1h;
        fastcgi_cache_valid                     301 1d;
        fastcgi_cache_valid                     any 1m;
        fastcgi_cache_min_uses                  1;
        fastcgi_cache_use_stale                 error timeout invalid_header http_500;
        fastcgi_param                           SERVER_NAME     $host;
        server_name_in_redirect                 off;
        #add_header                              "X-UA-Compatible" "IE=EmulateIE8";
        upstream Tall {    
        server   192.168.150.13:9010;    
                }
        log_format  wwwlog  '$remote_addr - $remote_user [$time_local] "$request" '
        log_format  wwwlog  '$remote_addr - $remote_user [$time_local] "$request" '
        '$status $body_bytes_sent "$http_referer" '
        '"$http_user_agent" $http_x_forwarded_for';
include   vhosts/Tall/*;
include   vhosts/Ts/*;
include   vhosts/Tip/*;
}

参考链接:

http://www.ttlsa.com/nginx/nginx-proxy_set_header/

https://www.cnblogs.com/xiao987334176/p/11263649.html

猜你喜欢

转载自www.cnblogs.com/skymyyang/p/13392848.html