2019独角兽企业重金招聘Python工程师标准>>>
###背景
想在本机做nginx的负载相关工作
###准备工作
1. 已安装好Parelles Desktop:
装一个Debian系统,系统用户名 : parallels,作为一个站点服务。
装一个Ubuntu系统,系统用户名 : parallels,作为一个站点服务。
2. 在本机安装Nginx, 一个作为负载均衡服务
###配置
####虚拟机是采用 共享网络(Shared Networking) 的模式
#####方便操作虚拟机中的系统,配置ssh登录方式
#####Debian虚拟机
-
在虚拟机执行命令,生成ssh相关信息 安装ssh服务: sudo apt-get install openssh-client openssh-server 在命令行输入: ssh-keygen,一路enter生成ssh相关信息
-
查看虚拟机IP 在虚拟机执行 : sudo ifconfig
eth1 ................次要信息省略.............................. inet addr:10.211.55.8 Bcast:10.211.55.255 Mask:255.255.255.0 ................次要信息省略..............................
-
将本机~/.ssh/id_rsa.pub拷贝到虚拟机的~/.ssh/authorized_keys文件中
-
输入命令登录到虚拟机 ssh parallels@10.211.55.8
#####Ubuntu虚拟机
-
操作方式通Debian虚拟一样 在虚拟机执行 : sudo ifconfig
eth1 ................次要信息省略.............................. inet addr:10.211.55.9 Bcast:10.211.55.255 Mask:255.255.255.0 ................次要信息省略..............................
####配置站点,以备负载均衡使用
-
配置本机站点(web1) cd /path/to/nginx/conf.d
vim load_balance.conf 复制如下内容到文件中 : server { listen 8090; server_name load.demo.com; location / { root /path/to/project; index index.html index.htm; } } 在文件中写入web1: echo "web1" > /path/to/project/index.html 绑定hosts: echo "127.0.0.1 load.demo.com" >> /etc/hosts 在浏览器输入load.demo.com:8090 : 显示 : web1
-
配置虚拟机中站点(web2) 按照1的配置,设置站点配置;在index.html文件中写入web2
在浏览器输入load.demo.com:8090 : 显示 : web2
####Nginx负载均衡配置
-
打开本机的nginx.conf配置文件 vim /path/to/nginx/nginx.conf
-
修改nginx.conf对应配置 ................次要信息省略.............................. http { include 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 /usr/local/var/log/nginx/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; ####增加配置########################################## upstream load_balance_test { server 10.211.55.8:8090 weight=1; server 10.211.55.9:8090 weight=1; } ####增加配置########################################## ####更改配置########################################## server { listen 8090; server_name load.demo.com; location / { proxy_pass http://load_balance_test; } } ####更改配置########################################## #gzip on; include /usr/local/etc/nginx/conf.d/*.conf; }
-
绑定hosts echo "127.0.0.1 load.demo.com" >> /etc/hosts
-
启动nginx,在浏览器中输入load.demo.com:8090,不停刷新,交替出现web1和web2,成功
###配置过程中遇到问题以及解决方案
1. 链接到虚拟机,但是链接拒绝。ssh localhost提示:ssh: connect to host localhost port 22: Connection refused
解决方案:
http://stackoverflow.com/questions/6313929/how-do-i-open-port-22-in-os-x-10-6-7,其实就是开启允许mac远程链接,系统偏好设置->共享->勾选远程登陆
2. 将本机id_rsa.pub拷贝到虚拟机的系统对应位置/path/to/.ssh/authorized_keys,但是一直提示输入密码,但是输入密码却总是连不上
通过ssh -vvv user@remote-ip打印调试信息,信息如下:
................次要信息省略..............................
debug1: Trying private key: ~/.ssh/id_dsa
debug3: no such identity: ~/.ssh/id_dsa: No such file or directory
................次要信息省略..............................
解决方案:
在/etc/ssh/sshd_config找配置项 IdentityFile,如果没有找到就新增;如果找到了有注释#就去掉#的注释;执行IdentityFile配置的私钥key文件:IdentityFile ~/.ssh/id_rsa