二、架构02-Nginx的反向代理、虚拟主机

一、Nginx反向代理

1、环境:
node01:192.168.32.132 Nginx (前端Nginx反向代理服务器)
node02:192.168.32.128 Httpd01 (后端服务器)
node03:192.168.32.131 Httpd02 (后端服务器)
2、先配置后端

[root@node02 ~]# yum install -y httpd mod_ssl
[root@node02 ~]# vim /var/www/html/index.html
<h1>Upstream Server 1</h1>
[root@node02 ~]# systemctl start httpd
[root@node02 ~]# ss -tnl
State       Recv-Q Send-Q                                Local Address:Port                                               Peer Address:Port
LISTEN      0      128                                               *:111                                                           *:*
LISTEN      0      128                                               *:22                                                            *:*
LISTEN      0      100                                       127.0.0.1:25                                                            *:*
LISTEN      0      128                                              :::111                                                          :::*
LISTEN      0      128                                              :::80                                                           :::*
LISTEN      0      128                                              :::22                                                           :::*
LISTEN      0      100                                             ::1:25                                                           :::*
LISTEN      0      128                                              :::443                                                          :::*

3、配置前端

[root@node01 ~]# yum install -y nginx
[root@node01 ~]# cd /etc/nginx/
[root@node01 nginx]#
[root@node01 nginx]#
[root@node01 nginx]# vim conf.d/ilinux.conf
server{
        listen 80;
        server_name www.ilinux.io;
        location / {
                proxy_pass http://192.168.32.128:80;
        }
}
[root@node01 ~]# systemctl start nginx
[root@node01 ~]# ss -tnl
State      Recv-Q Send-Q                                                                        Local Address:Port                                                                                       Peer Address:Port
LISTEN     0      128                                                                                       *:111                                                                                                   *:*
LISTEN     0      128                                                                                       *:80                                                                                                    *:*
LISTEN     0      128                                                                                       *:22                                                                                                    *:*
LISTEN     0      100                                                                               127.0.0.1:25                                                                                                    *:*
LISTEN     0      128                                                                                      :::111                                                                                                  :::*
LISTEN     0      128                                                                                      :::80                                                                                                   :::*
LISTEN     0      128                                                                                      :::22                                                                                                   :::*
LISTEN     0      100                                                                                     ::1:25                                                                                                   :::*

4、验证


image.png

5、查看后端服务器日志,会发现访问的IP是代理服务器的ip

[root@node02 ~]# tcpdump -i ens33 tcp port 80
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on ens33, link-type EN10MB (Ethernet), capture size 262144 bytes
14:44:40.504215 IP 192.168.32.132.52940 > node02.http: Flags [S], seq 709562863, win 29200, options [mss 1460,sackOK,TS val 899375 ecr 0,nop,wscale 7], length 0
14:44:40.504240 IP node02.http > 192.168.32.132.52940: Flags [S.], seq 3682950667, ack 709562864, win 28960, options [mss 1460,sackOK,TS val 900103 ecr 899375,nop,wscale 7], length 0
14:44:40.504428 IP 192.168.32.132.52940 > node02.http: Flags [.], ack 1, win 229, options [nop,nop,TS val 899375 ecr 900103], length 0
14:44:40.504480 IP 192.168.32.132.52940 > node02.http: Flags [P.], seq 1:98, ack 1, win 229, options [nop,nop,TS val 899375 ecr 900103], length 97: HTTP: GET / HTTP/1.0
14:44:40.504493 IP node02.http > 192.168.32.132.52940: Flags [.], ack 98, win 227, options [nop,nop,TS val 900103 ecr 899375], length 0
14:44:40.505321 IP node02.http > 192.168.32.132.52940: Flags [P.], seq 1:308, ack 98, win 227, options [nop,nop,TS val 900104 ecr 899375], length 307: HTTP: HTTP/1.1 200 OK
14:44:40.505420 IP node02.http > 192.168.32.132.52940: Flags [F.], seq 308, ack 98, win 227, options [nop,nop,TS val 900104 ecr 899375], length 0
14:44:40.505514 IP 192.168.32.132.52940 > node02.http: Flags [.], ack 308, win 237, options [nop,nop,TS val 899377 ecr 900104], length 0
14:44:40.505652 IP 192.168.32.132.52940 > node02.http: Flags [F.], seq 98, ack 309, win 237, options [nop,nop,TS val 899377 ecr 900104], length 0
14:44:40.505661 IP node02.http > 192.168.32.132.52940: Flags [.], ack 99, win 227, options [nop,nop,TS val 900104 ecr 899377], length 0
^C
10 packets captured
10 packets received by filter
0 packets dropped by kernel

