nginx实现简单负载均衡

配置文件

  • 主文件: /etc/nginx/nginx.config
例当前在22.22.22.4服务器
//同时开另外三个服务器

可以在http{} 里添加 include /etc/nginx/sites-enabled/*;
然后在 /etc/nginx/sites-enabled/default 中配置

upstream project {
    server 22.22.22.2:3000;
    server 22.22.22.3:3000;
    server 22.22.22.5:3000;
}
server {
    listen 80;
    location / {
        proxy_pass http://project;
    }
    #缓存
    location ~* .(css|js|gif|jpe?g|png)$ {
        expire 168h;
    }
    location /api {
        expires 10m;
     }
}


//测试
ab -c 40 -n -1000 http://22.22.22.4/
  • 启用: nginx -s start / service nginx start
  • 重启: nginx -s reload / service nginx reload
  • centos7下的操作
  • 开启服务: systemctl start nginx.service
  • 查看状态: systemctl status nginx
  • 查看错误日志: cat /var/log/nginx/error.log

使用iptables设置转发端口

iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080
原文地址:https://www.cnblogs.com/jinkspeng/p/8531440.html