nginx跳转参数与实例

上传人:小** 文档编号:89126251 上传时间:2019-05-19 格式:DOC 页数:20 大小:46.50KB
返回 下载 相关 举报
nginx跳转参数与实例_第1页
第1页 / 共20页
nginx跳转参数与实例_第2页
第2页 / 共20页
nginx跳转参数与实例_第3页
第3页 / 共20页
nginx跳转参数与实例_第4页
第4页 / 共20页
nginx跳转参数与实例_第5页
第5页 / 共20页
点击查看更多>>
资源描述

《nginx跳转参数与实例》由会员分享,可在线阅读,更多相关《nginx跳转参数与实例(20页珍藏版)》请在金锄头文库上搜索。

1、Nginx跳转参数与实例Nginx跳转参数与实例http:/ =|*| /uri/ location = / # 只匹配 / 查询。 configuration A location / # 匹配任何查询,因为所有请求都已 / 开头。但是正则表达式规则和长的块规则将被优先和查询匹配。 configuration B location /images/ # 匹配任何已 /images/ 开头的任何查询并且停止搜索。任何正则表达式将不会被测试。 configuration C location * .(gif|jpg|jpeg)$ # 不区分大小写匹配任何已 gif、jpg 或 jpeg 结尾的请

2、求。然而所有 /images/ 目录的请求将使用 Configuration C。 configuration D 正则表达式匹配,其中:1.* 为区分大小写匹配 2.* * 为不区分大小写匹配 3.* !和!*分别为区分大小写不匹配及不区分大小写不匹配文件及目录匹配,其中:1.* -f和!-f用来判断是否存在文件 2.* -d和!-d用来判断是否存在目录 3.* -e和!-e用来判断是否存在文件或目录 4.* -x和!-x用来判断文件是否可执行flag标记有:1.* last 相当于Apache里的L标记,表示完成rewrite 2.* break 终止匹配, 不再匹配后面的规则 3.* r

3、edirect 返回302临时重定向 地址栏会显示跳转后的地址 4.* permanent 返回301永久重定向 地址栏会显示跳转后的地址一些可用的全局变量有,可以用做条件判断(待补全)1.$args 2.$content_length 3.$content_type 4.$document_root 5.$document_uri 6.$host 7.$http_user_agent 8.$http_cookie 9.$limit_rate 10.$request_body_file 11.$request_method 12.$remote_addr 13.$remote_port 14.

4、$remote_user 15.$request_filename 16.$request_uri 17.$query_string 18.$scheme 19.$server_protocol 20.$server_addr 21.$server_name 22.$server_port 23.$uri结合QeePHP的例子1.if (!-d $request_filename) 2.rewrite /(a-z-A-Z+)/(a-z-A-Z+)/?(.*)$ /index.php?namespace=user&controller=$1&action=$2&$3 la

5、st; 3.rewrite /(a-z-A-Z+)/?$ /index.php?namespace=user&controller=$1 last; 4.break;多目录转成参数 = ($host * (.*)) 2.set $sub_name $1; 3.rewrite /sort/(d+)/?$ /index.php?act=sort&cid=$sub_name&id=$1 last; 4.目录对换/123456/xxxx - /xxxx?id=1234561.rewrite /(d+)/(.+)/ /$2?id=$1 last;例如下面设定nginx在用户使用ie的使用重定向

6、到/nginx-ie目录下:1.if ($http_user_agent MSIE) 2.rewrite (.*)$ /nginx-ie/$1 break; 3.目录自动加“/”1.if (-d $request_filename) 2.rewrite /(.*)(/)$ http:/$host/$1$2/ permanent; 3.禁止htaccess1.location /.ht 2. deny all; 3. 禁止多个目录1.location /(cron|templates)/ 2. deny all; 3.break; 4. 禁止以/data开头的文件可以禁止/data/下多级目录下

7、.log.txt等请求;1.location /data 2. deny all; 3. 禁止单个目录不能禁止.log.txt能请求1.location /searchword/cron/ 2. deny all; 3. 禁止单个文件1.location /data/sql/data.sql 2. deny all; 3. 给favicon.ico和robots.txt设置过期时间;这里为favicon.ico为99天,robots.txt为7天并不记录404错误日志1.location (favicon.ico) 2. log_not_found off; 3.expires 99d; 4.

8、break; 5. 6. 7. location (robots.txt) 8. log_not_found off; 9.expires 7d; 10.break; 11. 设定某个文件的过期时间;这里为600秒,并不记录访问日志1.location /html/scripts/loadhead_1.js 2. access_log off; 3. root /opt/lampp/htdocs/web; 4.expires 600; 5.break; 6. 文件反盗链并设置过期时间这里的return 412 为自定义的http状态码,默认为403,方便找出正确的盗链的请求“rewrite /

9、 http:/ off;”不记录访问日志,减轻压力“expires 3d”所有文件3天的浏览器缓存1.location * .+.(jpg|jpeg|gif|png|swf|rar|zip|css|js)$ 2.valid_referers none blocked * * localhost 208.97.167.194; 3.if ($invalid_referer) 4. rewrite / http:/ 5. return 412; 6. break; 7. 8. access_log off; 9. root /opt/lampp/htdocs/web; 10.expires 3d;

10、 11.break; 12. 只充许固定ip访问网站,并加上密码1.root /opt/htdocs/www; 2.allow 208.97.167.194; 3.allow 222.33.1.2; 4.allow 231.152.49.4; 5.deny all; 6.auth_basic C1G_ADMIN; 7.auth_basic_user_file htpasswd;将多级目录下的文件转成一个文件,增强seo效果/job-123-456-789.html 指向/job/123/456/789.html1.rewrite /job-(0-9+)-(0-9+)-(0-9+).html$

11、/job/$1/$2/jobshow_$3.html last;将根目录下某个文件夹指向2级目录如/shanghaijob/ 指向 /area/shanghai/如果你将last改成permanent,那么浏览器地址栏显是/location/shanghai/1.rewrite /(0-9a-z+)job/(.*)$ /area/$1/$2 last;上面例子有个问题是访问/shanghai 时将不会匹配1.rewrite /(0-9a-z+)job$ /area/$1/ last; 2.rewrite /(0-9a-z+)job/(.*)$ /area/$1/$2 last;这样/shang

12、hai 也可以访问了,但页面中的相对链接无法使用,如./list_1.html真实地址是/area/shanghia/list_1.html会变成/list_1.html,导至无法访问。那我加上自动跳转也是不行咯(-d $request_filename)它有个条件是必需为真实目录,而我的rewrite不是的,所以没有效果1.if (-d $request_filename) 2.rewrite /(.*)(/)$ http:/$host/$1$2/ permanent; 3.知道原因后就好办了,让我手动跳转吧1.rewrite /(0-9a-z+)job$ /$1job/ permanent; 2.rewrite /(0-9a-z+)job/(.*)$ /area/$1/$2 last;文件和目录不存在的时候重定向:1.if (!-e $request_filename) 2.proxy_pass http:/127.0.0.1; 3.域名跳转

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 商业/管理/HR > 管理学资料

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号