二、实现动静分离

1、再配置一个后端

[root@node03 ~]# yum install -y httpd mod_ssl
[root@node03 ~]# vim /var/www/html/index.html
<h1>Upstream Server 2</h1>
[root@node03 html]# more /var/www/html/
index.html    test001.jpg   test002.jfif  test003.jpeg  test004.jpg   test005.jpg   timg.jfif
[root@node03 html]# more /var/www/html/index.html
<h1>Upstream Server 2</h1>
[root@node03 html]# systemctl start httpd

2、配置前端

[root@node01 ~]# vim /etc/nginx/conf.d/ilinux.conf
server{
        listen 80;
        server_name www.ilinux.io;
        location / {
                proxy_pass http://192.168.32.128:80;
        }

        location ~* \.(jpg|jpeg|png)$ {
                proxy_pass http://192.168.32.131:80;
        }
}
[root@node01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@node01 ~]# nginx -s reload

3、验证


image.png

image.png

三、让后端得到真实服务器的ip和返回客户端的ip

1、配置前端

[root@node01 ~]# vim /etc/nginx/conf.d/ilinux.conf
server{
        listen 80;
        server_name www.ilinux.io;


#       location / {
#               root /data/nginx/html;
#       }


#       location /admin/ {
        location / {
                #proxy_pass http://192.168.32.128:80;
                #proxy_pass http://192.168.32.128:80;
                proxy_pass http://192.168.32.128:80/;
                proxy_set_header X-Real-IP $remote_addr;
                add_header X-Via $server_addr;
        }
        location ~* \.(jpg|jpeg|png)$ {
                proxy_pass http://192.168.32.131:80;
        }
}
[root@node01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@node01 ~]# nginx -s reload

2、验证

[root@node02 admin]# vim /etc/httpd/conf/httpd.conf
<IfModule log_config_module>
    #
    # The following directives define some format nicknames for use with
    # a CustomLog directive (see below).
    #
    LogFormat "%{X-Real-IP}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
    LogFormat "%h %l %u %t \"%r\" %>s %b" common
[root@node02 admin]# httpd -t
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 192.168.32.128. Set the 'ServerName' directive globally to suppress this message
Syntax OK
[root@node02 admin]# systemctl restart httpd
[root@node02 admin]# tail -f /var/log/httpd/access_log
192.168.32.132 - - [12/Feb/2019:14:43:35 +0800] "GET / HTTP/1.0" 200 27 "-" "curl/7.29.0"
192.168.32.132 - - [12/Feb/2019:14:44:40 +0800] "GET / HTTP/1.0" 200 27 "-" "curl/7.29.0"
192.168.32.132 - - [12/Feb/2019:15:03:20 +0800] "GET / HTTP/1.0" 200 27 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36"
192.168.32.132 - - [12/Feb/2019:15:03:20 +0800] "GET /favicon.ico HTTP/1.0" 404 209 "http://www.ilinux.io/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36"
192.168.32.132 - - [12/Feb/2019:15:04:34 +0800] "GET /test1.html HTTP/1.0" 404 208 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36"
192.168.32.132 - - [12/Feb/2019:15:09:57 +0800] "GET /admin/ HTTP/1.0" 404 204 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36"
192.168.32.132 - - [12/Feb/2019:15:12:35 +0800] "GET /admin/ HTTP/1.0" 200 21 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36"
192.168.32.132 - - [12/Feb/2019:15:14:28 +0800] "GET / HTTP/1.0" 200 27 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36"
192.168.32.1 - - [12/Feb/2019:15:36:27 +0800] "GET /admin/ HTTP/1.0" 200 21 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36"
192.168.32.1 - - [12/Feb/2019:15:36:29 +0800] "GET / HTTP/1.0" 304 - "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36"
192.168.32.1 - - [12/Feb/2019:15:37:45 +0800] "GET / HTTP/1.0" 304 - "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36"
^C
备注:因为vmware虚拟机用的NAT模式,所以我在本机的浏览器访问的话走的时候NAT的ip段的网关。即192.168.32.1这个地址去访问Nginx的,并不是本机的ip地址!
image.png

四、配置缓存

1、配置前端

[root@node01 ~]# vim /etc/nginx/nginx.conf
http {
    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  /var/log/nginx/access.log  main;


    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;


    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
    proxy_cache_path /data/nginx/cache levels=1:1:1 keys_zone=pcache:10m max_size=2g ;
[root@node01 ~]# vim /etc/nginx/conf.d/ilinux.conf
server{
        listen 80;
        server_name www.ilinux.io;
        proxy_cache pcache;
        proxy_cache_key $request_uri;
        proxy_cache_methods GET HEAD;
        proxy_cache_min_uses 2;
        proxy_cache_valid 200 302 10m;
        proxy_cache_valid 404      1m;
        proxy_cache_use_stale http_502;
        location / {
                #proxy_pass http://192.168.32.128:80;
                #proxy_pass http://192.168.32.128:80;
                proxy_pass http://192.168.32.128:80/;
                proxy_set_header X-Real-IP $remote_addr;
                add_header X-Via $server_addr;
        }
        location ~* \.(jpg|jpeg|png)$ {
                proxy_pass http://192.168.32.131:80;
        }
}

2、验证
强刷一下www.ilinux.io

image.png

image.png

五、配置Nginx+PHP动静结合(前端Nginx做静态web服务器,后端用php做动态web服务器)

1、架构图


image.png

2、配置后端

[root@node02 ~]# yum install -y php-fpm php-mysql php-mbstring php-mcrypt mariadb-server
[root@node02 opt]# vim  /etc/php-fpm.d/www.conf
listen = 0.0.0.0:9000
;listen.allowed_clients = 127.0.0.1
pm.max_children = 150
pm.status_path = /status
ping.path = /ping
[root@node02 opt]# mkdir /var/lib/php/session
[root@node02 opt]# chown apache:apache /var/lib/php/session/
[root@node02 opt]# systemctl start php-fpm.service
[root@node02 opt]# ss -tnl
State      Recv-Q Send-Q                                                                        Local Address:Port
LISTEN     0      128                                                                                       *:9000
LISTEN     0      128                                                                                       *:111
LISTEN     0      128                                                                                       *:22
LISTEN     0      100                                                                               127.0.0.1:25
LISTEN     0      128                                                                                      :::111
LISTEN     0      128                                                                                      :::80
LISTEN     0      128                                                                                      :::22
LISTEN     0      100                                                                                     ::1:25
LISTEN     0      128                                                                                      :::443

[root@node02 opt]# mkdir /data/apps -pv
mkdir: 已创建目录 "/data"
mkdir: 已创建目录 "/data/apps"
[root@node02 opt]# vim /data/apps/index.php
<?php
        phpinfo();
?>
[root@node02 opt]# vim /etc/my.cnf.d/
client.cnf         mysql-clients.cnf  server.cnf
[root@node02 opt]# vim /etc/my.cnf.d/server.cnf
[mysqld]
skip_name_resolve=ON
innodb_file_per_table=ON
[root@node02 opt]# systemctl start mariadb
[root@node02 opt]# ss -tnl
State      Recv-Q Send-Q                                                                        Local Address:Port
LISTEN     0      128                                                                                       *:9000
LISTEN     0      50                                                                                        *:3306
LISTEN     0      128                                                                                       *:111
LISTEN     0      128                                                                                       *:22
LISTEN     0      100                                                                               127.0.0.1:25
LISTEN     0      128                                                                                      :::111
LISTEN     0      128                                                                                      :::80
LISTEN     0      128                                                                                      :::22
LISTEN     0      100                                                                                     ::1:25
LISTEN     0      128                                                                                      :::443
[root@node02 opt]# mysql_secure_installation
[root@node02 opt]# unzip phpMyAdmin-4.0.10.20-all-languages.zip
[root@node02 pma]# cp config.sample.inc.php config.inc.php
[root@node02 pma]# vim config.inc.php

3、配置前端

[root@node01 ~]# vim /etc/nginx/conf.d/ilinux.conf
[root@node01 ~]# cd //etc/nginx/conf.d/
[root@node01 conf.d]# ls
ilinux.conf
[root@node01 conf.d]# mv ilinux.conf iunix.conf
[root@node01 conf.d]# vim iunix.conf
server{
        listen 80;
        server_name www.iunix.io;
        proxy_cache pcache;
        proxy_cache_key $request_uri;
        proxy_cache_methods GET HEAD;
        proxy_cache_min_uses 2;
        proxy_cache_valid 200 302 10m;
        proxy_cache_valid 404      1m;
        proxy_cache_use_stale http_502;
        location / {
                proxy_pass http://192.168.32.128:80/;
                proxy_set_header X-Real-IP $remote_addr;
                add_header X-Via $server_addr;
        }
        location ~* \.(jpg|jpeg|png)$ {
                proxy_pass http://192.168.32.131:80;
        }
}
[root@node01 conf.d]# vim ilinux.conf
server {
        listen 80;
        server_name www.ilinux.io;
        index index.php index.html;
        location ~* \.php$ {
                fastcgi_pass 192.168.32.128:9000;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME     /data/apps/$fastcgi_script_name;
                include fastcgi_params;
                fastcgi_keep_conn    on;
                fastcgi_cache fcache;
                fastcgi_cache_key $request_uri;
                fastcgi_cache_valid 200 302  10m;
                fastcgi_cache_valid 301       1h;
                fastcgi_cache_valid any       1m;
        }
}
[root@node01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@node01 ~]# nginx -s reload

4、验证


image.png

image.png

六、实现后端两台服务器动静分离

1、架构图


image.png

2、配置node03为后端静态web服务器

[root@node03 ~]# yum install -y nginx
[root@node03 ~]# mkdir -pv /data/nginx/html
[root@node03 ~]# mv phpMyAdmin-4.0.10.20-all-languages /data/nginx/html/
[root@node03 ~]# cd /data/nginx/html/
[root@node03 html]#
[root@node03 html]#
[root@node03 html]# ls
phpMyAdmin-4.0.10.20-all-languages
[root@node03 html]# ln -sv phpMyAdmin-4.0.10.20-all-languages pma
"pma" -> "phpMyAdmin-4.0.10.20-all-languages"
[root@node03 html]# ls
phpMyAdmin-4.0.10.20-all-languages  pma
[root@node03 html]# vim /etc/nginx/nginx.conf
    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /data/nginx/html;
[root@node03 html]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@node03 html]# systemctl start nginx
[root@node03 html]# ss -tnl
State      Recv-Q Send-Q                                                                        Local Address:Port
LISTEN     0      128                                                                                       *:111
LISTEN     0      128                                                                                       *:80
LISTEN     0      128                                                                                       *:22
LISTEN     0      100                                                                               127.0.0.1:25
LISTEN     0      128                                                                                      :::111
LISTEN     0      128                                                                                      :::80
LISTEN     0      128                                                                                      :::22
LISTEN     0      100                                                                                     ::1:25

