轉: http://blog.sina.com.cn/s/blog_474cf12b0100fyr2.html
Nginx配置文件非常重要,很多實用的功能都需要在此文件進行配置後,方可使用。此文件路徑爲/usr/local/nginx/conf/nginx.conf.以下是其內容的詳細說明,有網上轉載的內容,有的是我自己實際應用時添加的內容。
一、基本配置
#用戶和組
user www www;
#啓動進程,一般爲CPU數
worker_processes 1;
#全局錯誤日誌及PID文檔 日誌級別:【debug | info | notice | warn | error | crit 】
error_log /web/www/logs/error.log notice;
pid /usr/local/nginx/logs/nginx.pid;
#文件描述符數量
#worder_rlimit_nofile 51200;
#工作模式及連接數上限
events {
use epoll;
worker_connections 51200; # Maxclient = work_processes * worker_connections
}
#設定http服務器,利用他的反向代理功能提供負載均衡支持
http {
#設定mime類型
include mime.types;
default_type application/octet-stream;
#設定日誌格式
log_format main '$remote_addr - $remote_user [$time_local]'
'"$request" $status $bytes_sent '
'"$http_referer" "$http_user_agent" '
'"$gzip_ratio"';
log_format download '$remote_addr - $remote_user [$time_local] '
'"$request" $status $bytes_sent '
'"$http_referer" "$http_user_agent" '
'"$http_range" "$sent_http_content_range"';
#設定請求緩衝
client_header_buffer_size 1k;
large_client_header_buffers 4 4k;
#開啓gzip模塊
gzip on;
gzip_min_length 1100;
gzip_buffers 4 8k;
gzip_types text/plain;
output_buffers 1 32k;
postpone_output 1460;
#設定access log
access_log logs/access.log main;
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
#設定服務器支持shtml
ssi on;
ssi_silent_errors on;
ssi_types text/shtml;
#設定負載均衡的服務器列表
upstream mysvr {
#weigth參數表示權值,權值越高被分配到的機率越大
#本機上的Squid開啓3128端口
server 192.168.8.1:3128 weight=5;
server 192.168.8.2:80 weight=1;
server 192.168.8.3:80 weight=6;
}
#設定虛擬主機
server {
listen 80;
server_name 192.168.8.1 www.lnmp.com;
index
index.html index.htm index.php;
root
/web/www; # 設置該主機的根路徑
charset gb2312; #
#設定本虛擬主機的 main 日誌存放路徑
access_log logs/www.lnmp.com.access.log main;
error_page 404 /404.html # 服務器出現404錯誤時的返回頁面
#假如訪問 /img/*, /js/*, /css/* 資源,則直接取本地文檔,不通過squid
#假如這些文檔較多,不推薦這種方式,因爲通過squid的緩存效果更好
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ # 使用正則表達式匹配以gif...結尾的文件,你必須指定~ 前綴(區分大小寫) ;
~* 則不區分大小寫。“^~”標記禁止在字符串匹配後檢查正則表達式。 location =/ {}: 只匹配 / 的查詢; location
/ {}匹配任意以/ 開頭的路徑;location ^~ /images/ {} 匹配以/images/開頭的API。
{
expires 30d;
alias
/spool/images/\.(gif|jpg|jpeg|png|bmp|swf) ; # 將 location 指定的路徑跳轉到該路徑
}
location ~ .*\.(js|css)?$
{
expires 1h;
}
#假如這些文檔較多,不推薦這種方式,因爲通過squid的緩存效果更好
location
~ .*\.(php|php5)?$
{
#fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fcgi.conf;
}
#設置主機開啓SSI支持shtml
location ~ .*\.(shtml)?$ {
ssi on;
ssi_silent_errors on;
ssi_types text/shtml;
}
#對 "/" 啓用負載均衡
location / {
proxy_pass http://mysvr; 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 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
#設定查看Nginx狀態的地址
location /NginxStatus {
stub_status on;
access_log on;
auth_basic "NginxStatus";
}
}
}
|
二、高級配置
1、查看 Nginx 運行狀態
輸入地址http://192.168.8.1/NginxStatus/,輸入驗證帳號密碼,即可看到類似如下內容:
Active connections: 328
server accepts handled requests
9309 8982 28890
Reading: 1 Writing: 3 Waiting: 324
第一行表示現在活躍的連接數
第三行的第三個數字表示Nginx運行到
|
nginx的upstream目前支持4種方式的分配
1、輪詢(默認)
每個請求按時間順序逐一分配到不同的後端服務器,如果後端服務器down掉,能自動剔除。
2、weight
指定輪詢機率,weight和訪問比率成正比,用於後端服務器性能不均的情況。
2、ip_hash
每個請求按訪問ip的hash結果分配,這樣每個訪客固定訪問一個後端服務器,可以解決session的問題。
3、fair(第三方)
按後端服務器的響應時間來分配請求,響應時間短的優先分配。
4、url_hash(第三方)
按訪問url的hash結果來分配請求,使每個url定向到同一個後端服務器,後端服務器爲緩存時比較有效。
代理
只需要在nginx的配置文件中增加虛擬主機,然後加入
\proxy_pass http://localhost:8000;
3、配置Nginx虛擬主機(防盜鏈及expires設置)
server
{
listen 80;
server_name bbs.linuxtone.org www.linuxtone.org;
index index.html index.php index.htm;
root /data/www/wwwroot/lt/bbs;
#access_log /var/log/nginx/access_bbs.redocn.com.log combined;
location / {
if (!-e $request_filename) {
rewrite ^/archiver/((fid|tid)-[\w\-]+\.html)$ /archiver/index.php?$1 last;
rewrite ^/forum-([0-9]+)-([0-9]+)\.html$ /forumdisplay.php?fid=$1&page=$2 last;
rewrite ^/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ /viewthread.php?tid=$1&extra=page%3D$3&page=$2
last;
rewrite ^/space-(username|uid)-(.+)\.html$ /space.php?$1=$2 last;
rewrite ^/tag-(.+)\.html$ /tag.php?name=$1 last;
break;
}
}
#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;
if ($invalid_referer) {
rewrite ^/ http://bbs.linuxtone.org/images/default/logo.gif;
return 403;
}
}
# Add expires header for static content
location ~* \.(js|css|jpg|jpeg|gif|png|swf)$ {
if (-f $request_filename) {
|