14_部署LNMP环境、构建LNMP平台、地址重写

proxy    10.10.11.10
client     10.10.11.11
web1     10.10.11.12
web2     10.10.11.13
 
1.部署LNMP环境
1.1 部署nginx(前面已部署过)
1.2 部署mariadb
proxy]# yum -y install mariadb mariadb-server mariadb-devel
proxy]# systemctl start mariadb
proxy]# systemctl enable mariadb
proxy]# mysql
1.3 部署php
]# yum -y install php php-mysql php-fpm
]# systemctl start php-fpm
]# systemctl status php-fpm
]# systemctl enable php-fpm
 
2.构建LNMP平台
2.1 查看php-fpm配置文件(实验中不需要修改该文件)
proxy]# vim /etc/php-fpm.d/www.conf
[www]
listen = 127.0.0.1:9000    //PHP端口号
pm.max_children = 32       //最大进程数量
pm.start_servers = 15      //最小进程数量
pm.min_spare_servers = 5   //最少需要几个空闲着的进程
pm.max_spare_servers = 32  //最多允许几个进程处于空闲状态
2.2 修改Nginx配置文件支持php页面,并启动服务
proxy]# vim /etc/nginx/conf.d/default.conf
location / {
        root   /usr/share/nginx/html;
        index index.php  index.html index.htm;
    }
...
location ~ .php$ {
        root           /usr/share/nginx/html;   #绝对路径
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
]# nginx -s reload

2.3 创建PHP测试页面,连接并查询MariaDB数据库
proxy]# vim /usr/share/nginx/html/test.php
<?php
    phpinfo();
?>

client测试:
client]# firefox http://10.10.11.10/test.php
 
3.地址重写 (proxy)
3.1 修改配置文件(访问a.html重定向到b.html)
]# vim /etc/nginx/conf.d/default.conf
...
server_name  www.a.com;
  rewrite /a.html /b.html;
...
]# nginx -s reload
]# echo "aaa" > /usr/share/nginx/html/a.html
]# echo "bbb" > /usr/share/nginx/html/b.html
client测试:
]# firefox 10.10.11.10/a.html
]# firefox 10.10.11.10/b.html

3.2 访问a.html重定向到b.html(跳转地址栏)
]# vim /etc/nginx/conf.d/default.conf
...
server_name  www.a.com;
   rewrite /a.html /b.html redirect;
...
]# nginx -s reload
client测试:
]# firefox 10.10.11.10/a.html

3.3 修改配置文件(访问10.10.11.10的请求重定向至www.baidu.com)
]# vim /etc/nginx/conf.d/default.conf
...
server_name  www.a.com;
  rewrite ^/ http://www.baidu.com/;
...
]# nginx -s reload

client测试:
]# firefox 10.10.11.10

3.4 修改配置文件(访问192.168.4.5/下面子页面,重定向至www.baidu.com/下相同的页面)
]# vim /etc/nginx/conf.d/default.conf
...
server_name  www.a.com;
  rewrite ^/(.*)$ http://www.baidu.com/$1;
...
]# nginx -s reload

lient测试:
]# firefox 10.10.11.10/a.html

3.5 修改配置文件(实现curl和火狐访问相同链接返回的页面不同)
]# echo "I am Normal page" > /usr/share/nginx/html/test.html
]# mkdir -p /usr/share/nginx/html/firefox/
]# echo "firefox page" > /usr/share/nginx/html/firefox/test.html
]# vim /etc/nginx/conf.d/default.conf
...
location / {
   root   html;
   index index.php index.html index.htm;
    }
 
if ($http_user_agent ~* firefox) {
             
    rewrite ^(.*)$ /firefox/$1;
    }
...
]# nginx -s reload

client 测试:
]# firefox http://10.10.11.10/test.html
]# curl http://10.10.11.10/test.html
**********************
地址重写格式【总结】
rewrite   旧地址 新地址 [选项];
last      不再读其他rewrite
break     不再读其他语句,结束请求
redirect  临时重定向
permament 永久重定向
**********************

原文地址:https://www.cnblogs.com/luwei0915/p/12152707.html