centos7 nginx安装与配置实现负载均衡

此文章已不再更新,查看最新版文章和更多内容:

右键在新标签页中打开链接即可。

《centos7 nginx 安装与配置实现负载均衡》

 

--------------------------------------------------- 分割线 -----------------------------------------------------

nginx是一个web服务器,类似apache一样,但是比apache性能更好,更快。还可以实现反向代理,和负载均衡,常用于构建web服务集群的负载均衡。今天就来记录一下,nginx的安装与配置,实现负载均衡的作用。


1. 首先,安装

# 配置阿里云的epel-release源
$ rpm -ivh https://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm

# 直接yum安装nginx
$ yum install nginx


# 安装就这么简单方便,安装完成后,就可以使用systemctl来控制nginx的启动了
# 加入开机启动
$ systemctl enable nginx

# 启动nginx
$ systemctl start nginx

# 查看状态
$ systemctl status nginx



2. 修改配置文件
   设置nginx的配置文件,实现负载均衡。顾名思义就是将多个请求分发到不同的服务上,实现均衡的负载,减小单个服务的压力。

-------------------------------------------------------------------------------

# 修改主配置文件
$ vim /etc/nginx/nginx.conf
----------------------------------------------------------------------------------
# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/


user nginx;
# 工作进程数,默认为自动,可以自己设置,一般不大于cpu核数
worker_processes auto;
# 错误日志路径
error_log /var/log/nginx/error.log;
# pid文件路径
pid /run/nginx.pid;


# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;


events {
    # 时间模型,在linux2.6以上,常用epoll模型,是Linux 2.6以上版本内核中的高性能网络I/O模型
    use epoll;
    # 设置网路连接序列化,防止惊群现象发生,默认为on
    accept_mutex on;
    # 设置一个进程是否同时接受多个网络连接,默认为off
    multi_accept on;
    # 一个进程的最大连接数
    worker_connections 1024;
}


http {
    # 日志格式,格式名为 main
    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;
    tcp_nodelay         on;
    # 连接超时时间
    keepalive_timeout   65;
    types_hash_max_size 2048;

    # 开启压缩
    gzip on;
    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;


    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    # 包含,引入此目录下的配置文件
    include /etc/nginx/conf.d/*.conf;


    # 这里设置负载均衡,负载均衡有多中策略,nginx自带的有轮询,权重,ip-hash,响应时间等粗略。
    # 默认为平分http负载,为轮询的方式。
    # 权重则是按照权重来分发请求,权重高的负载大
    # ip-hash,根据ip来分配,保持同一个ip分在同一台服务器上。
    # 响应时间,根据服务器都nginx 的响应时间,优先分发给响应速度快的服务器,策略可以适当组合

    # tomcat为自定义的负载均衡规则名,可自定义多组upstream
    upstream tomcat {
        # ip_hash则为ip-hash方法
        ip_hash;
        # weight为权重
        server 192.168.14.132:8080 weight=5;
        server 192.168.14.133:8080 weight=3;
    }




    server {
        # 监听端口
        listen       80 default_server;
        # 监听的域名
        server_name  _;
        root         /usr/share/nginx/html;


        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

    
        # 表示根路径所有请求,可以自定义来针对不同的域名设定不同负载规则 和服务
        location / {
          # 反向代理,填上你自己的负载均衡规则名
          proxy_pass    http://tomcat;
          proxy_redirect off; 
          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_connect_timeout 90;
          proxy_send_timeout 90;
          proxy_read_timeout 90;
        }
   
   # location ~\.(gif|jpg|png)$ { (比如,以正则表达式写)  
   #  root /home/root/images;
   #  }


        error_page 404 /404.html;
            location = /40x.html {
        }


        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }


# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2 default_server;
#        listen       [::]:443 ssl http2 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers HIGH:!aNULL:!MD5;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / {
#        }
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }


}
----------------------------------------------------------------------------------


# 更新配置后,可以重载配置生效,不需要重启服务
$ systemctl reload nginx

 

OK,到此完成了nginx的安装与负载均衡。等待,有什么疑问或问题,欢迎大家留言指正。

 

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值