通过nginx实现负载均衡
程序员文章站
2024-01-31 23:14:40
...
实验主机为server2
一、在官网下载nginx-1.18.0.tar.gz到/root下然后安装软件
[aaa@qq.com ~]# tar xf nginx-1.18.0.tar.gz [aaa@qq.com ~]# cd nginx-1.18.0/ -------------------------------------------------------------------------------------- [aaa@qq.com nginx-1.18.0]#./configure --prefix=/usr/local/nginx --with-http_ssl_module checking for OS + Linux 4.18.0-80.el8.x86_64 x86_64 checking for C compiler ... not found ./configure: error: C compiler cc is not found ##显示需要安装gcc -------------------------------------------------------------------------------------- [aaa@qq.com nginx-1.18.0]# dnf install -y gcc [aaa@qq.com nginx-1.18.0]#./configure --prefix=/usr/local/nginx --with-http_ssl_module option, or install the PCRE library into the system, or build the PCRE library statically from the source with nginx by using --with-pcre=<path> option. ######显示需要安装pcre-devel -------------------------------------------------------------------------------------- [aaa@qq.com nginx-1.18.0]# dnf install -y pcre-devel [aaa@qq.com nginx-1.18.0]#./configure --prefix=/usr/local/nginx --with-http_ssl_module ./configure: error: SSL modules require the OpenSSL library. You can either do not enable the modules, or install the OpenSSL library into the system, or build the OpenSSL library statically from the source with nginx by using --with-openssl=<path> option. ##显示需要安装openssl-devel -------------------------------------------------------------------------------------- [aaa@qq.com nginx-1.18.0]# dnf install -y openssl-devel [aaa@qq.com nginx-1.18.0]#./configure --prefix=/usr/local/nginx --with-http_ssl_module Configuration summary + using system PCRE library + using system OpenSSL library + using system zlib library
二、自动生成/usr/local/nginx
[aaa@qq.com nginx-1.18.0]# make && make install [aaa@qq.com nginx-1.18.0]# ll -d /usr/local/nginx/ drwxr-xr-x 6 root root 54 Aug 9 15:47 /usr/local/nginx/
三、执行nginx服务,过滤80端口
[aaa@qq.com sbin]# cd /usr/local/nginx/sbin/ [aaa@qq.com sbin]# ./nginx -v nginx version: nginx/1.18.0 [aaa@qq.com sbin]# ./nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [aaa@qq.com sbin]# ./nginx ##80端口不要被其他占用,否则无效 [aaa@qq.com sbin]# netstat -antlp | grep 80 ##80端口不要被其他占用 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 22108/nginx: master [aaa@qq.com sbin]# ./nginx -s stop [aaa@qq.com sbin]# netstat -antlp | grep 80 ##过滤不到了 进程都关了
四、修改配置文件以及编辑服务nginx.service
[aaa@qq.com ~]# vi /root/nginx-1.18.0/auto/cc/gcc # debug ##:/debug #CFLAGS="$CFLAGS -g" ##注释掉这一行 ---------------------------------------------------------- [aaa@qq.com ~]# vi /root/nginx-1.18.0/src/core/nginx.h #define nginx_version 1018000 #define NGINX_VERSION "1.18.0" #define NGINX_VER "nginx" ##不显示版本号 --------------------------------------------------------- [aaa@qq.com ~]# vi /usr/lib/systemd/system/nginx.service [Unit] Description=The NGINX HTTP and reverse proxy server After=syslog.target network-online.target remote-fs.target nss-lookup.target Wants=network-online.target [Service] Type=forking PIDFile=/usr/local/nginx/nginx.pid ##注意各自nginx的路径 ExecStartPre=/usr/local/nginx/sbin/nginx -t ExecStart=/usr/local/nginx/sbin/nginx ExecReload=/usr/local/nginx/sbin/nginx -s reload ExecStop=/bin/kill -s QUIT $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target ------------------------------------------------------------ systemctl daemon-reload systemctl start nginx.service ------------------------------------------------------------ [aaa@qq.com ~]# vi /etc/security/limits.conf nginx - nofile 65535 ##倒数第二行添加nginx的 # End of file
五、添加nginx用户
[aaa@qq.com ~]# useradd -M -d /usr/local/nginx -s /sbin/nologin nginx
六、编辑nginx的配置文件
[aaa@qq.com ~]# vi /usr/local/nginx/conf/nginx.conf 2 #user nobody; 3 worker_processes auto; ##cpu的个数 auto自动获取 33 #gzip on; 34 upstream westos { 35 #ip_hash; ##hash算法先调用那个就一直调用 36 server 172.25.7.4; ##server3 37 server 172.25.7.5; ##server4 38 } 39 server { 40 listen 80; 41 server_name www.westos.org; 42 location / { 43 proxy_pass http://westos; 44 } 45 } 46 server { 47 listen 80; 48 server_name localhost;
七、重启nginx服务并且修改server1的地址解析
[aaa@qq.com ~]# sytemctl restart nginx.service [aaa@qq.com ~]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 172.25.7.250 Horizon_ley 172.25.7.1 server0 172.25.7.2 server1 172.25.7.3 server2 172.25.7.4 server3 172.25.7.5 server4 172.25.7.6 server5 172.25.7.7 server6 172.25.7.8 server7
[aaa@qq.com ~]# curl 172.25.7.3 this is vm3 [aaa@qq.com ~]# curl 172.25.7.3 this is vm4 [aaa@qq.com ~]# curl 172.25.7.3 this is vm3 [aaa@qq.com ~]# curl 172.25.7.3 this is vm4
通过nginx实现负载均衡success