1.ngnix介紹
ngnix www服務軟件 俄羅斯人開發 開源 性能很高 本身是一款靜態WWW軟件 靜態小文件 支持高并發,同時占用的資源很少。
2.最大特點
(1)靜態小文件(1M),支持高并發,同時占用的資源很少。3W并發,10個進程,內存150M.
(2)nginx服務端支持的平臺:unix、linux、windows都可以.
(3)配置簡單/靈活、輕量
(4)高并發(靜態小文件),靜態幾萬的并發
(5)占用資源少。2W并發 開10個線程服務,內存消耗幾百M
(6)功能種類比較多,每一個功能都不是特別強
(7)支持epoll模型。使得nginx可以支持高并發!apache select模型
(8)nginx可以配合動態服務
(9)利用nginx可以對IP限速,可以限制連接數。
它所具備的其他WWW服務特性如下:
支持基于名字、端口以及IP的多虛擬機主機站點;
支持rewrite模塊,支持URI重寫及正則表達式匹配;
支持基于客戶端IP地址和HTTP基本認證的訪問控制;
支持http響應速率限制;
支持同一IP地址的并發連接或請求數限制。
3.Nginx的應用場合:
(1)提供靜態服務(圖片,視頻服務),另一個lighttpd。并發:幾萬并發。
html,js,css,.flv,jpg,gif等。類似lighttpd。
(2)提供動態服務,nginx+fastcgi的方式運行php,jsp。動態并發500-1500.
apache+php,lighttpd+fcgi php
(3)提供反向代理(proxy)服務,或者稱為負載均衡。日PV2000W以下,都可以直接用nginx做代理。
haproxy F5 A10
(4)緩存服務.類似Squid,varnish
4.nginx主要應用場景:
1.web服務器(首選)
靜態文件,nginx首選
動態文件,配合fastcgi支持php
2.反向代理(負載均衡),1000-2000W PV,并發6000
3.緩存(不是很多)
注:nginx之所以超過apache的原因是nginx采用epoll模型,而apache采用的是select模型。
5.nginx虛擬主機:
一個server標簽就是一個虛擬主機。
1.基于域名的虛擬主機.通過域名來區分虛擬主機==>應用:外部網站
2.基于端口的虛擬主機.通過端口來區分虛擬主機==>應用:公司內部網站,網站的后臺
3.基于IP的虛擬主機。幾乎不用。不支持ifxconfig別名,配置文件可以。
6.nginx的安裝:
(1)HTTP rewrite module requires the PCRE library
PCRE == perl 兼容表達式
操作命令:
yum install pcre pcre-devel -y
rpm -qa pcre pcre-devel
(2)SSL module requires the OPEN SSL library
操作命令:
yum install openssl-devel -y
rpm -qa openssl-devel
(3)install nginx
tar -zxf ngnix-1.6.2
cd ngnix-1.6.2
. /configure --prefix=/application/ngnix-1.6.2 --user=ngnix --group=ngnix --with-http_ssl_module --with-http_stub_status_module
make && make install
cd ../
ln -s /application/nginx-1.6.2/ /application/nginx
(4)啟動nginx
/application/nginx/sbin/nginx
(5)驗證是否安裝成功
netstat -lntup|grep 80
或
lsof -i :80
7.nginx配置詳解
#user? nobody;
worker_processes? 1;====> worker進程的數量
#error_log? logs/error.log;
#error_log? logs/error.log? notice;
#error_log? logs/error.log? info;
#pid? ? ? ? logs/nginx.pid;
events {====>事件區塊開始
worker_connections? 1024;====>每個worker進程支持的最大連接數
}
http {====>http區塊開始
include? ? ? mime.types;====>nginx支持的媒體類型庫文件包含
default_type? application/octet-stream;====>默認的媒體類型
#log_format? main? '$remote_addr - $remote_user [$time_local] "$request" '
#? ? ? ? ? ? ? ? ? '$status $body_bytes_sent "$http_referer" '
#? ? ? ? ? ? ? ? ? '"$http_user_agent" "$http_x_forwarded_for"';
#access_log? logs/access.log? main;
sendfile? ? ? ? on;====>開啟高級傳輸模式
#tcp_nopush? ? on;
#keepalive_timeout? 0;
keepalive_timeout? 65; ====>連接超時時間
#gzip? on;
server {====>第一個server區塊開始 表示一個虛擬主機
listen? ? ? 80;
server_name? localhost;
#charset koi8-r;
#access_log? logs/host.access.log? main;
location / {
root? html; ====>站點根目錄
index? index.html index.htm;====>默認首頁文件
}?
#error_page? 404? ? ? ? ? ? ? /404.html;
# redirect server error pages to the static page /50x.html
#
error_page? 500 502 503 504? /50x.html;
location = /50x.html {
root? html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
#? ? proxy_pass? http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
#? ? root? ? ? ? ? html;
#? ? fastcgi_pass? 127.0.0.1:9000;
#? ? fastcgi_index? index.php;
#? ? fastcgi_param? SCRIPT_FILENAME? /scripts$fastcgi_script_name;
#? ? include? ? ? ? fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
#? ? deny? all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
#? ? listen? ? ? 8000;
#? ? listen? ? ? somename:8080;
#? ? server_name? somename? alias? another.alias;
#? ? location / {
#? ? ? ? root? html;
#? ? ? ? index? index.html index.htm;
#? ? }
#}
# HTTPS server
#
#server {
#? ? listen? ? ? 443 ssl;
#? ? server_name? localhost;
#? ? ssl_certificate? ? ? cert.pem;
#? ? ssl_certificate_key? cert.key;
#? ? ssl_session_cache? ? shared:SSL:1m;
#? ? ssl_session_timeout? 5m;
#? ? ssl_ciphers? HIGH:!aNULL:!MD5;
#? ? ssl_prefer_server_ciphers? on;
#? ? location / {
#? ? ? ? root? html;
#? ? ? ? index? index.html index.htm;
#? ? }
#}
}
8.核心模塊(Core Modules):
主模塊(Main Module):
配置和服務器全局有關的一些參數,比如錯誤日志、進程、權限等
user
worker_processes
error_log
syntax: error_log file [debug|info|notice|warn|error|crit]
default: ${prefix}/logs/error.log
指定保存服務器(和 fastcgi)錯誤的日志文件。
在不同的部分,錯誤等級的缺省值會不一樣,為:
1、在主要部分 – error
2、在 HTTP 部分 – crit
3、在server部分 – crit
在Nginx中獨有的而lighttpd卻拒絕實現的特性是,每個虛擬主機可以有各自
分離的錯誤日志。在 server {} 中添加本指令即可。
log_not_found
lock_file
pid
include
syntax: include file / *
default: none
你可以包含任何配置文件,基于你想要達到的目的。
該指令還支持文件名通配符:
include vhosts/*.conf
注意, 0.6.7 以前,相對路徑是相對于 configure 編
譯時的 –prefix= 指令指定的目錄,缺省為 /usr/local/nginx。
如果編譯時沒有指定,則使用絕對路徑。
0.6.7 以后,相對路徑是相對于 nginx 配置文件 nginx.conf 所在的目錄,
而不是 nginx 的 prefix 目錄了。
事件模塊(Events Module):
配置 IO 事件接口的相關參數,通常就是 worker_connections 這個參數。
use
syntax: use [kqueue|rtsig|epoll|/dev/poll|select|poll|eventport]
default:
通常 nginx 在編譯時,會根據平臺自動選擇最優的 Events 接口。
除非特殊情況下,編譯時指定了多個事件接口,則可以使用該指令來指定
特定的接口。
worker_connections
syntax: worker_connections number
default:
根據主模塊中的 worker_processes 指令和該指令可計算最大的客戶端連接數:
max_clients = worker_processes * worker_connections
不過,在做反向代理時, max_clients 變成了:
max_clients = worker_processes * worker_connections/4
因為,瀏覽器通常默認打開兩個與服務器的連接, 而 nginx 使用同一個文件描述符池中的描述符來連接后端。
HTTP 模塊
HTTP 核心模塊(Http Core Module):
控制 Nginx HTTP 處理過程的核心特性。
alias
default_type
syntax: default_type MIME-type
default: default_type text/plain
context: http, server,location
指定在標準 MIME 映射中沒有指定的文件的默認 MIME 類型。
error_page
syntax: error_page code [code…][=|=answer-code] uri
default: no
context: http, server, location, if in location
指定當錯誤出現時需要顯示的 URI 。
而且,還可以改變應答的代碼為另外一種代碼,比如:
error_page 404 =200 /.empty.gif;
index
keepalive_timeout
syntax: keepalive_timeout [time]
default: keepalive_timeout 75
context: http, server, location
指定 keep_alive 連接的客戶端的超時時間。
keepalive_requests
limit_except
limit_rate
listen
syntax: listen address:port [default [backlog=num|rcvbuf=size|sndbuf=size|accept_filter=filter|deferred|bind|ssl]
default: listen 80
context: server
指定最近的 server {…} 塊的監聽地址和端口。僅指定一個地址、一個端口、或一個地址的服務器名,都是可以的。
如果僅給定 IP 地址,則端口默認為 80
如果指令帶有 default 參數,則最近的 server {…} 配置塊,將成為 IP 地址:端口 對的默認服務器。
如果沒有指令帶有 default 參數,則缺省取 address:port 對出現的第一個 server 配置快。
location
syntax: location [=|~|~*|^~] /uri/ { … }
default: no
context: server
在特定 URI 上設置不同的配置。既能使用普通的字符串也和正則表達式來配置。
使用正則表達式,必須帶一個前綴:
1、 ~* 表示大小寫不敏感的。
2、 ~ 表示大小寫敏感的。
確定哪一個 location 指令匹配某查詢,首先檢查普通字符串。普通字符串匹配該查詢的開始
部分,而且是大小寫敏感的 – 取匹配得最接近的一個。之后,將以在配置文件中出現的次序
來檢查各個正則表達式。找到第一個匹配查詢的正則表達式時停止。如果沒有找到匹配的正則
表達式,則使用普通字符串搜索到的結果。
有兩種方式修改這樣的行為。一是使用 “=” 前綴,僅精確匹配查詢。如果查詢匹配,則搜索
停止,并立即處理請求。比如,如果對 “/” 的請求比較頻繁,則使用 “location = / ” 將
加快處理這種請求的過程。
第二種是使用前綴 ^~ 。該前綴與普通字符串一起使用,來告訴 nginx ,如果提供的路徑匹配
則不要去檢查正則表達式。舉個例子,”location ^~ /images/” 表示以 /images/ 開始的查詢
則可停止搜索 – 所有的正則表達式指令都不會被檢查。
總結一下,指令以下面的順序來檢查:
1、使用 = 前綴的指令,它精確匹配查詢。如果找到,則停止查找。
2、所有剩余的常規字符串的指令。如果該匹配使用了 ^~ 前綴,則搜索停止。
3、正則表達式,以在配置文件中定義的次序。
4、如果第 3 點獲得了一個匹配,則使用該結果。否則,使用從 第 2 點中匹配到的結果。
nginx 比對解碼后的 URIs, 知道這點很重要。如果你希望匹配 “/images/%20/test”,則你需要
使用 “/images/ /test” 來確定 location 。
root
sendfile
syntax: sendfile [on|off]
default: sendfile off
context: http, server, location
啟用或不啟用 sendfile()
server
syntax: server {…}
default: no
context: http
虛擬主機的配置指令。
不區分 IP 和基于名稱( Host 請求頭) 的虛擬主機。
listen 指令用來描述所有監聽的地址和端口。
server_name 指令來描述所有虛擬主機的名稱。
server_name
syntax: server_name name […]
default: server_name hostname
context: server
該指令執行兩種操作:
。 將 HTTP 請求中的 Host 請求頭與配置文件中 server {…} 配置塊比對,選擇第一個匹配的。
這就是虛擬服務器的定義。用下面的順序處理服務器名:
1、完整的,靜態的名稱;
2、以通配符開頭的名稱 – *.example.com
3、以通配符結尾的名稱 – www.example.*
4、使用正則表達式的名稱
如果沒有匹配的,則使用以下順序使用配置文件中的某個 [#server server { … }] 塊:
1、server 塊擁有標記為 default 的匹配 listen 指令。
2、擁有匹配的 listen 指令的第一個 server 塊。
。如果 server_name_in_redirect 指令設置了,則設置用于 HTTP 轉向中的服務器名。
server_tokens
types
HTTP 基本認證模塊(Http Auth Basic Module):
使用該模塊,可以基于 HTTP 基本認證,使用用戶名和密碼來保護站點或其中的某部分。
auth_basic
syntax: auth_basic [text|off]
default: auth_basic off
context: http, server, location, limit_except
該指令包含 HTTP 基本認證使用的測試名和密碼。分配的參數用于認證 realm 。
“off” 值將覆蓋掉從底層指令繼承來的動作。
auth_basic_user_file
syntax: auth_basic_user_file the_file
default: no
context: http, server, location, limit_except
該指令設置用于認證 realm 的 htpasswd 文件名。需要注意的是,從 0.6.7 開始,
相對路徑的文件路徑將相對于配置文件 nginx.conf 所在的目錄,而不是 prefix 目錄。
密碼必須使用 crypt 函數編碼,你可以使用apache中的 htpasswd 程序構建。
HTTP 自動索引
該模塊提供自動給出目錄文件列表頁面的功能。僅當 ngx_http_index_module 沒有找到索引
文件時,請求才會到達該 ngx_http_autoindex_module 模塊。
autoindex
syntax: autoindex [ on|off ]
default: autoindex off
context: http, server, location
啟用或禁止自動目錄文件列表功能。
autoindex_exact_size
syntax: autoindex_exact_size [ on|off ]
default: autoindex_exact_size on
context: http, server, location
定義在目錄列表中如何顯示文件的大小 — 精確的字節數或有取舍的更可讀的
autoindex_localtime autoindex_localtime
syntax: autoindex_localtime [ on|off ]
default: autoindex_localtime off
context: http, server, location
是否顯示文件時間為本地時間,缺省不顯示(為 GMT 時間)。
索引文件(Index):
設置 URL 中未指定文件時的默認文件。可指定多個文件。
前面的文件沒找到時,將依次查找后面的。
index
syntax: index file-path [file-path [ … ] ];
default: no
context: server, location
日志(Log):
設置 Nginx 如何為大量事件寫入日志。
access_log
syntax: access_log path [format [buffer=size | off]]
default: access_log log/access.log combined
context: http, server, location
經典案例:
1.server塊配置可如下配置
include www.conf;
include vhosts/*.conf;
2.ngnix 提供了查看狀態的web界面 具體配置如下:
##status
server{
listen 80;
server_name www.xiaoxiaoshan.com;
location / {
stub_status on;
access_log off;
}
}
第一個server表示nginx啟動到現在共處理了--個連接。
第二個accepts表示nginx啟動到現在共成功創建--次握手
請求丟失數 = (握手數-連接數)
第三個handled requests,表示總共處理了--次請求
reading:nginx讀取到客戶端的Header信息數
Writing:nginx返回個客戶端的header信息數
Waiting:nginx已經處理完正在等候下一次請求指令的駐留連接。開啟keep-alive的情況下,這個值等一active-(reading+writing)
3.nginx的錯誤信息:
nginx的錯誤信息是調試nginx服務的重要手段,屬于核心功能模塊,該參數名字為error_log,可以放在main區塊中全局配置,也可以放在不同主機中單獨記錄虛擬主機的錯誤信息。
error_log的語法格式及參數語法說明如下:
errot_log? file level;
error_log的默認值為:
#defaule: error_log logs/error.log error;
可以放置的標簽段為:
#context:main ,http,server,location
4.nginx的訪問日志介紹
nginx軟件會把每個用戶訪問網站的日志信息記錄到指定的日志文件里,共網站提供分析用戶瀏覽行為等。
nginx的訪問日志主要有下面兩個參數控制:
log_format 用來定義記錄日志的格式
access_log 用來指定日志文件的路徑及使用的何種日志格式記錄日志。
nginx日志格式中默認的參數配置如下:
#log_format? main? '$remote_addr - $remote_user [$time_local] "$request" '
#? ? ? ? ? ? ? ? ? '$status $body_bytes_sent "$http_referer" '
#? ? ? ? ? ? ? ? ? '"$http_user_agent" "$http_x_forwarded_for"';
#access_log? logs/access.log? main;
$server_name:虛擬主機名稱。
$remote_addr:遠程客戶端的IP地址。
-:空白,用一個“-”占位符替代,歷史原因導致還存在。
?$remote_user:遠程客戶端用戶名稱,用于記錄瀏覽者進行身份驗證時提供的名字,如登錄百度的用戶名scq2099yt,如果沒有登錄就是空白。
[$time_local]:訪問的時間與時區?,比如18/Jul/2012:17:00:01 +0800,時間信息最后的"+0800"表示服務器所處時區位于UTC之后的8小時。
$request:請求的URI和HTTP協議,這是整個PV日志記錄中最有用的信息,記錄服務器收到一個什么樣的請求
$status?:記錄請求返回的http狀態碼,比如成功是200。
$uptream_status:upstream狀態,比如成功是200.
$body_bytes_sent:?發送給客戶端的文件主體內容的大小,比如899,可以將日志每條記錄中的這個值累加起來以粗略估計服務器吞吐量。
$http_referer:記錄從哪個頁面鏈接訪問過來的。
$http_user_agent:客戶端瀏覽器信息
$http_x_forwarded_for:客戶端的真實ip,通常web服務器放在反向代理的后面,這樣就不能獲取到客戶的IP地址了,通過$remote_add拿到的IP地址是反向代理服務器的iP地址。反向代理服務器在轉發請求的http頭信息中,可以增加x_forwarded_for信息,用以記錄原有客戶端的IP地址和原來客戶端的請求的服務器地址。
$ssl_protocol:SSL協議版本,比如TLSv1。
$ssl_cipher:交換數據中的算法,比如RC4-SHA。
$upstream_addr:upstream的地址,即真正提供服務的主機地址。
$request_time:整個請求的總時間。
$upstream_response_time:請求過程中,upstream的響應時間。
5.nginx訪問日志輪詢切割
#!/bin/sh
dateformat=`date +%Y%m%d`
basedir="/application/nginx"
nginxlogdir="$basedir/logs"
logname="access_www"
[-d $nginxlogdir ]&&cd $nginxlogdir || exit 1
[ -f ${logname}.log ]||exit 1
/bin/mv ${logname}.log ${dateformat}_${logname}.log
$basedir/sbin/nginx -s reload
將上述腳本做定時任務執行。
6.nginx rewrite規則
nginx rewrite 正則表達式匹配
大小寫匹配
~ 為區分大小寫匹配
~* 為不區分大小寫匹配
!~和!~*分別為區分大小寫不匹配及不區分大小寫不匹配
文件及目錄匹配
-f和!-f用來判斷是否存在文件
-d和!-d用來判斷是否存在目錄
-e和!-e用來判斷是否存在文件或目錄
-x和!-x用來判斷文件是否可執行
flag標記
last 相當于Apache里的[L]標記,表示完成rewrite
break 終止匹配, 不再匹配后面的規則。
redirect 返回302臨時重定向 地址欄會顯示跳轉后的地址。
permanent 返回301永久重定向 地址欄會顯示跳轉后的地址。
logcation的幾個使用實例:
1)location? / { }:匹配任何查詢,因為所有請求都以 / 開頭。但是正則表達式規則將被優先和查詢匹配。
2)location =/ {}:僅僅匹配/
3)location ~* \.(gif|jpg|jpeg)$
{
rewrite \.(gif|jpg)$ /logo.png;
}:location不區分大小寫,匹配任何以gif,jpg,jpeg結尾的文件。
幾個實例:
多目錄轉成參數
要求:abc.domian.com/sort/2 => abc.domian.com/index.php?act=sort&name=abc&id=2
規則配置:
if ($host ~* (.*)\.domain\.com) {
set $sub_name $1;
rewrite ^/sort\/(\d+)\/?$ /index.php?act=sort&cid=$sub_name&id=$1 last;
}
目錄對換
要求:/123456/xxxx -> /xxxx?id=123456
規則配置:
rewrite ^/(\d+)/(.+)/ /$2?id=$1 last;
再來一個針對瀏覽器優化的自動rewrite,這里rewrite后的目錄可以是存在的;
例如設定nginx在用戶使用ie的使用重定向到/nginx-ie目錄
規則如下:
if ($http_user_agent ~ MSIE) {
rewrite ^(.*)$ /nginx-ie/$1 break;
}
目錄自動加“/” ,這個功能一般瀏覽器自動完成
if (-d $request_filename){
rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
}
以下這些可能就跟廣義的rewrite重寫無關了
禁止htaccess
location ~/\.ht {
deny all;
}
禁止多個目錄
location ~ ^/(cron|templates)/ {
deny all; break;
}
禁止以/data開頭的文件,可以禁止/data/下多級目錄下.log.txt等請求
location ~ ^/data {
deny all;
}
禁止單個文件
location ~ /data/sql/data.sql {
deny all;
}
給favicon.ico和robots.txt設置過期時間; 這里為favicon.ico為99天,robots.txt為7天并不記錄404錯誤日志
location ~(favicon.ico) {
log_not_found off;
expires 99d;
break;
}
location ~(robots.txt) {
log_not_found off;
expires 7d;
break;
}
設定某個文件的瀏覽器緩存過期時間;這里為600秒,并不記錄訪問日志
location ^~ /html/scripts/loadhead_1.js {
access_log off;
expires 600;
break;
}
Nginx還可以自定義某一類型的文件的保質期時間,具體寫法看下文的代碼:
location ~* \.(js|css|jpg|jpeg|gif|png|swf)$ {
if (-f $request_filename) {
expires??? 1h;
break;
}
}
//上段代碼就將js|css|jpg|jpeg|gif|png|swf這類文件的保質期設置為一小時。
防盜鏈的設置:
防盜鏈:如果你的網站是個下載網站,下載步驟應該是先經過你的主頁找到下載地址,才能下載,為了防止某些網友直接訪問下載地址完全不通過主頁下載,我們就可以使用防盜鏈的方式,具體代碼如下:
location ~* \.(gif|jpg|swf)$ {
valid_referers none blocked start.igrow.cn sta.igrow.cn;
if ($invalid_referer) {
rewrite ^/ http://$host/logo.png;
}
}
文件反盜鏈并設置過期時間--<盜鏈多次請求也會打開你的站點的圖片啊,所以設置下緩存時間,不會每次盜鏈都請求并下載這張圖片>
location ~* ^.+\.(jpg|jpeg|gif|png|swf|rar|zip|css|js)$ {
valid_referers none blocked *.jjonline.cn *.jjonline.com.cn *.lanwei.org *.jjonline.org localhost ?42.121.107.189;
if ($invalid_referer) {
rewrite ^/?http://img.jjonline.cn/forbid.gif;
return 417;
break;
}
access_log off;
break;
}
說明:
這里的return 417 為自定義的http狀態碼,默認為403,方便通過nginx的log文件找出正確的盜鏈的請求地址
“rewrite ^/ http://img.jjonline.cn/forbid.gif;”顯示一張防盜鏈圖片
“access_log off;”不記錄訪問日志,減輕壓力
“expires 3d”所有文件3天的瀏覽器緩存
只充許固定ip訪問網站,并加上密碼;這個對有權限認證的應用比較在行
location \ {
allow 22.27.164.25; #允許的ipd
deny all;
auth_basic “KEY”; #認證的一些設置
auth_basic_user_file htpasswd;
}
說明:location的應用也有各種變化,這里的寫法就針對了根目錄了。
文件和目錄不存在的時重定向
if (!-e $request_filename) {
#proxy_pass http://127.0.0.1; #這里是跳轉到代理ip,這個代理ip上有一個監聽的web服務器
rewrite ^/ http://www.jjonline.cn/none.html; ?#跳轉到這個網頁去
#return 404; #直接返回404碼,然后會尋找root指定的404.html文件
}
域名跳轉
server {
listen 80;
server_name jump.jjonline.cn ;#需要跳轉的多級域名
index index.html index.htm index.php; #入口索引文件的名字
root /var/www/public_html/; #這個站點的根目錄
rewrite ^/ http://www.jjonline.cn/;
#rewrite到這個地址,功能表現:在瀏覽器上輸入jump.jjonline.cn并回車,不會有任何提示直接變成www.jjonline.cn
access_log off;
}
多域名轉向
server {
listen 80;
server_name www.jjonline.cn www.jjonline.org;
index index.html index.htm index.php;
root /var/www/public_html/;
if ($host ~ “jjonline\.org”) {
rewrite ^(.*) http://www.jjonline.cn$1 permanent;
}
}
三級域名跳轉
if ($http_host ~* “^(.*)\.i\.jjonline\.cn$”) {
rewrite ^(.*) http://demo.jjonline.cn$1;
break;
}
域名鏡向
server {
listen 80;
server_name mirror.jjonline.cn;
index index.html index.htm index.php;
root /var/www/public_html;
rewrite ^/(.*) http://www.jjonline.cn/$1 last;
access_log off;
}
某個子目錄作鏡向,這里的示例是demo子目錄
location ^~ /demo {
rewrite ^.+ http://demo.jjonline.cn/ last;
break;
}
以下在附帶本博客的rewrite寫法,emlog系統的rewrite
location ~ {
if (!-e $request_filename) {
rewrite ^/(.+)$ /index.php last;
}
}
7.fastcgi模塊
配置文件如下:
server {
listen? ? ? 80;
server_name? blog.xiaoxiaoshan.com··;
root? html/blog;
index? index.php index.html index.htm;
location / {
if (-f $request_filename/index.html){
rewrite (.*) $1/index.html break;
}
if (-f $request_filename/index.php){
rewrite (.*) $1/index.php;
}
if (!-f $request_filename){
rewrite (.*) /index.php;
}
}
location ~ .*\.(php|php5)?$
{
fastcgi_pass? 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
access_log? /app/logs/blog_access.log? main;
}
8.負載均衡
upstream backend {
server bacnkend1.example.com weight = 5;
server backend2.example.com:8080;
server unix:/tmp/backend3;
server backup1.example.com:8080 backup;
server backup2.example.com:8080 backup;
ip_hash;
}
server {
location / {
proxy_pass http://backend;
}
}
9.Http_proxy模塊
location / {
? ? proxy_pass http://localhost:8000;
? ? proxy_set_header Host $host;
? ? proxy_set_header X-Real-IP $remote_addr;
}