Nginx基于域名跳转、基于客户端IP访问跳转、基于目录下所有php文件跳转

配置Nginx基于域名跳转、基于客户端IP访问跳转、基于目录下所有php文件跳转

一、基于域名跳转
1、配置访问www.benet.com/test/1/index.php跳转到www.benet.com/test/1/index.php
1)
创建两个站点网站目录

[root@centos01 ~]# mkdir -p /code/test/1/
[root@centos01 ~]# echo “www.benet.com” > /code/index.html
[root@centos01 ~]# echo “www.benet.com.php” > /code/test/1/index.html
[root@centos01 ~]# mkdir -p /accp/test/1/
[root@centos01 ~]# echo “www.accp.com” > /accp/index.html
[root@centos01 ~]# echo “www.accp.com.php” > /accp/test/1/index.html
2)
修改nginx主配置文件

[root@centos01 ~]# vim /usr/local/nginx/conf/nginx.conf
server {
listen 192.168.100.10:80;
server_name www.benet.com;
location /test/1/ {
root /code/;
index index.html index.php;
if ($host = ‘www.benet.com’) {
rewrite (.+) http://www.accp.com/$1 permanent;
}
}
}
server {
listen 192.168.100.10:80;
server_name www.accp.com;
location / {
root /accp/;
index index.html index.php;
}
}
在这里插入图片描述
3)
检查配置是否错误杀掉nginx并重新开启

[root@centos01 ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@centos01 ~]# killall nginx
[root@centos01 ~]# nginx
4)
配置dns

[root@centos01 ~]# vim /etc/named.conf
options {
listen-on port 53 { any; };
directory “/var/named”;
};
zone “benet.com” IN {
type master;
file “benet.com.zone”;
};
zone “accp.com” IN {
type master;
file “accp.com.zone”;
};
在这里插入图片描述
[root@centos01 ~]# vim /var/named/benet.com.zone

$TTL 86400
@ SOA benet.com. root.benet.com (
2020031210
1H
15M
1W
1D
)
@ NS centos01.benet.com.
centos01 A 192.168.100.10
www A 192.168.100.10
在这里插入图片描述
[root@centos01 ~]# vim /var/named/accp.com.zone

$TTL 86400
@ SOA accp.com. root.accp.com (
2020031210
1H
15M
1W
1D
)
@ NS centos01.accp.com.
centos01 A 192.168.100.10
www A 192.168.100.10

在这里插入图片描述
[root@centos01 ~]# systemctl restart named
5)
客户端配置IP地址访问www.benet.com/test/1/index.html

在这里插入图片描述
在这里插入图片描述
二、基于客户端IP访问跳转
1、配置网站升级网站管理员IP地址192.168.100.111可以访问,其他IP地址访问报错
1)
修改nginx主配置文件

[root@centos01 ~]# vim /usr/local/nginx/conf/nginx.conf
server {
listen 192.168.100.10:80;
server_name www.accp.com;
location / {
root /accp/;
index index.html index.php error.html;
set $rewrite true;
if ( $remote_addr = “192.168.100.111”) {
set $rewrite false;
}
if ( $rewrite = true){
rewrite (.+) /error.html;
}
location = /error.html {
root /error/;
}
}
}
在这里插入图片描述
2)
检查配置是否错误杀掉nginx并重新开启

[root@centos01 ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@centos01 ~]# killall nginx
[root@centos01 ~]# nginx
3)
客户端访问,使用192.168.100.111访问主页是正确的网页

在这里插入图片描述
在这里插入图片描述
使用其他IP地址者是错误网页
在这里插入图片描述
在这里插入图片描述

三、基于目录下所有php文件跳转
1、用户访问www.accp.com/uploade/index.php跳转到www.accp.com主页
1)
修改nginx主配置文件

[root@centos01 ~]# vim /usr/local/nginx/conf/nginx.conf
server {
listen 192.168.100.10:80;
server_name www.accp.com;
location / {
root /accp/;
index index.html index.php error.html;
}
location ~* /uploade/.*.php$ {
rewrite (.+) http://www.accp.com/ permanent;
}
}
在这里插入图片描述
2)
检查配置是否错误杀掉nginx并重新开启

[root@centos01 ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@centos01 ~]# killall nginx
[root@centos01 ~]# nginx
3)
创建访问uploade网站根目录

[root@centos01 ~]# mkdir /accp/uploade/
[root@centos01 ~]# echo “www.accp.uploade.php” > /accp/uploade/index.php
4)
客户端访问www.accp.com/uploade/index.php

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值