Centos7 nginx 虛擬主機、反向代理服務器及負載均衡,多台主機分離php-fpm實驗


一、簡介

  本章介紹一些架構原理基礎知識,

  1.1、LNMP及php-fpm

    請參考:Centos7-yum部署配置LNMP+php-fgm,一台機器上部署

   

  1.2、透明代理、反向代理,正向代理

    請參考透明代理、反向代理,正向代理

    貼一張架構圖

  

  1.3、實現反向代理配置

  

1 server{
2     listen 80;
3     location /{
4         proxy_pass http:192.168.216.52:8080 #上游的應用服務器
5 
6     }
7 }

 

  1.4、負載均衡

  nginx通過反代可以實現負載均衡的效果,上面是通過反向代理實現負載,所以nginx實現的是七層負載均衡,它能夠識別http協議,根據http報文將不同類型的請求轉發到不同的后端web服務器上,后端的web服務器稱為“上游服務器”,即upstream服務器。架構圖和上面類似配置如下:

  

 1 upstream myweb{
 2     server 192.168.216.53:8080;
 3     server 192.168.216.54:8080;
 4 }
 5 
 6 server {
 7     listen 80;
 8     location /{
 9         proxy_pass http://myweb;
10     }
11 }

 

  1.5、nginx的反向代理有幾種實現方式:

    1)僅使用模塊ngx_http_proxy_module實現簡單的反向代理,指令為proxy_pass。

    2)使用fastcgi模塊提供的功能,反向代理動態內容,指令為fastcgi_pass。

    3)使用ngx_http_memcached_module模塊提供的功能,反向代理mencache緩存內容,指令為memcached_pass。

    4)結合upstream模塊實現更人性化的分組反向代理。  

             1.5.1、注意fastcgi_pass與proxy_pass的區別

  1.6、虛擬主機

    有的網站訪問量大,需要負載,然而病逝所有網站都需要,對於訪問量太小的可以將多個網站部署再同一台服務器上,比如你可以把www.test1.com 和www.test2.com兩個網站部署再同一個服務器上,兩個域名解析到同一個ip地址,但是用戶通過兩個域名卻可以打開兩個完全不同的網站,互相不影響,就像訪問兩個服務器一樣

    

 1 server {
 2     listen 80 default_server;
 3     server_name;
 4     return 444; #過濾其他域名的請求,返回444狀態碼
 5 }
 6 
 7 server {
 8     
 9     listen 80;
10     server_name www.test1.com;
11     location /{
12         proxy_pass http://localhost:8080
13     }
14 }
15 
16 server {
17     listen 80;
18     server_name www.test2.com;
19     location /{
20         proxy_pass http://localhost:8081;
21     }
22 }

    

二、部署

  2.1、配置簡單的反代實驗

     2.1.1、架構

  

  反向代理配置挺簡單的,nginx-proxy(192.168.216.51)的配置不需要root、index等指令,只需要幾個和代理相關的指令即可

   2.2、部署web1-51,的反向代理配置

    yum install nginx -y

  修改配置

[root@www ~]# cat /etc/nginx/conf.d/default.conf 
server {
        listen  80;
        server_name www.web1.com;
        location ~ \.(png|jpg|jpeg|bmp|gif)$ {
                proxy_pass http://192.168.216.53:80;
        }

        location / {
                proxy_pass http://192.168.216.54:80;
        }

        location ~ \.(php|php5)$ {
                proxy_pass http://192.168.216.52:80;
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
                root html;
        }
}
[root@www ~]# 

  2.3、部署web3,圖片服務器

  安裝httpd服務

  yum install -y httpd

  放兩張圖片后啟動服務

  

1 [root@www html]# pwd
2 /var/www/html
3 [root@www html]# ll 
4 total 340
5 -rw-r--r--. 1 root root 320660 Jun  7  2018 123.jpg
6 -rw-r--r--. 1 root root  21177 Feb 13 12:45 345.jpg
7 [root@www html]# systemctl restart httpd

  驗證,出錯只顯示一個小方塊瀏覽器f12查看一下,文件大小明顯不對,查看preview也不正常, 重新上傳解決

  

  

  

  正確效果圖如下

  

  2.3、web4部署靜態服務器,同樣是httpd

  web4-54

  yum install -y httpd

  

