美文网首页
二、架构02-Nginx的反向代理、虚拟主机

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

作者: 无法成为野兽 | 来源:发表于2019-02-15 11:56 被阅读0次

一、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

相关文章

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

    一、Nginx反向代理 1、环境:node01:192.168.32.132 Nginx (前端Nginx反向代理...

  • nginx

    nginx的配置、虚拟主机、负载均衡和反向代理一nginx的配置、虚拟主机、负载均衡和反向代理二nginx的配置、...

  • 架构图解

    架构图片理解 单个服务器 单机 动静分离动静分离的架构 反向代理反向代理的架构 负载均衡 缓存相关 业务拆分

  • httpd反向代理

    添加虚拟主机,配置反向代理主要修改/etc/httpd/conf.d下虚拟主机文件主要配置文件:

  • Web服务器根据url参数代理

    根据url参数代理到不同的虚拟主机中TIP:正向代理、反向代理以及url重写 正向代理:正向代理是一个位于客户端和...

  • nginx配置代理

    nginx 反向代理服务器,用来代理访问路径的跳转 虚拟主机的配置 访问路径: http://dev.admin....

  • 小白学nginx之nginx的代理和负载均衡

    正向代理和反向代理的区别 正向代理,主要是内部上网反向代理,主要用于公司集群架构中区别在于形式上服务的"对象"不一...

  • nginx 反向代理情况下 开启 GZIP 压缩

    网络架构 客户端 -- > 反向代理nginx --> 真实应用服务器 配置方式 反向代理服务器开启 gzi...

  • Nginx基本配置与参数

    标签(空格分隔): nginx 1 全局配置 2 gzip 配置 3 反向代理配置 4 php虚拟主机配置 5 m...

  • nginx

    nginx是现在网站架构必不可少的工具,最常用的功能就是反向代理,资源动静态分离,还有负载均衡。 1.反向代理 修...

网友评论

      本文标题:二、架构02-Nginx的反向代理、虚拟主机

      本文链接:https://www.haomeiwen.com/subject/tlepeqtx.html