设为首页收藏本站

就爱编程论坛

 找回密码
 注册

人人连接登陆

无需注册,直接登录

用新浪微博连接

一步搞定

QQ登录

只需一步,快速开始

查看: 274|回复: 0
打印 上一主题 下一主题

Nginx Location 语法,与简单配置 [复制链接]

Rank: 9Rank: 9Rank: 9

  • TA的每日心情
    无聊
    2025-5-27 03:37:20
  • 签到天数: 366 天

    [LV.9]以坛为家II

    论坛先锋 学习至圣 荣誉成员 论坛元老 活跃之星 终极领袖

    我玩的应用:

    跳转到指定楼层
    楼主
    发表于 2012-3-30 22:52:13 |只看该作者 |倒序浏览
    一、介绍Nginx是俄罗斯人编写的十分轻量级的HTTP服务器,Nginx,它的发音为“engine X”, 是一个高性能的HTTP和反向代理服务器,同时也是一个IMAP/POP3/SMTP 代理服务器.
    二、Location语法语法:location [=|~|~*|^~] /uri/ { … }
    注:
    1、~   为区分大小写匹配
    2、~* 为不区分大小写匹配
    3、!~和!~*分别为区分大小写不匹配及不区分大小写
    不匹配
    示例一:
    location  / {
    }
    匹配任何查询,因为所有请求都以 / 开头。但是正则表达式规则将被优先和查询匹配。
    示例二:
    location =/ {}
    仅仅匹配/

    示例三:
    location ~* \.(gif|jpg|jpeg)$ {
    rewrite \.(gif|jpg)$ /logo.png;

    注:不区分大小写匹配任何以gif,jpg,jpeg结尾的文件

    三、ReWrite语法
    last - 基本上都用这个Flag。
    break - 中止Rewirte,不在继续匹配
    redirect - 返回临时重定向的HTTP状态302
    permanent - 返回永久重定向的HTTP状态301

    1、下面是可以用来判断的表达式:
    -f和!-f用来判断是否存在文件
    -d和!-d用来判断是否存在目录
    -e和!-e用来判断是否存在文件或目录
    -x和!-x用来判断文件是否可执行
    2、下面是可以用作判断的全局变量
    例:http://localhost:88/test1/test2/test.php
    $host:localhost
    $server_port:88
    $request_uri:
    http://localhost:88/test1/test2/test.php
    $document_uri:/test1/test2/test.php
    $document_root:D:\nginx/html
    $request_filename:D:\nginx/html/test1/test2/test.php

    四、Redirect语法
        server {
        listen 80;
        server_name start.igrow.cn;
        index index.html index.php;
        root html;
        if ($http_host !~ "^www\.itlearner\.com$ {
             rewrite ^(.*)
    http://www.itlearner.com$1 redirect;
        }
        }

    五、防盗链location ~* \.(gif|jpg|swf)$ {
      valid_referers none blocked start.igrow.cn sta.igrow.cn;
      if ($invalid_referer) {
      rewrite ^/
    http://$host/logo.png;
      }
    }

    六、根据文件类型设置过期时间
    location ~* \.(js|css|jpg|jpeg|gif|png|swf)$ {
    if (-f $request_filename) {
       expires    1h;
       break;
    }
    }

    七、禁止访问某个目录
    location ~* \.(txt|doc)${
       
      root /data/www/wwwroot/linuxtone/test;
       
    deny all;
    }

    ++ 一些可用的全局变量
    $args
    $content_length
    $content_type
    $document_root
    $document_uri
    $host
    $http_user_agent
    $http_cookie
    $limit_rate
    $request_body_file
    $request_method
    $remote_addr
    $remote_port
    $remote_user
    $request_filename
    $request_uri
    $query_string
    $scheme
    $server_protocol
    $server_addr
    $server_name
    $server_port
    $uri


    该贴已经同步到 admin的微博
    分享到: QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    分享分享0 收藏收藏0 支持支持0 反对反对0 分享到人人 转发到微博
    [img=http://mail.qq.com/cgi-bin/qm_share?t=qm_mailme&email=fRUcHhYWGAQ9GxIFEBwUEVMeEhA]http://rescdn.qqmail.com/zh_CN/htmledition/images/function/qm_open/ico_mailme_02.png[/img]

    使用道具 举报

    您需要登录后才可以回帖 登录 | 注册 人人连接登陆

    晴云孤魂's Blog|就爱编程搜帖|手机版|Archiver|就爱编程论坛     

    GMT+8, 2025-7-2 15:16 , Processed in 0.097561 second(s), 27 queries .

    Powered by Discuz! X2

    © 2001-2011 Comsenz Inc.

    回顶部