1 [root@web4 html]# pwd
2 /var/www/html
3 [root@web4 html]# cat index.html 
4 hi,this is web4,static
5 [root@web4 html]# 

  systemctl restart httpd

  查看一下效果,沒問題,下面部署nginx+php-fpm

  

  2.3、部署nginx+php-fpm

  兩台分離部署

  

  2.3.1、web2-52 

  yum install nginx -y 

  配置

 1 [root@web2 html]# vim /etc/nginx/conf.d/default.conf 
 2 
 3 server {
 4         listen 80;
 5         server_name 192.168.216.52;
 6         index index.html index.htm index.phpi;
 7         root /www/web;
 8         location / {
 9 
10                 root /usr/share/nginx/html;
11         }
12 
13         location ~.*\.php$ {
14 
15                 fastcgi_pass    192.168.216.55:9000;
16                 fastcgi_index   index.php;
17                 fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
18                 include         fastcgi_params;
19         }

  systemctl restart nginx

 

  2.3.2、web5-55

  

1  yum install -y php php-devel php-fpm php-mysql php-common php-devel php-gd libjpeg* php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-mcrypt php-bcmath php-mhash libmcrypt libmcrypt-devel
vim   /etc/php-fpm.d/www.conf 

listen = 192.168.216.55:9000

listen.owner = nobody
listen.group = nobody

user = nginx

group = nginx
1  mkdir -p /www/web/
2  group -g 983 nginx
3  groupadd -g 983 nginx
4  useradd -u 983 -g nginx nginx
5 
6 chown -R nginx:nginx /www

  准備phpinfo文件

1 [root@web5 web]# ll 
2 total 4
3 -rwxrwxrwx 1 nginx nginx 21 Feb 12 12:35 index.php
4 [root@web5 web]# cat index.php 
5 <?php
6         phpinfo();
7 ?>
8 [root@web5 web]# 

  注意:如果測試結果為不運行php,下載php得話,應該是nginx配置文件目錄配置不當造成得

   檢查結果,成功

  

  2.4、負載均衡配置

    這個實驗得負載均衡再nginx主機負載php請求,做兩個php-fpm的負載均衡

  修改web2-52的nginx配置文件

  

 1 [root@web2 web]# cat /etc/nginx/conf.d/default.conf 
 2 upstream php-cluster {
 3         server 127.0.0.1:9000 max_fails=3 fail_timeout=10s;
 4         server 192.168.216.55:9000 max_fails=3 fail_timeout=10s;
 5         }
 6 
 7 server {
 8         listen 80;
 9         server_name 192.168.216.52;
10         index index.html index.htm index.phpi;
11         root /www/web;
12         location / {
13 
14                 root /usr/share/nginx/html;
15         }
16 
17         location ~.*\.php$ {
18 
19                 fastcgi_pass    php-cluster;
20                 fastcgi_index   index.php;
21                 fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
22                 include         fastcgi_params;
23         }
24 }
25 [root@web2 web]# 

  並且安裝php,php-fpm

  

1  yum install -y php php-devel php-fpm php-mysql php-common php-devel php-gd libjpeg* php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-mcrypt php-bcmath php-mhash libmcrypt libmcrypt-devel

  配置php-fpm配置文件

  

1 [root@web2 web]# vim /etc/php-fpm.d/www.conf 
2 listen = 127.0.0.1:9000
3 listen.owner = nobody
4 listen.group = nobody
5 user = nginx
6 group = nginx

  創建index.php文件

  mkdir -p /www/web

  vim /www/web/index.php

  

1 [root@web2 web]# cat index.php 
2 <?php
3         echo ("52");  #輸出52,測試的時候容易分辨
4         phpinfo();
5 ?>

  web5-55同樣輸出55

 

1 [root@web5 web]# vim index.php 
2 
3 <?php
4 echo ("55");
5         phpinfo();
6 ?>

 

  好了,重啟服務測試一把

  systemctl restart nginx   #web2

  systemctl restart php-fpm  #web2與web5

  測試效果如下

  

  刷新瀏覽器

  

 

轉載請注明出處:https://www.cnblogs.com/zhangxingeng/p/10330735.html

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM