码迷,mamicode.com
首页 > 其他好文 > 详细

LNMP部署(地址重写)

时间:2018-03-06 11:28:45      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:LNMP及地址重写学习

location 作用:
可以有多个
会匹配地址栏里的扩展地址进行匹配
支持正则: ~ .php$ 代表正则匹配 模糊匹配 以.php结尾的地址

格式
rewrite 旧地址 新地址 选项
rewrite regex replacement flag

选项flag可以是如下参数
last
停止执行其他重写规则,根据url继续搜索其他location,地址栏不改变

break
停止执行其他重写规则,完成本次请求

redirect
302临时重定向地,址栏改变,爬虫不更新url

permanent
301永久重定向,地址栏改变,爬虫更新url

rewrite a b last;防止来回跳转
rewrite b a;

正则表达式
区分大小写匹配 ~
不区分大小写匹配 ~
区分大小写不匹配 !~
不区分大小写不匹配 !~

判断文件是否存在 -f
判断目录是否存在 -d
判断文件是否可执行 -x
判断文件目录连接是否存在 -e

域名跳转 #server里location外
server_name www.360buy.com;
rewrite ^/(.*) http://www.jd.com/$1;
location / {

也面跳转 #location里
location / {
if ( $http_user_agent ~ firefox ) {
rewrite ^(.
)$ /test$1 break;
}


LNMP 动态网站
L linux
N Nginx
M mysql mariadb
P 网站开发语言 PHP Perl Python...

安装的软件列表如下:
nginx
mariadb、mariadb-server、mariadb-devel
php、php-fpm、php-mysql

#yum -y install mariadb mariadb-server
#yum -y install mariadb-devel
#yum -y install php php-mysql
#rpm -ivh php-fpm-5.4.16-36.el7_1.x86_64.rpm 服务端口9000

起服务
#nginx #端口80
#systemctl start php-fpm #端口9000
#systemctl start mariadb #端口3306

开机启动目录 /usr/lib/systemd/system/

nginx 相关配置
源文件里有模版 需要修改
#vim /usr/local/nginx/conf/nginx.conf
......
location ~ .php$ {
root html; #页面目录
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi.conf; #包含的子配置文件
}
......

location 作用:
可以有多个
会匹配地址栏里的扩展地址进行匹配
支持正则: ~ .php$ 代表正则匹配 模糊匹配 以.php结尾的地址


模版
wordpress PHP
织梦 PHP
帝国 PHP
php-fpm配置文件
#vim /etc/php-fpm.d/www.conf
[www]
listen = 127.0.0.1:9000
listen.allowed_clients = 127.0.0.1
user = apache
group = apache
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35

创建PHP测试页面1:
#vim /usr/local/nginx/html/test1.php
<?php
$i="This is a test Page";
echo $i;
?>

创建PHP测试页面2,连接MariaDB数据库:
#vim /usr/local/nginx/html/test2.php
<?php
$links=mysql_connect("localhost","root","密码");
//注意:root为mysql账户名称,密码需要修改为实际mysql密码,无密码则留空即可
if($links){
echo "link db ok!!!";
}
else{
echo "link db no!!!";
}
?>

创建PHP测试页面3,连接并查询MariaDB数据库:
#vim /usr/local/nginx/html/test3.php
<?php
$mysqli = new mysqli(‘localhost‘,‘root‘,‘‘,‘mysql‘);
if (mysqli_connect_errno()){
die(‘Unable to connect!‘). mysqli_connect_error();
}
$sql = "select * from user";
$result = $mysqli->query($sql);
while($row = $result->fetch_array()){
printf("Host:%s",$row[0]);
printf("</br>");
printf("Name:%s",$row[1]);
printf("</br>");
}
?>

测试
#firefox http://192.168.4.5/test1.php
#firefox http://192.168.4.5/test2.php
#firefox http://192.168.4.5/test3.php


地址重写:

格式
rewrite 旧地址 新地址 选项
rewrite regex replacement flag

选项flag可以是如下参数
last
停止执行其他重写规则,根据url继续搜索其他location,地址栏不改变

break
停止执行其他重写规则,完成本次请求

redirect
302临时重定向地,址栏改变,爬虫不更新url

permanent
301永久重定向,地址栏改变,爬虫更新url

rewrite a b last;
rewrite b a;

页面的跳转
注意位置 在location里面
#vim /usr/local/nginx/conf/nginx.conf
.. ..
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
rewrite /a.html /b.html redirect;
}
}

