标签:com htm sys round war style 日志 添加 反向
lb01服务器:172.16.1.5
web01服务器:172.16.1.7
web02服务器:172.16.1.8
[root@web02 ~] # cd /etc/nginx/conf.d/
[root@web02 conf.d] # vim www.conf
server {
listen 80;
server_name www.oldboy.com;
location / {
root /html/www;
index index.html index.htm;
}
}
?
[root@web01 conf.d] # cd /html/????????????????????创建站点目录
[root@web01 html] # mkdir -p www
[root@web01 html] # cd www
[root@web01 www] # echo "$(hostname -i) www.oldboy.com" > index.html
?
[root@web01 www] # systemctl restart nginx
[root@web01 www] # curl 10.0.0.7
172.16.1.7 www.oldboy.com????????????????????????已经成功
?
[root@web02 www] # curl 10.0.0.8
172.16.1.8 www.oldboy.com
[root@lb01 ~] # yum -y install nginx????????安装nginx
?
[root@lb01 ~] # nginx -V
nginx version: nginx/1.16.1????????????????版本信息
[root@lb01 ~] # cd /etc/nginx/conf.d/
[root@lb01 conf.d] # vim www.conf ????????????????编辑配置文件
upstream oldboy {????????????????????????????设置集群名称
server 172.16.1.7:80;????????????????????????指定具体的集群中的服务器主机信息
server 172.16.1.8:80;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://oldboy;????????????????????????使用http协议来进行集群中的服务器代理
}
}
[root@lb01 conf.d] # nginx -t????????????????????检查语法是
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb01 conf.d] # systemctl restart nginx????????????重启nginx服务
[root@lb01 conf.d] # curl 10.0.0.7
172.16.1.7 www.oldboy.com
[root@lb01 conf.d] # curl 10.0.0.8
172.16.1.8 www.oldboy.com
多台web服务器性能不同的时候使用
[root@lb01 conf.d] # vim www.conf
upstream oldboy {
server 172.16.1.7:80 weight=4;????????????????????2
server 172.16.1.8:80 weight=2;????????????????????1
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://oldboy;
}
}
?
[root@lb01 conf.d] # nginx -t????????????????检查语法是否正确
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb01 conf.d] # systemctl restart nginx????????重启nginx
[root@lb01 conf.d] # curl 10.0.0.5
172.16.1.8 www.oldboy.com????????????????????一次8
[root@lb01 conf.d] # curl 10.0.0.5
172.16.1.7 www.oldboy.com????????????????????2次7
[root@lb01 conf.d] # curl 10.0.0.5
172.16.1.7 www.oldboy.com????????????????????2次7
应用在某台服务器出现故障不能进行处理前端发过来的请求的时候
[root@lb01 conf.d] # vim www.conf
upstream oldboy {
server 172.16.1.7:80;
server 172.16.1.8:80 max_fails=3 fail_timeout=60s;????????设置最大失败次数为3次,超时时间为60s
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://oldboy;
}
}
[root@web02 www] # systemctl stop nginx
[root@lb01 conf.d] # curl 10.0.0.5????????????????????只会给7分配
172.16.1.7 www.oldboy.com
[root@lb01 conf.d] # curl 10.0.0.5
172.16.1.7 www.oldboy.com
[root@lb01 conf.d] # curl 10.0.0.5
172.16.1.7 www.oldboy.com
[root@lb01 conf.d] # curl 10.0.0.5
172.16.1.7 www.oldboy.com
[root@lb01 conf.d] # curl 10.0.0.5
172.16.1.7 www.oldboy.com
[root@lb01 conf.d] # curl 10.0.0.5
172.16.1.7 www.oldboy.com
[root@lb01 conf.d] # curl 10.0.0.5
^[[A172.16.1.7 www.oldboy.com
[root@lb01 conf.d] # curl 10.0.0.5
172.16.1.7 www.oldboy.com
[root@web02 www] # systemctl start nginx
[root@lb01 conf.d] # curl 10.0.0.5????????????????发现7,8都会分配
172.16.1.8 www.oldboy.com
[root@lb01 conf.d] # curl 10.0.0.5
172.16.1.7 www.oldboy.com
[root@lb01 conf.d] # curl 10.0.0.5
172.16.1.8 www.oldboy.com
[root@lb01 conf.d] # curl 10.0.0.5
172.16.1.7 www.oldboy.com
[root@lb01 conf.d] #
?
正常的服务器出现了问题,热备服务器就会工作,正常服务器好了,热备就不需要了
?
[root@lb01 conf.d] # vim www.conf
upstream oldboy {
server 172.16.1.7:80
server 172.16.1.8:80 backup;????????????????????????假如8服务器当做热备
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://oldboy;
}
}
?
[root@lb01 conf.d] # nginx -t????????????????检查语法是否正确
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb01 conf.d] # systemctl restart nginx????????重启nginx
[root@lb01 conf.d] # curl 10.0.0.5????????????????现在只有7在工作,只有7坏了,8才会工作
172.16.1.7 www.oldboy.com
[root@lb01 conf.d] # curl 10.0.0.5
172.16.1.7 www.oldboy.com
[root@lb01 conf.d] # curl 10.0.0.5
企业中出现某台服务器的负载压力一直很高的情况下
[root@lb01 conf.d] # vim www.conf
upstream oldboy {
least_conn;????????????????????按照连接数进行分配
server 172.16.1.7:80;
server 172.16.1.8:80;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://oldboy;
}
}
出现在web网站界面反复登录的情况
指定某个客户端访问某个web服务器
[root@lb01 conf.d] # vim www.conf
upstream oldboy {
ip_hash;????
server 172.16.1.7:80;
server 172.16.1.8:80;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://oldboy;
}
}
[root@web01 conf.d] # cat *.conf
server {
listen 80;
server_name bbs.oldboy.com;????????????????bbs网站的配置
location / {
root /html/bbs;
index index.html index.htm;
}
}
server {
listen 80;
server_name blog.oldboy.com;????????????????????blog网站的配置
location / {
root /html/blog;
index index.html index.htm;
}
}
server {
listen 80;
server_name www.oldboy.com;????????????????????????www网站的配置
location / {
root /html/www;
index index.html index.htm;
}
}
[root@web01 conf.d] #
?
[root@web01 conf.d] # cd /html/
[root@web01 html] # mkdir -p blog bbs????????????????创建bbs,blog站点目录
?
[root@web01 html] # cd /html/blog/
[root@web01 blog] # echo "$(hostname -i) blog.oldboy.com" > index.html????????????添加内容????
[root@web01 blog] # cd ..
[root@web01 html] # cd bbs
[root@web01 bbs] # echo "$(hostname -i) bbs.oldboy.com" > index.html????????????添加内容
[root@web01 bbs] # cd ../
[root@web01 html] # cd www
[root@web01 www] # echo "$(hostname -i) www.oldboy.com" > index.html????????添加内容
[root@web01 www] #
?
[root@web01 nginx] # systemctl restart nginx????????????????????????????????重启nginx
[root@web01 nginx] # nginx -t????????????????????????????????????检查nginx语法
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 nginx] #
[root@lb01 conf.d] # vim www.conf
upstream oldboy {
server 172.16.1.7:80;
server 172.16.1.8:80 max_fails=3 fail_timeout=60s;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://oldboy;
proxy_set_header Host $host;????????????????设置不同域名可以通过负载均衡访问不同网站的内容
}
}
?
????
[root@lb01 conf.d] # systemctl restart nginx????????重启nginx服务
[root@lb01 conf.d] # nginx -t????????????????检查nginx语法的正确性
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb01 conf.d] #
172.16.1.5 - - [02/Dec/2019:13:39:43 +0800] "GET / HTTP/1.0" 200 26 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36" "-"
出现此类问题是因为负载均衡服务器没有将客户端的访问地址告诉web服务器造成的
[root@lb01 conf.d] # vim www.conf
upstream oldboy {
server 172.16.1.7:80;
server 172.16.1.8:80 max_fails=3 fail_timeout=60s;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://oldboy;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;????????????????加入远程需要添加的代理信息
}
}
172.16.1.5 - - [02/Dec/2019:13:52:01 +0800] "POST /wp-admin/admin-ajax.php HTTP/1.0" 404 555 "http://blog.oldboy.com/wp-admin/post.php?post=5&action=edit" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36" "10.0.0.1"????成功
[root@lb01 conf.d] # vim www.conf
upstream oldboy {
server 172.16.1.7:80;
server 172.16.1.8:80 max_fails=3 fail_timeout=60s;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://oldboy;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_next_upstream error timeout invalid_header http_403 http_502;????????设置上出现错误代码的指令信息
}
}
[root@web01 ~] # cd /html/www/
[root@web01 www] # ll
total 4
-rw-r--r-- 1 root root 26 Dec 2 11:12 index.html
[root@web01 www] # echo "static.oldboy.com" > index.html????????????添加测试静态数据
[root@web02 ~] # cd /html/www/
You have new mail in /var/spool/mail/root
[root@web02 www] # ll
total 4
-rw-r--r-- 1 root root 26 Dec 2 11:08 index.html
[root@web02 www] # echo "dynamic oldboy.com" > index.html????????添加测试动态数据
[root@lb01 conf.d] # vim www.conf
upstream static_oldboy {????????????????????创建静态集群
server 172.16.1.7:80;
}
upstream dynamic_oldboy {????????????????????创建动态集群
server 172.16.1.8:80;
}
server {
listen 80;
server_name localhost;
location ~* /static_oldboy {????????????????????代理静态网站
proxy_pass http://static_oldboy;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
location ~* /dynamic_oldboy {
proxy_pass http://dynamic_oldboy;????????????????代理动态网站
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
????
[root@lb01 conf.d] # nginx -t????????????????????检查语法信息
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb01 conf.d] # systemctl restart nginx????????????重启nginx服务
[root@lb01 conf.d] #
[root@web01 ~] # cd /html/www/
[root@web01 www] # echo "iphone.oldboy.com" > index.html????????????添加测试是手机端过来的数据信息
You have new mail in /var/spool/mail/root
[root@web01 www] #
[root@web02 ~] # cd /html/www/
[root@web02 www] # echo "pc.oldboy.com" > index.html????????????添加测试是电脑发送过来的数据
[root@web02 www] #
[root@lb01 conf.d] # vim www.conf
upstream iphone {
server 172.16.1.7:80;
}
upstream pc {
server 172.16.1.8:80;
}
server {
listen 80;
server_name localhost;
location / {
if ($http_user_agent ~* iphone){????????????????????看终端是否匹配iphone
proxy_pass http://iphone;
}
proxy_pass http://pc;????????????????????????????不匹配iphone剩下的匹配电脑
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
172.16.1.5 - - [02/Dec/2019:15:24:22 +0800] "GET / HTTP/1.0" 200 18 "-" "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1" "10.0.0.1"
标签:com htm sys round war style 日志 添加 反向
原文地址:https://www.cnblogs.com/liangyuxing/p/11971296.html