Nginx 配置

content_by_lua ‘’;
set_by_lua $c "return ngx.var.a + ngx.var.b";
rewrite_by_lua "ngx.var.a = ngx.var.a + 1";
access_by_lua '
            if ngx.var.remote_addr == "127.0.0.1" then
                return
            end

            ngx.exit(403)
        ';

  

虚拟主机

server_name  .a.org; #可以用正则表达式 *.a.org 跟a.org 可以合并为 .a.org 多个域名并排,空格分隔

显示目录或文件

autoindex on;
autoindex_exact_size on; #显示文件大小
autoindex_localtime on; #显示最后修改时间

 压缩返回

gzip on;
gzip_types text/plain application/x-javascript text/css application/xml; #指定类型文件压缩
gzip_min_length 1000; 
gzip_buffers 4 8k;

默认读取的文件

index index.php index.html index.htm;

设置网站的根目录

root /var/nginx/avdata/public_html; #确保有目录权限x

默认请求处理

location / { #默认的处理
    try_files $uri $uri/ index.php?$args;
}

默认处理的匹配方式 配置的$uri 即不包含参数以/开头的请求地址

~ 区分大小写正则匹配

~* 不区分大小写正则匹配

^~ 如果匹配之后将停止往下检查

= 完全相等

例:

 location ~ \.php$ {#匹配.php的请求
  
 try_files $uri $uri/ index.php?$args;#$args显示传递GET参数
       include fastcgi.conf;
       fastcgi_pass   127.0.0.1:9000;
       fastcgi_index  index.php; }
location = index.php {#匹配index.php ,非页面上输入index.php的请求.页面上index.php的请求为/index.php
            include fastcgi_params;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;#$fastcgi_script_name默认跟$uri相同
            fastcgi_index index.php;
}

静态文件超时

location ~* \.(jpg|jpeg|gif|png|js|css)$ {#匹配指定结尾文件
            access_log off;#错误不记录日志
            expires 30d;#客户端缓存30天
}

通过规则:

{}里面的设置不影响外部的设置,

location / {
    root /var/nginx/a/public_html/hi;
}

外部的设置影响{}里的设置,所以可以在虚拟主机的外部定义root,全局共用

IP监听

listen       80; #IP:端口 或者直接端口,监听全部IP

简单重写代替

# 最后一个是处理方式或者是处理地址,之前的为文件或目录
# 找不到对应文件或目录将用最后一个作为请求代替
# $uri/ 以/结尾表示目录
try_files $uri $uri/ index.php;#文件或目录找不到的时候用index.php代替$uri

别名

location /i/ {
    alias /good/; #但$uri为/i/*规则的时候,$uri将被修改为/good/* 不能用于正则表达式内的location
}

禁止访问

location ~ \.db$ {#符合.db 结尾的文件禁止访问
            deny  all; 
}

指定IP访问

location ~ \.db$ {#本机可访问.db 结尾的文件
    allow 127.0.0.1;
}

其他权限:

location /hello {
        allow 127.0.0.1;
        deny all;
        echo "hello world";
    }

 

传输速度限制

#超过传输指定文件后限制速度
limit_rate_after 1m; limit_rate 100k;

404错误记录

log_not_found off;# 404错误不记录

 定义静态404错误

        location / {//找不到文件定义状态吗
            try_files $uri $uri/  =404;
        }   
location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_intercept_errors on;//让错误显示到前端
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }   
        error_page   404  /404.html;//自定义404

规则:

~   为区分大小写匹配
~* 为不区分大小写匹配
!~和!~*分别为区分大小写不匹配及不区分大小写不匹配
-f和!-f用来判断是否存在文件
-d和!-d用来判断是否存在目录
-e和!-e用来判断是否存在文件或目录
-x和!-x用来判断文件是否可执行
#ReWrite语法
last - 基本上都用这个Flag。
break - 中止Rewirte,不在继续匹配
redirect - 返回临时重定向的HTTP状态302
permanent - 返回永久重定向的HTTP状态301

 

一些核心及内置变量:http://nginx.org/cn/docs/http/ngx_http_core_module.html

附属:

一般Nginx 变量值容器的生命期是与当前请求相关联的,有特殊的。
变量是声明的,非过程的

设置变量

set $a aaa;

输出变量

echo $a;
echo "a $a;"

正常下,$都将被转义 所以 通过以下办法定义有$字符的变量

geo $do {
default "$";
}

执行某个地址:

echo_exec /bar;
echo_location /bar "a=1&b=2";

获得参数:

$arg_*

输出指令不能有多条
配合echo的前后

echo_before_body "before...";
echo_after_body "after...";

处理顺序:

rewrite
access
content (echo proxy_pass 等)
index #(地址不为/弃权处理)
autoindex #(地址不为/弃权处理)
ngx_static
find-config
。。。
try-files /file1 /file2 =403 #(挨个尝试文件是否存在)

LUA 调用:

content_by_lua ‘ ngx.exit(403)’;
set_by_lua $c "return ngx.var.a + ngx.var.b";
rewrite_by_lua "ngx.var.a = ngx.var.a + 1";
access_by_lua '
            if ngx.var.remote_addr == "127.0.0.1" then
                return
            end

            ngx.exit(403)
        ';

示例:

    server {
        listen       80;
        server_name xx.com www.xx.com;
        gzip on;
        gzip_types text/plain application/x-javascript text/css application/xml;
        gzip_min_length 1000;
        gzip_buffers 4 8k;
        root    /var/www/xx/public_html/;
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        location / {
            index  index.php index.html index.htm;
            try_files $uri $uri/ index.php?$args;
        }
        location ~ \.(ico|gif|bmp|jpg|jpeg|png|swf|js|css) {
            expires 7d;
            try_files $uri $uri/ index.php?$args;
        }
        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param KOHANA_ENV "TESTING";
            fastcgi_param  PHP_VALUE  "open_basedir=$document_root:/tmp/";
            include        fastcgi_params;
        }
        location = index.php {
            include fastcgi_params;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_param SCRIPT_FILENAME $document_root/index.php;
            fastcgi_param KOHANA_ENV "TESTING";
            fastcgi_param  PHP_VALUE  "open_basedir=$document_root:/tmp/";
            fastcgi_index index.php;
        }
    }

一般情况下记得把PHP某些函数禁用

disable_functions = exec,passthru,shell_exec,system,proc_open,popen,parse_ini_file,show_source,dl

disable_classes = perl

posted @ 2013-01-07 16:19  liushan  阅读(385)  评论(0编辑  收藏  举报