根据URL路径访问不同的服务

URL例子

http://www.zjwblog.cn/${module}/xxx/xx

配置 upstream

upstream user {
  server 192.168.100.101 weight=30;
  server 192.168.100.102 weight=50;
  server 192.168.100.103 weight=20;
}

upstream order {
  server 192.168.100.111 weight=30;
  server 192.168.100.112 weight=30;
  server 192.168.100.113 weight=40;
}

使用 / 实现

server {
  listen 80;
  server_name www.zjwblog.cn;
  access_log "pipe:rollback /var/log/nginx/access.log interval=1d baknum=10 maxsize=1G" main;

  location ^~/user/ {
    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_set_header X-NginX-Proxy true;

    proxy_pass http://user/;
  }

  location ^~/order/ {
    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_set_header X-NginX-Proxy true;

    proxy_pass http://order/;
  }
}

使用rewrite实现

server {
  listen 80;
  server_name www.zjwblog.cn;
  access_log "pipe:rollback /var/log/nginx/access.log interval=1d baknum=10 maxsize=1G" main;

  location ^~/user/ {
    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_set_header X-NginX-Proxy true;

    rewrite ^/user/(.*)$ /$1 break;

    proxy_pass http://user;
  }

  location ^~/order/ {
    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_set_header X-NginX-Proxy true;

    rewrite ^/order/(.*)$ /$1 break;

    proxy_pass http://order;
  }
}

你可能感兴趣的:(Nginx)