linux-keepalived-03 Nginx+Apache部署

linux-keepalived-03 Nginx+Apache部署

说明

  本篇做一个小实验,主要是为后面keepalived+Nginx+Apache部署做准备,所以把这篇文章归类为keepalived一类里面。

实验要求

  1. 完成nginx代理服务器
  2. 在nginx配置中使用proxy_set_header X-Real-IP $remote_addr;并且去httpd服务器定义日志格式
    LogFormat “ \”%{X-Real-IP}i\” %h %l %u %t \”%r\” %>s %b \”%{Referer}i\” \”%{User-Agent}i\”” combined
  3. 最后观察日志前后是否有客户端访问的真实IP地址

实验环境

node1(nginx):192.168.141.53
node3(web1):192.168.141.12
node4(web2):192.168.141.132

实验准备

node1

1
2
3
4
5
6
7
8
9
10
11
12
[root@node1 ~]# systemctl stop firewalld
[root@node1 ~]# vim /etc/selinux/config
[root@node1 ~]# cat /etc/selinux/config |grep SELINUX
# SELINUX= can take one of these three values:
SELINUX=disabled
# SELINUXTYPE= can take one of three two values:
SELINUXTYPE=targeted
[root@node1 ~]#
[root@node1 ~]# setenforce 0
[root@node1 ~]# getenforce
Permissive
[root@node1 ~]#

noed3、node4

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@node3 ~]# firewall-cmd --add-port=80/tcp --permanent
success
[root@node3 ~]# firewall-cmd --reload
success
[root@node3 ~]#
[root@node3 ~]# vim /etc/selinux/config
[root@node3 ~]# cat /etc/selinux/config |grep SELINUX
# SELINUX= can take one of these three values:
SELINUX=disabled
# SELINUXTYPE= can take one of three two values:
SELINUXTYPE=targeted
[root@node3 ~]#
[root@node3 ~]# setenforce 0
[root@node3 ~]# getenforce
Permissive
[root@node3 ~]#

实验步骤

nginx部署

node1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[root@node1 ~]# yum install epel-release -y
[root@node1 ~]# yum install nginx -y
[root@node1 ~]# cd /etc/nginx/conf.d/
[root@node1 conf.d]# vim proxy.conf
[root@node1 conf.d]# cat proxy.conf
upstream websers{
server 192.168.141.12;
server 192.168.141.132;
}
server{
listen 8080;
server_name 192.168.141.53;
location / {
proxy_pass http://websers;
}
}
[root@node1 conf.d]#
[root@node1 conf.d]# systemctl restart nginx
[root@node1 conf.d]# systemctl enable nginx
[root@node1 conf.d]# nginx -s reload
[root@node1 conf.d]#

apache部署

node3

1
2
3
4
[root@node3 ~]# yum install httpd -y
[root@node3 ~]# echo "i am web1" > /var/www/html/index.html
[root@node3 ~]# systemctl restart httpd
[root@node3 ~]# systemctl enable httpd

node4

1
2
3
4
[root@node4 ~]# yum install httpd -y
[root@node3 ~]# echo "i am web2" > /var/www/html/index.html
[root@node4 ~]# systemctl restart httpd
[root@node4 ~]# systemctl enable httpd

检查node3、node4的apache服务是否正常

1Vi1Gq.png

用cmd检查node1的nginx服务是否正常

1Viqeg.png

查看apache服务日志

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@node3 ~]# cat /var/log/httpd/access_log
192.168.141.1 - - [09/Apr/2019:07:25:13 -0400] "GET / HTTP/1.1" 200 10 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:66.0) Gecko/20100101 Firefox/66.0"
192.168.141.1 - - [09/Apr/2019:07:25:13 -0400] "GET /favicon.ico HTTP/1.1" 404 209 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:66.0) Gecko/20100101 Firefox/66.0"
192.168.141.53 - - [09/Apr/2019:07:28:21 -0400] "GET / HTTP/1.0" 200 10 "-" "curl/7.55.1"
192.168.141.53 - - [09/Apr/2019:07:28:23 -0400] "GET / HTTP/1.0" 200 10 "-" "curl/7.55.1"
192.168.141.53 - - [09/Apr/2019:07:28:24 -0400] "GET / HTTP/1.0" 200 10 "-" "curl/7.55.1"
192.168.141.53 - - [09/Apr/2019:07:28:25 -0400] "GET / HTTP/1.0" 200 10 "-" "curl/7.55.1"
192.168.141.53 - - [09/Apr/2019:07:28:25 -0400] "GET / HTTP/1.0" 200 10 "-" "curl/7.55.1"
[root@node3 ~]#


[root@node4 ~]# cat /var/log/httpd/access_log
192.168.141.1 - - [09/Apr/2019:19:25:26 +0800] "GET / HTTP/1.1" 200 10 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:66.0) Gecko/20100101 Firefox/66.0"
192.168.141.1 - - [09/Apr/2019:19:25:26 +0800] "GET /favicon.ico HTTP/1.1" 404 209 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:66.0) Gecko/20100101 Firefox/66.0"
192.168.141.53 - - [09/Apr/2019:19:28:22 +0800] "GET / HTTP/1.0" 200 10 "-" "curl/7.55.1"
192.168.141.53 - - [09/Apr/2019:19:28:23 +0800] "GET / HTTP/1.0" 200 10 "-" "curl/7.55.1"
192.168.141.53 - - [09/Apr/2019:19:28:24 +0800] "GET / HTTP/1.0" 200 10 "-" "curl/7.55.1"
192.168.141.53 - - [09/Apr/2019:19:28:25 +0800] "GET / HTTP/1.0" 200 10 "-" "curl/7.55.1"
[root@node4 ~]#

  可以看到,node3与node4日志记录里,访问自己的是node1:192.168.141.53。那么问题来了,如果某用户通过node1的代理在node3上登录了我们的网站,他刷新网页的时候,万一刷新的网页数据被node1负载均衡转发给了node4,然而node4上面是没有其对应session的,这样的话,该用户就不得不再次输入账号和密码,是很荒唐的一件事情。但是其实呢,nginx用的七层代理可以避免这种情况的发生,所以不需要进行手动干预,这些在我前几篇博客中有详细的介绍,这里只是提一下。

为了方便我们运维人员来观察是谁访问了我们的apache服务器,我们需要修改一下日志格式。

首先看一下官方文档:

http://httpd.apache.org/docs/2.2/mod/mod_log_config.html

1VFCOU.png

查看当前apache服务日志格式

1
2
3
4
5
[root@node3 ~]# cat /etc/httpd/conf/httpd.conf | grep LogFormat
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
[root@node3 ~]#

  可以看到,若要在apache日志文件中看到客户端ip,需要加一个 \”%{X-Real-IP}i\” ,其中X-Real-IP是我们在nginx定义的变量,气质为客户端的真实ip地址。

这也就是我们的第二个实验要求:

  在nginx配置中使用proxy_set_header X-Real-IP $remote_addr;并且去httpd服务器定义日志格式
LogFormat “ \”%{X-Real-IP}i\” %h %l %u %t \”%r\” %>s %b \”%{Referer}i\” \”%{User-Agent}i\”” combined

node1上:

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@node1 ~]# cd /etc/nginx/conf.d/
[root@node1 conf.d]# vim proxy.conf

在location / {
proxy_pass http://websers;
}
里面加上:
proxy_set_header X-Real-IP $remote_addr;

保存退出
[root@node1 ~]# systemctl restart nginx
[root@node1 ~]# nginx -s reload
[root@node1 ~]#

1Vk0v6.png

node3上

1
2
3
4
5
6
7
[root@node3 ~]# vim /etc/httpd/conf/httpd.conf

将196行修改为:
LogFormat " \"%{X-Real-IP}i\" %h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined

保存退出
[root@node3 ~]# systemctl restart httpd

1Vkg5d.png

node4上

1
2
3
4
5
6
7
[root@node4 ~]# vim /etc/httpd/conf/httpd.conf

将196行修改为:
LogFormat " \"%{X-Real-IP}i\" %h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined

保存退出
[root@node4 ~]# systemctl restart httpd

同上。

1VkhxP.png

现在再次用cmd来访问192.168.141.53:8080

1VkLPs.png

然后回到node3上查看日志:

1
[root@node3 ~]# cat /var/log/httpd/access_log

1VkX2q.png

然后回到node4上查看日志:

1
[root@node4 ~]# cat /var/log/httpd/access_log

1VkzrT.png

  可以看到相比于之前的日志,日志条目最前面多了一个ip,这个ip显然就是我的电脑的ip,此外还有我电脑的操作系统信息等,这样我们修改apache日志格式的目的就已经达到了。

欢迎打赏,谢谢
------ 本文结束------
0%