1、 修改host文件设置nginx虚拟站点
1) 修改host文件
[root@centos01 ~]# cat /etc/hosts
192.168.100.10 www.benet.com www.benet.com.en www.benet.com.zh
2)配置nginx添加虚拟主机
[root@centos01 ~]# vim /usr/local/nginx/conf/nginx.conf
server {
listen 192.168.100.10:80;
server_name www.benet.com;
location / {
root /code/;
index index.html;
}
}
server {
listen 192.168.100.10:80;
server_name www.benet.com.zh www.benet.com.en;
location / {
if ( $http_host ~* "zh") {
set $language zh;
rewrite ^/$ http://www.benet.com/$language redirect;
}
if ( $http_host ~* "en") {
set $language en;
rewrite ^/$ http://www.benet.com/$language redirect;
}
}
}
3)创建虚拟站点根目录
[root@centos01 ~]# mkdir /code/
4)创建两个虚拟站点根目录
[root@centos01 ~]# mkdir /code/en/
[root@centos01 ~]# mkdir /code/zh
[root@centos01 ~]# echo "en" > /code/en/index.html
[root@centos01 ~]# echo "zh" > /code/zh/index.html
2、 当用户用户网址http://www.benet.com/test给用户返回提示
1) 修改主配置文件
[root@centos01 ~]# vim /usr/local/nginx/conf/nginx.conf
server {
listen 192.168.100.10:80;
server_name www.benet.com;
location / {
default_type text/html;
if ( $request_uri ~* "^/test") {
#return 200 "hello!!!";
return 302 http://www.accp.com;
}
}
}
2) 验证www.benet.com/test
输入test返回helloo
输入test访问www.accp.com
3、 地址重写的类型
1) 重写的类型
标记 | 说 明 |
---|---|
last | 相当于Apache的[L]标记,表示完成rewrite |
break | 本条规则匹配完成即终止,不再匹配后面的任何规则 |
redirect | 返回302临时重定向,浏览器地址会显示跳转后的URL地址,爬虫不会更新url |
permanent | 返回301永久重定向,浏览器地址栏会显示跳转后的URL地址,爬虫更新url |
2) 网站根目录写入测试数据
[root@centos01 ~]# echo "1.html" > /code/1.html
[root@centos01 ~]# echo "2.html" > /code/2.html
[root@centos01 ~]# echo "3.html" > /code/3.html
[root@centos01 ~]# echo "a.html" > /code/a.html
[root@centos01 ~]# echo "b.html" > /code/b.html
3) 修改主配置文件
[root@centos01 ~]# vim /usr/local/nginx/conf/nginx.conf
server {
listen 192.168.100.10:80;
server_name www.benet.com;
root /code;
location / {
rewrite /1.html /2.html;
rewrite /2.html /3.html;
}
location /2.html {
rewrite /2.html /a.html;
}
location /3.html {
rewrite /3.html /b.html;
}
}
4) 客户端端验证访问
http://www.benet.com/1.html最终被跳转到b.html
5) 配置停止跳转
[root@centos01 code]# vim /usr/local/nginx/conf/nginx.conf
server {
listen 192.168.100.10:80;
server_name www.benet.com;
root /code;
location / {
rewrite /1.html /2.html break;
rewrite /2.html /3.html;
}
location /2.html {
rewrite /2.html /a.html;
}
location /3.html {
rewrite /3.html /b.html;
}
}
6) 客户端验证
http://www.benet.com/1.html将被跳转到2.html停止
4、 last的应用
1) 修改nginx配置文件
[root@centos01 ~]# vim /usr/local/nginx/conf/nginx.conf
server {
listen 192.168.100.10:80;
server_name www.benet.com;
root /code;
location / {
rewrite /1.html /2.html last;
rewrite /2.html /3.html;
}
location /2.html {
rewrite /2.html /a.html;
}
location /3.html {
rewrite /3.html /b.html;
}
}
2) 客户端访问
www.bene.com/1.html停止在a.html
5、 配置用户输入bbs.benet.com/test/1/index.php跳转到www.benet.com/test/1/index.php
1) 修改nginx主配置文件
[root@centos01 ~]# vim /usr/local/nginx/conf/nginx.conf
server {
listen 192.168.100.10:80;
server_name www.benet.com;
location / {
root /code/;
index index.html index.php;
}
}
server {
listen 192.168.100.10:80;
server_name bbs.benet.com;
location /test/1/ {
root /bbs/;
index index.html;
if ($host = 'bbs.benet.com') {
rewrite (.+) http://www.benet.com/$1 permanent;
}
}
}
2) 设置网站测试
[root@centos01 ~]# mkdir /bbs/test/1/
[root@centos01 ~]# echo "bbs.benet.com.php" > /bbs/test/1/index.php
[root@centos01 ~]# mkdir /code/test/1/
[root@centos01 ~]# echo "www.benet.com.index.php" > /code/test/1/index.php
3) 用户输入访问数据库
http://bbs.benet.com//test/1/index.php
4) 验证跳转到特定网站
6、 配置参数跳转www.benet.com/ 100-200-100.htm跳转到www.benet.com网站主页
1) 修改nginx主配置
server {
listen 192.168.100.10:80;
server_name www.benet.com;
location / {
root /code/;
index index.html index.php;
if ( $request_uri ~ ^/100-200-100.html$ ) {
rewrite (.*) http://www.benet.com permanent;
}
}
}
2) 设置访问页面
[root@centos01 ~]# echo "test" > /code/100-200-100.html
3) 客户端访问测试
http://www.benet.com/100-200-100.html
4) 跳转到www.benet.com主页
7、 配置客户端输入www.benet.com/upload/index.php访问www.benet.com主页
1) 修改nginx主配置文件
[root@centos01 ~]# vim /usr/local/nginx/conf/nginx.conf
server {
listen 192.168.100.10:80;
server_name www.benet.com;
location / {
root /code/;
index index.html index.php;
}
location ~* /upload/.*\.php$ {
rewrite (.+) http://www.benet.com permanent;
}
}
2)设置网页根目录
[root@centos01 ~]# mkdir /code/upload/
[root@centos01 ~]# echo "www.benet.com.php" > /code/upload/index.php
3) 客户端输入域名访问
www.benet.com/upload/index.php
4) 跳转到网站主页
8、 访问www.benet.com/1/index.html跳转到首页
1) 修改nginx主配置文件
[root@centos01 ~]# vim /usr/local/nginx/conf/nginx.conf
server {
listen 192.168.100.10:80;
server_name www.benet.com;
location / {
root /code/;
index index.html index.php;
}
location ~* ^/1/test.html {
rewrite (.+) http://www.benet.com permanent;
}
2) 创建网站根目录
[root@centos01 ~]# mkdir /code/1/
[root@centos01 ~]# echo "test" > /code/1/test.html
3) 客户端访问
http://www.benet.com/1/test.html
4) 跳转到www.benet.com首页
9、 允许特定IP地址访问nginx其他任何IP地址访问报错
1) 修改nginx主配置文件
[root@centos01 ~]# vim /usr/local/nginx/conf/nginx.conf
server {
listen 192.168.100.10:80;
server_name www.benet.com;
location / {
root /code/;
index index.html error.html;
set $rewrite true;
if ($remote_addr = "192.168.100.80"){
set $rewrite false;
}
if ($rewrite = true){
rewrite (.+) /error.html;
}
location = /error.html {
root /error/;
}
}
}
2) 创建错误也网站根目录
[root@centos01 ~]# mkdir /error/
[root@centos01 ~]# echo "error" > /error/error.html
3) 用允许192.168.100.80 的IP地址访问网站
4) 非IP地址192.168.100.80访问