一、配置地址重写(例子)
1、配置网址跳转(用户输入nginx.iso.com跳转到media.iso.com)
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
server {
listen 192.168.200.20:80;
server_name nginx.iso.com;
root /var/www/wordpress;
index index.html;
}
server {
listen 80;
server_name test.iso.com;
location / {
if ($host = "test.iso.com") {
rewrite ^/(.*)$ http://nginx.iso.com/$1 permanent;
}
}
}
2、配置客户端带参数跳转(通过访问tes.iso.com/100-100|200-*.html 转化到nginx.iso.com中)
server {
listen 192.168.200.20:80;
server_name nginx.iso.com;
location / {
root /var/www/wordpress;
index index.html;
}
}
server {
listen 192.168.200.20:80;
server_name test.iso.com;
location / {
if ( $request_uri ~ ^/100-(100|200)-(\d+).html$ ) {
rewrite (.*) http://nginx.iso.com permanent;
}
}
}
3、配置基于域名的跳转(nginx.iso.com/test/1/index.html 跳转到test.iso.com/test/1/index.html)
用户只要访问nginx.iso.com/test/1/就会跳转到test.iso.com/test/1/(nginx.iso.com/test/不会跳转)
server {
listen 80;
server_name nginx.iso.com;
root /var/www/wordpress;
location /test/1/ {
if ($host = "nginx.iso.com") {
rewrite ^/(.*)$ http://test.iso.com/$1 permanent;
}
}
}
4、配置网站升级,网站管理员IP地址(192.168.200.222)可以访问网站,其他请求全部拒绝
server {
listen 80;
server_name nginx.iso.com;
location / {
root /var/www/wordpress;
index index.php index.html;
set $rewrite true;
if ( $remote_addr = "192.168.200.222" ) {
set $rewrite false;
}
if ( $rewrite = true ) {
rewrite (.+) /error.html;
}
location = /error.html {
root /error; 需要手动创建
}
}
}
5、用户访问nginx.iso.com/upload/index.html 跳转到 media.iso.com 主页上
server {
listen 80;
server_name nginx.iso.com;
location / {
root /var/www/wordpress;
index index.php index.html;
}
location ~* /upload/.*\.php$ { 根目录下upload下的所有.php的文件都会匹配
rewrite (.+) http://media.iso.com permanent; 进行跳转,或给用户提醒
或 return 301 http://media.iso.com
或 return 200 "The Server Is Upgrade"
}
}
6、last和break的应用
[root@nginx /]# vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name nginx.iso.com;
root /var/www/wordpress;
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;
}
}
[root@nginx wordpress]# echo “1.html” > ./1.html
[root@nginx wordpress]# echo “3.html” > ./2.html
[root@nginx wordpress]# echo “2.html” > ./2.html
[root@nginx wordpress]# echo “3.html” > ./3.html
[root@nginx wordpress]# echo “a.html” > ./a.html
[root@nginx wordpress]# echo “b.html” > ./b.html
客户端验证:http://www.benet.com/1.html最终被跳转到b.html
1)last用法
server {
listen 80;
server_name nginx.iso.com;
root /var/www/wordpress;
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;
}
}
客户端验证:http://www.benet.com/1.html最终被跳转到a.html
2)break用法
server {
listen 80;
server_name nginx.iso.com;
root /var/www/wordpress;
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;
}
}
客户端验证:http://www.benet.com/1.html最终被跳转到2.html
7、常见的变量
$request_uri 除域名外的其他部分
$host 网站的域名
$http_host 域名加其他,相当于$host+$request_uri
$remote_addr 客户端的IP地址