Nginx(發音同 engine x)是一款由俄羅斯程式設計師 Igor Sysoev 所開發輕量級的網頁伺服器、反向代理伺服器以及電子郵件(IMAP/POP3)代理伺服器。起初是供俄國大型的入口網站及搜尋引擎 Rambler(俄語:Рамблер)使用。此軟體 BSD-like 協定下發行,可以在 UNIX、GNU/Linux、BSD、Mac OS X、Solaris,以及Microsoft Windows 等作業系統中執行。
下載相關模組,如何編譯?請參閱源碼包中的 README 檔案。
Module ngx_http_hls_module
nginx-rtmp-module
官方的教學文件
HTTP Live Streaming(縮寫是 HLS)是一個由蘋果公司提出的基於 HTTP 的流媒體 網路傳輸協議。是蘋果公司 QuickTime X 和 iPhone 軟體系統的一部分。它的工作原理是把整個流分成一個個小的基於 HTTP 的文件來下載,每次只下載一些。當媒體流正在播放時,客戶端可以選擇從許多不同的備用源中以不同的速率下載同樣的資源,允許流媒體會話適應不同的數據速率。在開始一個流媒體會話時,客戶端會下載一個包含元數據的extended M3U (m3u8) playlist 文件,用於尋找可用的媒體流。
HLS 只請求基本的 HTTP 報文,與實時傳輸協議(RTP)不同,HLS 可以穿過任何允許HTTP 數據通過的防火牆或者代理伺服器。它也很容易使用內容分髮網絡來傳輸媒體流。
Nginx 搭建 HLS 流媒體服務器
第一種方案:ffmpeg+nginx
新的 ffmpeg 已經支持 HLS。
點播:
如上述的描述生成 HLS 分片的播放清單:
ffmpeg -i <媒體文件> -c:v libx264 -c:a copy -f hls /usr/local/nginx/html/test.m3u8
※ 請注意目錄權限問題,有時會造成客戶端無法訪問。
直播:
ffmpeg -i udp://@:1234 -c:v libx264 -c:a copy -f hls /usr/local/nginx/html/test.m3u8
建立 web 服務器:
編輯 /usr/local/nginx/conf/nginx.conf 使用預設配置就可以。
server {listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
}
啟動 nginx。
客戶端訪問:http://IP/test.m3u8
在 windows 上可以用 vlc 播放。
第二個方案,用 nginx-rtmp-module
rtmp {
server {
listen 1935;
chunk_size 4000;
#HLS
# For HLS to work please create a directory in tmpfs (/tmp/app here)
# for the fragments. The directory contents is served via HTTP (see
# http{} section in config)
# Incoming stream must be in H264/AAC. For iPhones use baseline H264
# profile (see ffmpeg example).
# This example creates RTMP stream from movie ready for HLS:
# ffmpeg -loglevel verbose -re -i movie.avi -vcodec libx264
# -vprofile baseline -acodec libmp3lame -ar 44100 -ac 1
# -f flv rtmp://localhost:1935/hls/movie
#
# If you need to transcode live stream use 'exec' feature.
#
application hls {
live on;
hls on;
hls_path /tmp/app;
hls_fragment 5s;
}
}
}
http {
server {
listen 80;
location /hls {
# Serve HLS fragments
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
alias /tmp/app;
expires -1;
}
}
事實上整個 HLS 的架設,Nginx 僅扮演 HTTP 封包解析與 GET 檔案下載的工作而已,ffmpeg 負責將媒體檔切割成多個小檔,並組件播放清單 m3u (也可以用 m3u8-segmenter 或 httpsegmenter 來切,但僅限 TS 檔),換成 Apple 所規定的文件。所以需要 AAC,mp3,x264 庫的支持。至於 RTMP 的轉發也是呼叫外部的 ffmpeg 來完成,Nginx 只是作 rtmp 封包轉發的工作而已。
設定檔說明
重新導向
server
{
listen 80;
server_name linuxtone.org abc.linuxtone.org;
index index.html index.php;
root /data/www/wwwroot;
if ($http_host !~ "www\.linxtone\.org") {
rewrite ^(.*) http://www.linuxtone.org$1 redirect;
}
……………………
}
目錄自動加斜線,解決IE瀏覽器不識別
if (-d $request_filename){
rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
}
Nginx 防盜鏈
#Preventing hot linking of images and other file types
location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip)$ {
valid_referers none blocked server_names *.linuxtone.org http://localhost baidu.com;
if ($invalid_referer) {
rewrite ^/ http://www.linuxtone.org/images/default/logo.gif;
# return 403;
}
}
過期 (expires)
第一種方法:根據文件類型 expires
# Add expires header for static content
location ~* \.(js|css|jpg|jpeg|gif|png|swf)$ {
if (-f $request_filename) {
root /data/www/wwwroot/bbs;
expires 1d;
break;
}
第二種方法:根據判斷某個目錄
# serve static files
location ~ ^/(images|javascript|js|css|flash|media|static)/ {
root /data/www/wwwroot/down;
expires 30d;
}
訪問控制
#/usr/local/apache2/bin/htpasswd -c /usr/local/nginx/conf/htpasswd/tongji linuxtone 創建用戶
location ~ ^/(tongji)/ {
root /data/www/wwwroot/count;
auth_basic 「LT-COUNT-TongJi」;
auth_basic_user_file /usr/local/nginx/conf/htpasswd/tongji;
}
禁止訪問某類型的文件.
方法一:
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
下載限制並發和速率
limit_zone one $binary_remote_addr 10m;
server
{
listen 80;
server_name down.linuxotne.org;
index index.html index.htm index.php;
root /data/www/wwwroot/down;
#Zone limit
location / {
limit_conn one 1;
limit_rate 20k;
}
……….
}
實現 Apache 一樣目錄列表
location / {
autoindex on;
}
如何不記錄部分日志
location ~ .*\.(js|jpg|JPG|jpeg|JPEG|css|bmp|gif|GIF)$
{
access_log off;
}
虛擬主機配置
http {
server {
listen 80;
server_name www.domain1.com;
access_log logs/domain1.access.log main;
location / {
index index.html;
root /var/www/domain1.com/htdocs;
}
}
server {
listen 80;
server_name www.domain2.com;
access_log logs/domain2.access.log main;
location / {
index index.html;
root /var/www/domain2.com/htdocs;
}
}
}
日志處理
#contab -e
59 23 * * * /usr/local/sbin/logcron.sh /dev/null 2>&1
# cat /usr/local/sbin/logcron.sh
#!/bin/bash
log_dir=」/data/logs」
time=`date +%Y%m%d`
/bin/mv ${log_dir}/access_linuxtone.org.log ${log_dir}/access_count.linuxtone.org.$time.log
kill -USR1 `cat /var/run/nginx.pid`
++Nginx 如何不記錄部分日志
location ~ .*\.(js|jpg|JPG|jpeg|JPEG|css|bmp|gif|GIF)$
{
access_log off;
}
Cache 服務配置
如果需要將文件緩存到本地,則需要增加如下幾個子參數
proxy_store on;
proxy_store_access user:rw group:rw all:rw;
proxy_temp_path 緩存目錄;其中,
proxy_store on用來啟用緩存到本地的功能,
proxy_temp_path用來指定緩存在哪個目錄下,如:proxy_temp_path html;
#在經過上一步配置之後,雖然文件被緩存到了本地磁盤上,但每次請求仍會向遠端拉取文件,為了避免去遠端拉取文件,必須修改proxy_pass:代碼:
if ( !-e $request_filename) {
proxy_pass http://mysvr;
}
#即改成有條件地去執行proxy_pass,這個條件就是當請求的文件在本地的proxy_temp_path指定的目錄下不存在時,再向後端拉取。
負載均衡
1. Nginx 基礎知識
nginx的upstream目前支持4種方式的分配
1)、輪詢(默認)
每個請求按時間順序逐一分配到不同的後端服務器,如果後端服務器down掉,能自動剔除。
2)、weight
指定輪詢幾率,weight和訪問比率成正比,用於後端服務器性能不均的情況。
2)、ip_hash
每個請求按訪問ip的hash結果分配,這樣每個訪客固定訪問一個後端服務器,可以解決session的問題。
3)、fair(第三方)
按後端服務器的響應時間來分配請求,響應時間短的優先分配。
4)、url_hash(第三方)
負載均衡
實例1代碼:
upstream bbs.linuxtone.org {#定義負載均衡設備的Ip及設備狀態
server 127.0.0.1:9090 down;
server 127.0.0.1:8080 weight=2;
server 127.0.0.1:6060;
server 127.0.0.1:7070 backup;
}
在需要使用負載均衡的server中增加代碼:
proxy_pass http://bbs.linuxtone.org/;
每個設備的狀態設置為:代碼:
1.down 表示單前的server暫時不參與負載
2.weight 默認為1.weight越大,負載的權重就越大。
3.max_fails :允許請求失敗的次數默認為1.當超過最大次數時,返回proxy_next_upstream 模塊定義的錯誤
4.fail_timeout:max_fails次失敗後,暫停的時間。
5.backup: 其它所有的非backup機器down或者忙的時候,請求backup機器。所以這台機器壓力會最輕。nginx支持同時設置多組的負載均衡,用來給不用的server來使用。
client_body_in_file_only 設置為On 可以講client post過來的數據記錄到文件中用來做debug
client_body_temp_path 設置記錄文件的目錄 可以設置最多3層目錄
location 對URL進行匹配.可以進行重定向或者進行新的代理 負載均衡
++Nginx 負載均衡實例 2
按訪問url的hash結果來分配請求,使每個url定向到同一個後端服務器,後端服務器為緩存時比較有效,也可以用作提高Squid緩存命中率.
簡單的負載均等實例:
#vi nginx.conf //nginx主配置文件核心配置代碼:
……….
#loadblance my.linuxtone.org
upstream my.linuxtone.org {
ip_hash;
server 127.0.0.1:8080;
server 192.168.169.136:8080;
server 219.101.75.138:8080;
server 192.168.169.117;
server 192.168.169.118;
server 192.168.169.119;
}
…………..
include vhosts/linuxtone_lb.conf;
………
#vi proxy.conf
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 50m;
client_body_buffer_size 256k;
proxy_connect_timeout 30;
proxy_send_timeout 30;
proxy_read_timeout 60;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
proxy_max_temp_file_size 128m;
proxy_store on;
proxy_store_access user:rw group:rw all:r;
#nginx cache
client_body_temp_path /data/nginx_cache/client_body 1 2;
proxy_temp_path /data/nginx_cache/proxy_temp 1 2;#vi linuxtone_lb.conf
server
{
listen 80;
server_name my.linuxtone.org;
index index.php;
root /data/www/wwwroot/mylinuxtone;
if (-f $request_filename) {
break;
}
if (-f $request_filename/index.php) {
rewrite (.*) $1/index.php break;
}
error_page 403 http://my.linuxtone.org/member.php?m=user&a=login;
location / {
if ( !-e $request_filename) {
proxy_pass http://my.linuxtone.org;
break;
}
include /usr/local/nginx/conf/proxy.conf;
}
}
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 「LTWS/」 NGINX_VERSION
#define NGINX_VAR 「NGINX」
#define NGX_OLDPID_EXT 「.oldbin」
#endif
# curl -I my.linuxtone.org
HTTP/1.1 200 OK
Server: LTWS/1.3
Date: Mon, 24 Nov 2008 02:42:51 GMT
Content-Type: text/html; charset=gbk
Transfer-Encoding: chunked
Connection: keep-alive
naginx 服務簡單控制腳本
見附件.nagixd
其它說明
++nginx幾個參數
-c 為 Nginx 指定一個配置文件,來代替缺省的。
-t 不運行,而僅僅測試配置文件。nginx 將檢查配置文件的語法的正確性,並嘗試打開配置文件中所引用到的文件。
-v 顯示 nginx 的版本。
-V 顯示 nginx 的版本,編譯器版本和配置參數。
++ 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
限會員,要發表迴響,請先登入