nginx 相關配置

一:下載&安裝
下載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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章