nginx怎么配置url重定向(nginx配置index.html)

nginx怎么配置url重定向(nginx配置index.html)

浏览次数:
信息来源: 用户投稿
更新日期: 2026-03-21
文章简介

本文系统:centos6.5_x64三台主机:nginx主机,hostname:master.lansgg.comip:192.168.10.128apache主机,hostname:clien

2025阿里云双十一服务器活动

本文系统:centos6.5_x64

三台主机:nginx主机,hostname:master.lansgg.comip:192.168.10.128apache主机,hostname:client1.lansgg.comip:192.168.10.129

1、地址重定向:是指当使用者浏览某个网址时,将他导向到另一个网址的技术。常用在把一串很长的网址,转成较短的网址。因为当要传播某网站时,常常因为网址太长,不好记忆;又有可能因为换了网路的免费网页空间,网址又必须要变更,不知情的使用者还以为网站关闭了。这时就可以用网路上的转址了。这个技术使一个网页是可借由不同的统一资源定位符(url)连结。

1.1、这个模块允许使用正则表达式重写uri(需pcre库),并且可以根据相关变量重定向和选择不同的配置。如果这个指令在server字段中指定,那么将在被请求的location确定之前执行,如果在指令执行后所选择的location中有其他的重写规则,那么它们也被执行。如果在location中执行这个指令产生了新的uri,那么location又一次确定了新的uri。这样的循环可以最多执行10次,超过以后nginx将返回500错误

*!~和!~*分别为区分大小写不匹配及不区分大小写不匹配

*-f和!-f用来判断是否存在文件

*-d和!-d用来判断是否存在目录

*-e和!-e用来判断是否存在文件或目录

*-x和!-x用来判断文件是否可执行

flag标记有:*last相当于apache里的[l]标记,表示完成rewrite

*break终止匹配,不再匹配后面的规则

*redirect返回302临时重定向地址栏会显示跳转后的地址

*permanent返回301永久重定向地址栏会显示跳转后的地址

一些可用的全局变量有,可以用做条件判断

$content_length,http请求信息里的"content-length";

$content_type,请求信息里的"content-type";

$document_root,针对当前请求的根路径设置值;

$document_uri,与$uri相同;

$host,请求信息中的"host",如果请求中没有host行,则等于设置的服务器名;

$limit_rate,对连接速率的限制;

$request_method,请求的方法,比如"get"、"post"等;

$remote_port,客户端端口号;

$remote_user,客户端用户名,认证用;

$request_filename,当前请求的文件路径名

$request_uri,请求的uri,带查询字符串;

$query_string,与$args相同;

$scheme,所用的协议,比如http或者是https,比如rewrite^(.+)$$scheme://example.com$1redirect;

$server_protocol,请求的协议版本,"http/1.0"或"http/1.1";

$server_addr,服务器地址,如果没有用listen指明服务器地址,使用这个变量将发起一次系统调用以取得地址(造成资源浪费);

$server_name,请求到达的服务器名;

$server_port,请求到达的服务器端口号;

$uri,请求的uri,可能和最初的值有不同,比如经过重定向之类的。

  • rewrite^(/download/.*)/media/(.*)\..*$$1/mp3/$2.mp3last;

    rewrite^(/download/.*)/audio/(.*)\..*$$1/mp3/$2.ralast;

    return403;

    但是如果我们将其放入一个名为/download/的location中,则需要将last标记改为break,否则nginx将执行10次循环并返回500错误。

    rewrite^(/download/.*)/media/(.*)\..*$$1/mp3/$2.mp3break;

    rewrite^(/download/.*)/audio/(.*)\..*$$1/mp3/$2.rabreak;

    }

    如果替换字段中包含参数,那么其余的请求参数将附加到后面,为了防止附加,可以在最后一个字符后面跟一个问号:

    rewrite^/users/(.*)$/show?user=$1?last;

    注意:大括号({和}),可以同时用在正则表达式和配置块中,为了防止冲突,正则表达式使用大括号需要用双引号(或者单引号)。例如要重写以下的url:

    /photos/123456

    为:

    /path/to/photos/12/1234/123456.png

    则使用以下正则表达式(注意引号):

    rewrite"/photos/([0-9]{2})([0-9]{2})([0-9]{2})"/path/to/photos/$1/$1$2/$1$2$3.png;

    如果指定一个“?”在重写的结尾,nginx将丢弃请求中的参数,即变量$args,当使用$request_uri或$uri&$args时可以在rewrite结尾使用“?”以避免nginx处理两次参数串。

    在rewrite中使用$request_uri将www.example.com重写到example.com:

    server_namewww.example.com;

    rewrite^http://example.com$request_uri?permanent;

    }

    同样,重写只对路径进行操作,而不是参数,如果要重写一个带参数的url,可以使用以下代替:

    rewrite^http://example.com/new-address.html?permanent;

    }

    示例:当访问www.lansgg.com的时候跳转到www.aries.com;

    server_namewww.lansgg.comlansgg.com;

    access_loglogs/lansgg.access.logmain;

    error_loglogs/lansgg.error.log;

    root/opt/nginx/nginx/html/lansgg;

    rewrite^/http://www.aries.com/;

    }

    break指令可使用server,location,if区域;中止rewirte,不在继续匹配

    last指令可server,location,if区域;

    last与break的区别在于,last并不会停止对下面location的匹配。

    测验一下break与last的区别

    server_namewww.lansgg.comlansgg.com;

    nginx怎么配置url重定向,nginx配置index.html

    error_loglogs/lansgg.error.log;

    root/opt/nginx/nginx/html/lansgg;

    rewrite/c1.html/c2.htmlbreak;

    [root@mastersbin]#echo"c1">/opt/nginx/nginx/html/lansgg/c1.html

    [root@mastersbin]#echo"c2">/opt/nginx/nginx/html/lansgg/c2.html

    使用break会停止匹配下面的location,直接发起请求,他会显示c2的内容;

    使用last的话,会继续搜索下面是否有符合条件(符合重写后的/c2.html请求)的location。此时,/c2.html刚好与面location的条件对应上了,进入花括号{}里面的代码执行,这里会返回508。

    server_namewww.lansgg.comlansgg.com;

    access_loglogs/lansgg.access.logmain;

    error_loglogs/lansgg.error.log;

    root/opt/nginx/nginx/html/lansgg;

    rewrite/c1.html/c2.htmllast;

    }

    使用firebug可以看到;

    if指令可使用server,location区域;

    示例:当访问网址的时候跳转到www.aries.com;

    server_namewww.lansgg.comlansgg.com;

    access_loglogs/lansgg.access.logmain;

    error_loglogs/lansgg.error.log;

    root/opt/nginx/nginx/html/lansgg;

    if($http_host=www.lansgg.com){

    rewrite(.*)http://www.aries.com;

    }

    epoch指定“expires”的值为1january,1970,00:00:01gmt。

    max指定“expires”的值为31december203723:59:59gmt,“cache-control”的值为10年。

    -1指定“expires”的值为服务器当前时间-1s,即永远过期

    “cache-control”头标的值由指定的时间来决定:

    负数:cache-control:no-cache

    正数或零:cache-control:max-age=为指定时间的秒数s。其他的单位有d(天),h(小时)

    "off"表示不修改“expires”和“cache-control”的值

    控制图片等过期时间为30天,这个时间可以设置的更长。具体视情况而定

    location~\.(gif|jpg|jpeg|png|bmp|ico)${

    log_not_foundoff;#不记录404notfound错误日志expires30d;

    break;}

    控制匹配/resource/或者/mediatormodule/里所有的文件缓存设置到最长时间

    location~/(resource|mediatormodule)/{

    }

    设定某个文件的过期时间;这里为600秒,并不记录访问日志

    location^~/html/scripts/loadhead_1.js{

    root/opt/lampp/htdocs/web;

    gzip_typestext/plainapplication/x-javascripttext/csstext/htmlapplication/xml;

    可以通过网页gzip检测工具来检测网页是否启用了gzip

    临时重定向示例:访问www.lansgg.com/c重定向到www.lansgg.com/cc

    server_namewww.lansgg.comlansgg.com;

    access_loglogs/lansgg.access.logmain;

    error_loglogs/lansgg.error.log;

    root/opt/nginx/nginx/html/lansgg;

    rewrite^/c/(.*)$http://www.lansgg.com/cc/$1;

    2directories,4files

    302即为临时重定向;

    server_namewww.lansgg.comlansgg.com;

    access_loglogs/lansgg.access.logmain;

    error_loglogs/lansgg.error.log;

    root/opt/nginx/nginx/html/lansgg;

    }

    2.2、配置nginx配置文件nginx.conf

    server_namewww.lansgg.comlansgg.com;

    access_loglogs/lansgg.access.logmain;

    error_loglogs/lansgg.error.log;

    root/opt/nginx/nginx/html/lansgg;

    proxy_passhttp://192.168.10.129/other;

    proxy_set_headerx-real-ip$remote_addr;

    }

    2.3、配置client1

    echo"192.168.10.129">/var/www/html/other/index.html

    查看日志:

    [root@client1~]#tail-f/var/log/httpd/access_log

    192.168.10.1--[06/nov/2014:21:25:44+0800]"get/other/http/1.1"20015"-""mozilla/5.0(windowsnt6.1;wow64;rv:32.0)gecko/20100101firefox/32.0"

    以上就是nginx怎么配置url重定向的详细内容,更多请关注主机测评网其它相关文章!

    标签:
  • 什么是项目管理中的瀑布方法(项目管理瀑布式)
    « 上一篇
    返回列表
    下一篇 »

    如本文对您有帮助,就请抽根烟吧!