nginx 相关配置

来源:互联网 发布:桃谷绘里香推荐 知乎 编辑:程序博客网 时间:2024/04/30 03:26

一:下载&安装
下载nginx_mod_h264_streaming-2.2.5,nginx-accesskey-2.0.3
http://h264.code-shop.com/trac/wiki/Mod-H264-Streaming-Introduction-Version2
http://flash.9ria.com/thread-34176-1-1.html
编译安装
./configure --prefix=/opt/test/nginx8 --with-http_ssl_module  --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_stub_status_module  --with-http_gzip_static_module --with-poll_module --add-module=/opt/test/nginx_mod_h264_streaming-2.2.5 --add-module=/opt/test/nginx-accesskey-2.0.3

二:Nginx 配置flv3,mp4,添加了nginx的accessKey Module 实现防盗链

      参考网址:http://flash.9ria.com/thread-34176-1-1.html
location ~* /.flv$ {
  flv;
  accesskey             on;
  accesskey_hashmethod  md5;
  accesskey_arg         "key";
  accesskey_signature   "$remote_addr";
}

location ~* /.mp4$ {
  mp4;
}

三: Nginx身份验证(访问控制)

#mkdir -p /opt/test/nginx8/auth/
#/usr/local/apache2/bin/htpasswd -c /opt/test/nginx8/auth/authfile username #添加用户名为username
location ~^/(auth)/ {
    #root   html;
    auth_basic "Auth";
    auth_basic_user_file /opt/test/nginx8/auth/authfile;
    #deny all;
    #expires  360d;
    #return 403;
    valid_referers none blocked server_names xxx.com *.xxx.com;
    if ($invalid_referer) {
        rewrite ^/ http://192.168.12.149:8089/test01/test.html;
    #     return 403;
    }
}

四:目录自动加斜线,解决IE浏览器不识别
if (-d $request_filename){
rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
}

五: Nginx 防盗链

nginx有个模块叫做 ngx_http_referer_module,通过这个模块,可以很方便的做防盗链设置
一般常用的方法是在 server或者location段中加入
valid_referers   none blocked www.mydomain.com mydomain.com;
其中 none 表示 空的来路,也就是直接访问,比如直接在浏览器打开一个图片
blocked 表示被防火墙标记过的来路
具体配置示例如下:
location ~* /.(gif|jpg|png|swf|flv)$ {
    valid_referers none blocked www.xxx.com www.xx.net;
    if ($invalid_referer) {
        rewrite ^/ http://www.yyy.com/403.html;
        #return 404;
    }
}

六 Nginx expires 配置

第一种方法:根据文件类型expires
location ~* /.(js|css|jpg|jpeg|gif|png|swf)$ {
    if (-f $request_filename) {
        root /data/www/;
        expires 1d;
        break;
    }
}
第二种方法:根据判断某个目录
location ~ ^/(images|javascript|js|css|flash|media|static)/ {
    root /data/down;
    expires 30d;
}


七:Nginx 禁止访问某类型的文件.
方法一:
location ~* /.(txt|doc)$ {
    if (-f $request_filename) {
        root /data/www/wwwroot/linuxtone/test;
        break;
    }
}
方法二
location ~* /.(txt|doc)${
    root /data/www/wwwroot/linuxtone/test;
    deny all;
}

禁止访问某个目录
location ~ ^/(WEB-INF)/ {
deny all;
}

八:使用ngx_http_access_module限制ip访问
location / {
deny 192.168.1.1;
allow 192.168.1.0/24;
allow 10.1.1.0/16;
deny all;
}
详细参见wiki: http://wiki.codemongers.com/NginxHttpAccessModule#allow

九: Nginx 下载限制并发和速率
limit_zone one $binary_remote_addr 10m;
server
{
listen 80;
server_name down.xxx.org;
index index.html index.htm index.php;
root /data/www/wwwroot/down;
#Zone limit
location / {
limit_conn one 1;
limit_rate 20k;
}
……….
}

十: Nginx 实现Apache一样目录列表
location / {
autoindex on;
}

十一:Nginx 优化
1).减小nginx编译后的文件大小 (Reduce file size of nginx)
默认的nginx编译选项里居然是用debug模式(-g)的(debug模式会插入很多跟踪和ASSERT之类),编译以后一个nginx有好几兆。去掉nginx的debug模式编译,编译以后只有几百K
在 auto/cc/gcc,最后几行有:
# debug
CFLAGS=”$CFLAGS -g”
注释掉或删掉这几行,重新编译即可。
2).修改Nginx的header伪装服务器
代码:
# vi src/core/nginx.h
#ifndef _NGINX_H_INCLUDED_
#define _NGINX_H_INCLUDED_
#define NGINX_VERSION “1.3″
#define NGINX_VER “XXWS/” NGINX_VERSION
#define NGINX_VAR “NGINX”
#define NGX_OLDPID_EXT “.oldbin”
#endif
# curl -I my.linuxtone.org
HTTP/1.1 200 OK
Server: XXWS/1.3
Date: Mon, 24 Nov 2008 02:42:51 GMT
Content-Type: text/html; charset=gbk
Transfer-Encoding: chunked
Connection: keep-alive


2) 其它说明
++nginx几个参数
-c 为 Nginx 指定一个配置文件,来代替缺省的。
-t 不运行,而仅仅测试配置文件。nginx 将检查配置文件的语法的正确性,并尝试打开配置文件中所引用到的文件。
-v 显示 nginx 的版本。
-V 显示 nginx 的版本,编译器版本和配置参数。

3) 基本语法
   Nginx Location
location [=|~|~*|^~] /uri/ { … }
= 严格匹配。如果这个查询匹配,那么将停止搜索并立即处理此请求。
~ 为区分大小写匹配
~* 为不区分大小写匹配
!~和!~*分别为区分大小写不匹配及不区分大小写不匹配
^~ 如果把这个前缀用于一个常规字符串,那么告诉nginx 如果路径匹配那么不测试正则表达式。

例:
location = / { # 只匹配 / 查询。
location / { # 匹配任何查询,因为所有请求都已 / 开头。但正则表达式规则和长的块规则将被优先和查询匹配。
location ^~ /images/ { # 匹配任何已 /images/ 开头的任何查询并且停止搜索。任何正则表达式将不会被测试。
location ~* /.(gif|jpg|jpeg)$ { # 匹配任何已 gif、jpg 或 jpeg 结尾的请求。

++ 文件及目录匹配
* -f和!-f用来判断是否存在文件
* -d和!-d用来判断是否存在目录
* -e和!-e用来判断是否存在文件或目录
* -x和!-x用来判断文件是否可执行
++ 一些可用的全局变量
$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

原创粉丝点击