学习Express之nginx配置负载均衡

AvaMAS 9年前

来自: http://my.oschina.net/songzhu/blog/610376


关于 express-demo

关于 部署 express-demo

虽然 PM2 已有负载均衡的功能,但是他只对多进程之间进行处理,如果是多个服务器之间,就无法办到了,这里我们选用nginx

    服务器准备

    s0:CentOS,172.16.70.175,nginx 服务器

    s1:CentOS,172.16.70.174,web 服务器

    s2:CentOS,172.16.70.173,web 服务器

1、配置 s1,s2

    由于之前已有说明,这不再细说。参考:http://my.oschina.net/songzhu/blog/610337

    修改 s1 服务器的 config.js 内容为:

var config = {      server:"172.16.70.174"  };    module.exports = config;

   重启 web

pm2 restart pm2-start.json

     修改 s2 服务器的 config.js 内容为:

var config = {      server:"172.16.70.173"  };    module.exports = config;

    重启 web

pm2 restart pm2-start.json

2、配置 s0

    安装 nginx

$ rpm -ivh http://nginx.org/packages/centos/7/x86_64/RPMS/nginx-1.8.0-1.el7.ngx.x86_64.rpm

    启动 nginx

$ service nginx start

    验证

$ curl http://localhost/

    配置负载均衡,编辑 /etc/nginx/nginx.conf,在 http 节点里面增加以下内容:

    upstream web-server {          server 172.16.70.173:3000;          server 172.16.70.174:3000;      }            server {   listen 80;   server_name my.server.com;   location / {       proxy_pass http://web-server;       proxy_set_header Host $host;       proxy_set_header X-Real-IP $remote_addr;       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;   }      }

    完整的 /etc/nginx/nginx.conf 内容如下:

user  nginx;  worker_processes  1;    error_log  /var/log/nginx/error.log warn;  pid        /var/run/nginx.pid;      events {      worker_connections  1024;  }      http {      include       /etc/nginx/mime.types;      default_type  application/octet-stream;        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '                        '$status $body_bytes_sent "$http_referer" '                        '"$http_user_agent" "$http_x_forwarded_for"';        access_log  /var/log/nginx/access.log  main;        sendfile        on;      #tcp_nopush     on;        keepalive_timeout  65;        #gzip  on;        include /etc/nginx/conf.d/*.conf;            upstream web-server {          server 172.16.70.173:3000;          server 172.16.70.174:3000;      }            server {   listen 80;   server_name my.server.com;   location / {       proxy_pass http://web-server;       proxy_set_header Host $host;       proxy_set_header X-Real-IP $remote_addr;       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;   }      }  }

    编辑完成之后,重启 nginx

$ service nginx restart

    修改本地hosts文件,增加:172.16.70.175 my.server.com

    在本地执行:

$ curl http://my.server.com/  <!DOCTYPE html><html><head><title>Express</title><link rel="stylesheet" href="/stylesheets/style.css"></head><body><h1>Express</h1><p>Welcome to Express on 172.16.70.173</p></body></html>  $ curl http://my.server.com/  <!DOCTYPE html><html><head><title>Express</title><link rel="stylesheet" href="/stylesheets/style.css"></head><body><h1>Express</h1><p>Welcome to Express on 172.16.70.174</p></body></html>  $ curl http://my.server.com/  <!DOCTYPE html><html><head><title>Express</title><link rel="stylesheet" href="/stylesheets/style.css"></head><body><h1>Express</h1><p>Welcome to Express on 172.16.70.173</p></body></html>  $ curl http://my.server.com/  <!DOCTYPE html><html><head><title>Express</title><link rel="stylesheet" href="/stylesheets/style.css"></head><body><h1>Express</h1><p>Welcome to Express on 172.16.70.174</p></body></html>

    结果和预期一样,nginx 成功将请求平均分配到两台服务器上了。

3、遇到的问题

    502 bag getway.

    通过查看错误日志:

$ cat /var/log/nginx/error.log

    发现具体的错误是:connect() to 172.16.70.174:3000 failed (13: Permission denied) while connecting to upstream, client: 172.16.70.9, server: my.server.com, request: "GET / HTTP/1.1", upstream: "http://172.16.70.174:3000/", host: "my.server.com:80"

    解决办法是,执行以下命令

$ sudo setsebool -P httpd_can_network_connect 1  $ service nginx restart

    解决错误参考网址:http://blog.frag-gustav.de/2013/07/21/nginx-selinux-me-mad/


项目代码:https://git.oschina.net/cavintang/express-demo.git