时间:2023-04-18 21:08:01 | 来源:网站运营
时间:2023-04-18 21:08:01 来源:网站运营
十年经验程序员,手把手教你使用Nginx搭建web集群【建议新手收藏】:#!/bin/bashnginx_pkg='nginx-1.5.1.tar.gz'nginx_prefix=/usr/local/nginxhtml=/var/nginxlog=/var/log/nginxcheck13 () { [ $UID -ne 0 ] && echo "need to be root to that" && exit 1 [ ! -f $nginx_pkg ] && echo "not found source packager" && exit 1 [ ! -d $html ] && mkdir -p $html [ ! -d $log ] && mkdir -p $log}nginx_install () { source_pkg=`echo $nginx_pkg|awk -F ".tar" '{print $1}'` [ -d /usr/src/$source_pkg ]&&rm -rf /usr/src/$source_pkg tar xf $nginx_pkg -C /usr/src cp nginxd /usr/src/$source_pkg if [ $? -eq 0 ];then cd /usr/src/$source_pkg if [ $? -eq 0 ];then yum -y install gcc-* pcre pcre-devel zlib zlib-devel openssl-* &> /dev/null [ $? -ne 0 ]&&"YUM set error" && exit 1 ./configure --prefix=$nginx_prefix if [ $? -eq 0 ];then make if [ $? -eq 0 ];then make install if [ $? -eq 0 ];then ln -s -f $nginx_prefix/conf/nginx.conf /etc/ ln -s -f $nginx_prefix/logs/ $log/logs ln -s -f $nginx_prefix/html $html/html ln -s -f $nginx_prefix/sbin/ /usr/sbin/ cp nginxd /etc/init.d/nginx;chmod 755 /etc/init.d/nginx else exit 1 fi else exit 1 fi else exit 1 fi else exit 1 fi else exit 1fi [ $? -eq 0 ]&&clear||exit echo -e "/n/033[32m Nginx Install Success: /033[0m" echo -e "/n" echo -e "/tNginx_conf: /etc/nginx.conf" echo -e "/tNginx_html: $html/html" echo -e "/tNginx_access_log: $log/logs/access.log" echo -e "/tNginx_error_log: $log/logs/error.log/n/n/n/n" read -n1 -p "press any key and exit...." echo }check13nginx_install
(2)配置web服务器操作[root@web02 ~]# sh nginx_install # 脚本安装nginx[root@web02 ~]# echo web02 > /usr/local/nginx/html/index.html # 写入页面[root@web02 ~]# yum -y install elinks &>/dev/null # 安装文本浏览器[root@web02 ~]# /usr/local/nginx/sbin/nginx # 启动nginx[root@web02 ~]# elinks http://localhost -dump web02
3、配置分发器(轮询方式分发)# 清除空行和注释项$ sed -i '/#/d' nginx.conf$ sed -i '/^$/d' nginx.conf# 配置nginx.confworker_processes 1;events { worker_connections 1024;}http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; upstream web{ # 名为web的反向代理群组 server 192.168.31.42; server 192.168.31.43; } server { listen 80; server_name localhost; location / { proxy_pass http://web; # 去找反向代理 } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }}
4、集群分发测试(默认轮询)[root@web02 ~]# elinks http://192.168.31.40 -dump web01[root@web02 ~]# elinks http://192.168.31.40 -dump web02[root@web02 ~]# elinks http://192.168.31.40 -dump web01[root@web02 ~]# elinks http://192.168.31.40 -dump web02
为了答谢大家关注和支持,这次给大家准备了限时领取福利:阿里面试题、百度面试题、滴滴面试题、华为面试题、京东面试题、美团面试题、腾讯面试题、头条面试题、中兴面试题。upstream web { server 192.168.31.42; server 192.168.31.43;}server { listen 80; server_name localhost; location / { proxy_pass http://web; } }
前面已经测试验证了轮询算法分发。配置backup参数如下所示:upstream web { server 192.168.31.42 weight=1; server 192.168.31.43 weight=1 backup;}server { listen 80; server_name localhost; location / { proxy_pass http://web; } }
关停第一个节点情况,访问尝试:[root@web02 ~]# elinks http://192.168.31.40 -dump web02[root@web02 ~]# elinks http://192.168.31.40 -dump web02[root@web02 ~]# elinks http://192.168.31.40 -dump web02
(2)基于权重的分发upstream web { # 设置权重比例1:2 server 192.168.31.42 weight=1; server 192.168.31.43 weight=2;}server { listen 80; server_name localhost; location / { proxy_pass http://web; } }
访问测试验证:[root@web02 ~]# elinks http://192.168.31.40 -dump web01[root@web02 ~]# elinks http://192.168.31.40 -dump web02[root@web02 ~]# elinks http://192.168.31.40 -dump web02[root@web02 ~]# elinks http://192.168.31.40 -dump web01[root@web02 ~]# elinks http://192.168.31.40 -dump web02[root@web02 ~]# elinks http://192.168.31.40 -dump web02
通过权重比例的分配,可以让性能更强的服务器承担处理更多的请求。upstream web { ip_hash; # 指定ip_hash即可,默认weight权重比例1: 1 server 192.168.31.42; server 192.168.31.43; }server { listen 80; server_name localhost; location / { proxy_pass http://web; } }
访问测试验证:# 源ip固定[root@web01 ~]# elinks http://192.168.31.40 -dump web02[root@web01 ~]# elinks http://192.168.31.40 -dump web02[root@web01 ~]# elinks http://192.168.31.40 -dump web02MacBook-Pro:~ hqs$ elinks http://192.168.31.40 -dump web01MacBook-Pro:~ hqs$ elinks http://192.168.31.40 -dump web01MacBook-Pro:~ hqs$ elinks http://192.168.31.40 -dump web01
Request URL: http://192.168.31.43/ # 请求的URLRequest Method: GET # 请求的方法Status Code: 200 OKRemote Address: 192.168.31.43:80Referrer Policy: no-referrer-when-downgrade # 请求的策略Response headers # 响应头Accept-Ranges: bytesConnection: keep-aliveContent-Length: 6Content-Type: text/htmlDate: Fri, 26 Oct 2018 12:43:02 GMTETag: "5bd3014d-6"Last-Modified: Fri, 26 Oct 2018 11:58:05 GMTServer: nginx/1.15.5Request Headers # 请求头GET /index.php HTTP/1.1 # 请求方法是GET;域名后面的部分就是路径,默认是‘/’;使用的HTTP协议是1.1 Host: 192.168.31.43 # 访问的域名(域名或IP均可)Connection: keep-alive # 长连接Upgrade-Insecure-Requests: 1 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36 # 用户浏览器的类型Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8 # 可以接受的数据类型Accept-Encoding: gzip, deflate # 压缩Accept-Language: zh-CN,zh;q=0.9 # 语言
1、基于host分发http { upstream web1 { # 名为web1的反向代理群组 server 192.168.31.42; server 192.168.31.52; } upstream web2 { # 名为web2的反向代理群组 server 192.168.31.43; server 192.168.31.53; } server { # web1虚拟主机 listen 80; server_name www.web1.com; # 基于域名分发必须有域名 location / { proxy_pass http://web1; } } server { # web2虚拟主机 listen 80; server_name www.web2.com; # 基于域名分发必须有域名 location / { proxy_pass http://web2; } }}
基于域名的分发测试:[root@web02 ~]# elinks http://www.web1.com -dump web01[root@web02 ~]# elinks http://www.web1.com -dump web01[root@web02 ~]# elinks http://www.web1.com -dump web01[root@web02 ~]# elinks http://www.web2.com -dump web02[root@web02 ~]# elinks http://www.web2.com -dump web02[root@web02 ~]# elinks http://www.web2.com -dump web02
2、基于开发语言分发# 192.168.31.40分发器上nginx配置http { upstream php { server 192.168.31.42; } upstream html { server 192.168.31.43; } server { location ~* /.php$ { # 以php结尾的 proxy_pass http://php; } location ~* /.html$ { # 以html结尾的 proxy_pass http://html; } }}
测试验证:# 安装php环境$ yum install httpd php # 安装apache和php# 启动apache,自带php$ systemctl start httpd# 编写php文件$ echo "<?php phpinfo(); ?>" > /var/www/html/index.php# 访问192.168.31.40/index.php 可以看到php-info信息页面# 访问192.168.31.40/index.html 可以看到web02
3、基于浏览器的分发$ vim /usr/local/nginx/conf/nginx.confhttp { server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } } server { listen 81; server_name localhost; location / { root web3; index index.html index.htm } }}$ mkdir /usr/local/nginx/web3$ echo web03 > /usr/local/nginx/web3/index.html$ /user/local/nginx/sbin/nginx
(2)基于浏览器分发的分发器配置upstream elinks { server 192.168.31.42; }upstream chrome { server 192.168.31.43; }upstream any { server 192.168.31.42:81; }server { listen 80; server_name www.web1.com; location / { proxy_pass http://any; if ( $http_user_agent ~* Elinks ) { proxy_pass http://elinks; } if ( $http_user_agent ~* chrome ) { proxy_pass http://chrome; } }}
(3)访问测试upstream bj.server { server 192.168.31.42; # web01}upstream sh.server { server 192.168.31.43; # web02}upstream default.server { server 192.168.31.42:81; # web03}geo $geo { # IP库 default default; 192.168.31.241/32 bj; # 杭州 192.168.31.242/32 sh; # 杭州}server { listen 80; server_name www.web1.com; location / { proxy_pass http://$geo.server$request_uri; }}
关键词:收藏,新手,建议,使用,程序,经验,把手