#redirect 客户端显示跳转
是支持正则的 a.html的.(点)也算任意单个字符
a.html文件不需要存在

域名的跳转

访问www.360buy.com跳转到www.jd.com
注意位置 server里面location外面
#vim /usr/local/nginx/conf/nginx.conf
.. ..
server {
listen 80;
server_name www.360buy.com;
rewrite ^/(.*) http://www.jd.com/$1;
location / {
root html;
index index.html index.htm;

}
}

访问www.nidama.com跳转到www.nidaye.com

#vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.nidama.com;

    rewrite ^/(.*) http://www.nidaye.com/$1;
    location / {
        root   html;
        index  index.html index.htm;
    }
}
server {
    listen       80;
    server_name  www.nidaye.com;
    location / {
        root   html;
        index  index.html index.htm;
    }
}

注 rewrite ^/(.) http://www.jd.com/$1;
(.
)代表保留扩展域名后面的$1就代表前面的扩展域名
实现只跳转 域名 不影响子网页

charset utf-8; #万国解码 支持中文显示

正则表达式
区分大小写匹配 ~
不区分大小写匹配 ~
区分大小写不匹配 !~
不区分大小写不匹配 !~

判断文件是否存在 -f
判断目录是否存在 -d
判断文件是否可执行 -x
判断文件目录连接是否存在 -e

server {
   listen  80;
   server_name www.test.com;

location / {
        root   html;
        index  index.html;
    }
if ($http_user_agent ~* curl){
        rewrite ^(.*)$ /test$1;
    }
}

根 是当匹配不到后默认匹配的路径 根的优先级最低
location / {
root html;
index index.htm;

##################################################################

实验总结:

对IP先做域名跳转 后做页面跳转 之后再皮匹配阅览器访问不同样式的页面。

IP-->www.test.com-->将访问a.html(不存在)跳转至b.html-->firefox 阅览器第一版版 ,其他阅览器第一版

#ls
index.html test01 test02
test01]# ls #二版页面
b.html inde.html
test02]# ls #一版页面
b.html inde.html

#vim /usr/local/nginx/conf/nginx.conf
...
server {
listen 80;
server_name localhost;

    rewrite ^/(.*) http://www.test.com/$1 break;

    location / {
        root   html;
        index  index.html index.htm;
    }

...
server {
listen 80;
server_name www.test.com;

location / {
    root    html;
    index   inde.html;

if ( $request_uri = "/" ) {      #扩展页面啥也不输入会跳转一个页面
    rewrite / /index.html break;
    }

    rewrite /a.html /b.htmlredirect;    #将扩展名a改b访问

if ( $http_user_agent ~* firefox ) {    #客户端为火狐跳到一版页面
    rewrite ^/(.*) /test01/$1 break;
    }
if ( $http_user_agent ~* curl ) {   #客户端为curl跳到二版页面
    rewrite ^/(.*) /test02/$1 break;
    }
}
}

nginx内置变量:

//该段为自定义log输出样式函数。配合下面的access_log使用  
//日志格式设置:  
//$remote_addr与$http_x_forwarded_for用以记录客户端的ip地址;  
//$remote_user:用来记录客户端用户名称;  
//$time_local: 用来记录访问时间与时区;  
//$request: 用来记录请求的url与http协议;  
//$status: 用来记录请求状态;成功是200,  
//$body_bytes_sent :记录发送给客户端文件主体内容大小;  
//$http_referer:用来记录从那个页面链接访问过来的;  
//$http_user_agent:记录客户浏览器的相关信息  

###################################################################

LNMP部署(地址重写)

标签:LNMP及地址重写学习

原文地址:http://blog.51cto.com/45545613/2083323

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!