3、配置前端Nginx包括缓存

[root@node01 ~]# vim /etc/nginx/conf.d/ilinux.conf
server {
        listen 80;
        server_name www.ilinux.io;
        index index.php index.html;
        location / {
                root /data/nginx/html;
                proxy_pass http://192.168.32.131:80;
        }
        location ~* \.php$ {
                fastcgi_pass 192.168.32.128:9000;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME     /data/apps/$fastcgi_script_name;
                include fastcgi_params;
                fastcgi_keep_conn    on;
                fastcgi_cache fcache;
                fastcgi_cache_key $request_uri;
                fastcgi_cache_valid 200 302  10m;
                fastcgi_cache_valid 301       1h;
                fastcgi_cache_valid any       1m;
        }
        location ~* ^/(status|ping)$ {


                                        include     fastcgi_params;
                                        fastcgi_pass  192.168.32.128:9000;
                                        fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;

        }
}
[root@node01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@node01 ~]# nginx -s reload

4、验证
01


image.png

02
没加缓存之前,使用ab进行并发测试


image.png

加缓存之后,使用ab进行并发测试
image.png

03php的ping状态的返回值和php的状态
image.png

image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,332评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,508评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,812评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,607评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,728评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,919评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,071评论 3 410
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,802评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,256评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,576评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,712评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,389评论 4 332
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,032评论 3 316
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,798评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,026评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,473评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,606评论 2 350

推荐阅读更多精彩内容