nginx自定义路由

改写路径

# 匹配/service/开头的任何查询并且停止搜索。任何正则表达式将不会被测试。
location ^~ /service/ {
    #将service指向/html/service目录
    #测试过程中,可以用 在结尾加上 redirect 查看跳转结果
    #rewrite ^/service/(.*)$  /html/service/$1$2 redirect;
    rewrite ^/service/(.*)$  /html/service/$1$2;
}

映射虚拟目录

location /phpstudy/static/ {
    alias /usr/local/nginx/html/phpstudy/www/static/;
}       

禁止访问所有.开头的隐藏文件设置

location ~* /.* {
deny all;
}

禁止访问以/data开头的文件

location ~ ^/data
{
deny all;
}

禁止访问以.sh,.flv,.mp3为文件后缀名的文件

location ~ .*\.(sh|flv|mp3)$
{
return 403;
}

设置某些类型文件的浏览器缓存时间

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)$
{
expires 1h;
}

给favicon.ico和robots.txt设置过期时间

location ~(favicon.ico) {
log_not_found off;
expires 99d;
break;
}
location ~(robots.txt) {
log_not_found off;
expires 7d;
break;
}

  • 这里为favicon.ico为99天,robots.txt为7天并不记录404错误日志

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

location ^~ /html/scripts/loadhead_1.js {
access_log  off;
root /opt/lampp/htdocs/web;
expires 600;
break;
}

禁止访问path目录

location ^~ /path {
deny all;
}

当访问的文件和目录不存在时,重定向到某个php文件

if( !-e $request_filename )
{
rewrite ^/(.*)$ index.php last;
}

文件和目录不存在的时候重定向

if (!-e $request_filename) {
proxy_pass http://127.0.0.1;
}

如果客户端使用的是IE浏览器,则重定向到/ie目录下

if( $http_user_agent  ~ MSIE)
{
rewrite ^(.*)$ /ie/$1 break;
}

禁止访问多个目录

location ~ ^/(cron|templates)/
{
deny all;
break;
}

文件反盗链并设置过期时间

location ~*^.+\.(jpg|jpeg|gif|png|swf|rar|zip|css|js)$ {
valid_referers none blocked *.linuxidc.com*.linuxidc.net localhost 208.97.167.194;
if ($invalid_referer) {
rewrite ^/ http://img.linuxidc.net/leech.gif;
return 412;
break;
}
access_log  off;
root /opt/lampp/htdocs/web;
expires 3d;
break;
}

  • 这里的return412 为自定义的http状态码,默认为403,方便找出正确的盗链的请求
    “rewrite ^/ http://img.linuxidc.net/leech.gif;”显示一张防盗链图片
    “access_log off;”不记录访问日志,减轻压力
    “expires 3d”所有文件3天的浏览器缓存

只允许固定ip访问网站,并加上密码

root /opt/htdocs/www;
allow  208.97.167.194; 
allow  222.33.1.2; 
allow  231.152.49.4;
deny  all;
auth_basic “C1G_ADMIN”;
auth_basic_user_file htpasswd;

将多级目录下的文件转成一个文件,增强seo效果




rewrite^/job-([0-9]+)-([0-9]+)-([0-9]+)\.html$ /job/$1/$2/jobshow_$3.html last;
  • /job-123-456-789.html 指向/job/123/456/789.html

域名跳转

server
{
listen      80;
server_name  jump.linuxidc.com;
index index.html index.htm index.php;
root  /opt/lampp/htdocs/www;
rewrite ^/ http://www.linuxidc.com/;
access_log  off;
}

多域名转向

server_name  www.linuxidc.com www.linuxidc.net;
index index.html index.htm index.php;
root  /opt/lampp/htdocs;
if ($host ~ "linuxidc\.net") {
rewrite ^(.*) http://www.linuxidc.com$1permanent;
}
server
    {
        listen       80;
        server_name www.test.com www.test.com.cn;
        index index.html index.htm index.php;
        root /home/wwwroot;

        if ($host = 'www.test.com.cn' ) {
                rewrite ^/(.*)$ http://www.test.com/$1 permanent;
        }
        if ($host = 'test.com' ) {
                rewrite ^/(.*)$ http://www.test.com/$1 permanent;
        }
        if ($host = 'test.com.cn' ) {
                rewrite ^/(.*)$ http://www.test.com/$1 permanent;
        }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值