Hot For Coding

Nginx反向代理

Nginx反向代理非常强大,先来看看一个图

                  +--- host --------> node.js on localhost:8080
                  |
users --> nginx --|--- host/blog ---> node.js on localhost:8181
                  |
                  +--- host/mail ---> node.js on localhost:8282

用户访问Nginx,再由Nginx转发给后端node.js服务,而后端的node.js服务可以无限水平扩展来解决后端流量过大的问题

在Nginx.conf加入以下语句即可配置代理

server {
  listen       ...;
  ...
  location / {
    proxy_pass http://127.0.0.1:8080;
  }
  ...
}

如果按照上面那张图来配置的话,应该是这样

server {
  listen       ...;
  ...
  location / {
    proxy_pass http://127.0.0.1:8080;
  }
  
  location /blog {
    proxy_pass http://127.0.0.1:8181;
  }

  location /mail {
    proxy_pass http://127.0.0.1:8282;
  }
  ...
}

多个server,每个server有自己的代理

server {
  listen       80;
  server_name  example1.com;

  location / {
    proxy_pass http://127.0.0.1:8080;
  }
}

server {
  listen       80;
  server_name  example2.com;

  location / {
    proxy_pass http://127.0.0.1:8181;
  }
}

server {
  listen       80;
  server_name  example3.com;

  location / {
    proxy_pass http://127.0.0.1:8282;
  }
}

分享

TITLE: Nginx反向代理

LINK: https://www.qttc.net/34-nginx-proxy.html

NOTE: 原创内容,转载请注明出自琼台博客