nginx跟据url进行分流

需求让Nginx跟据请求的url不同,而转发到不同的server上
举例:
url http://127.0.0.1/json/0769/
url http://127.0.0.1/json/0734/
需求把url后缀为0769的请求转发到server1, url 后缀为0734的请求转发到server1
 

nginx.conf实现代码

 

 
  1. worker_processes 2;

  2.  
  3. #error_log /var/log/nginx/error.log;

  4. #pid /var/run/nginx.pid;

  5.  
  6. events {

  7. worker_connections 1024;

  8. use epoll;

  9. }

  10.  
  11. http {

  12. charset utf-8;

  13.  
  14. map $zone $up_stream {

  15. ^~0769 frontends_0769;

  16. ^~0734 frontends_0734;

  17. default frontends_0769;

  18. }

  19.  
  20. # Enumerate all the Tornado servers here

  21. upstream frontends_0769 {

  22. server 127.0.0.1:3333;

  23. #server 127.0.0.1:3334;

  24. #server 127.0.0.1:3335;

  25. #server 127.0.0.1:3336;

  26. }

  27.  
  28. upstream frontends_0734 {

  29. server 127.0.0.1:3334;

  30. #server 127.0.0.1:3334;

  31. #server 127.0.0.1:3335;

  32. #server 127.0.0.1:3336;

  33. }

  34.  
  35. include mime.types;

  36. default_type application/octet-stream;

  37.  
  38. #access_log /var/log/nginx/access.log;

  39.  
  40. keepalive_timeout 65;

  41. proxy_read_timeout 200;

  42. sendfile on;

  43. tcp_nopush on;

  44. tcp_nodelay on;

  45. gzip on;

  46. gzip_min_length 1000;

  47. gzip_proxied any;

  48. gzip_types text/plain text/css text/xml

  49. application/x-javascript application/xml

  50. application/atom+xml text/javascript;

  51.  
  52. # Only retry if there was a communication error, not a timeout

  53. # on the Tornado server (to avoid propagating "queries of death"

  54. # to all frontends)

  55. proxy_next_upstream error;

  56.  
  57. server {

  58. listen 8089;

  59.  
  60. location / {

  61. proxy_pass_header Server;

  62. proxy_set_header Host $http_host;

  63. proxy_redirect off;

  64. proxy_set_header X-Real-IP $remote_addr;

  65. proxy_set_header X-Scheme $scheme;

  66. #proxy_pass http://frontends;

  67. #if ( $request_uri ~* /json/(\d\d\d\d)\d+/ ) {

  68. # set $zone $1;

  69. # proxy_pass http://frontends_$zone;

  70. #}

  71. if ( $request_uri ~ ^/json/(\d+)/$ ) {

  72. set $zone $1;

  73. proxy_pass http://$up_stream;

  74. }

  75.  
  76. }

  77. }

  78. }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值