🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
需求,目前的服务器做了nginx的负载均衡,实现目标 请求:http://www.ckl.com/json/rooms/2010101/info.json rewrite:http://test.ckl.tv/room/info/uid/2010101 请求上面的地址,rewrite到一个新的地址并且发往另一组upstream 1.目前配置如下: ``` cat ckl.conf upstream qm_test { server 127.0.0.1:81; keepalive 100; } server { server_name test.ckl.com; access_log /app/local/log/nginx/ckl_access.log main; proxy_headers_hash_max_size 51200; proxy_headers_hash_bucket_size 6400; location / { proxy_pass http://qm_test/; proxy_http_version 1.1; proxy_set_header Connection ""; proxy_connect_timeout 10; proxy_send_timeout 10; proxy_set_header Host $host; proxy_set_header X-Real-IP $http_x_forwarded_for; proxy_set_header X-Forwarded-Server $host; proxy_set_header X-Forwarded-For $http_x_forwarded_for; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } ``` 2.新的配置如下 ``` upstream ckl_backend { server 10.10.1.23:80; keepalive 100; } upstream qm_test { server 127.0.0.1:81; keepalive 100; } server { server_name test.ckl.com; access_log /app/local/log/nginx/ckl_access.log main; proxy_headers_hash_max_size 51200; proxy_headers_hash_bucket_size 6400; #set $proxy_url qm_test; rewrite ^/json/rooms/([^\/]+)/info.json$ /room/info/uid/$1 last; location ~ ^/room/info/uid/([^\/]+)$ { proxy_pass http://ckl_backend; proxy_http_version 1.1; proxy_set_header Connection ""; proxy_connect_timeout 10; proxy_send_timeout 10; proxy_set_header Host $host; proxy_set_header X-Real-IP $http_x_forwarded_for; proxy_set_header X-Forwarded-Server $host; proxy_set_header X-Forwarded-For $http_x_forwarded_for; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location / { proxy_pass http://qm_test/; proxy_http_version 1.1; proxy_set_header Connection ""; proxy_connect_timeout 10; proxy_send_timeout 10; proxy_set_header Host $host; proxy_set_header X-Real-IP $http_x_forwarded_for; proxy_set_header X-Forwarded-Server $host; proxy_set_header X-Forwarded-For $http_x_forwarded_for; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } ``` 3.测试访问: ``` 192.168.1.23 - - [03/Jun/2016:14:43:15 +0800] "GET /room/info/uid/2010101 HTTP/1.1" 200 8693 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.75 Safari/537.36" - ``` 发现请求的是/json/rooms/ 跳转到/room/info/uid/ 请求的upstream http://ckl_backend;