[轉]Nginx 配置檔案nginx.conf的完整配置說明

weixin_34402090發表於2011-08-09

轉載自: http://www.inginx.com/nginx-conf-in/

#使用者 使用者組
user www www;
#工作程式,根據硬體調整,有人說幾核cpu,就配幾個,我覺得可以多一點
worker_processes 5;
#錯誤日誌
error_log logs/error.log;
#pid檔案位置
pid logs/nginx.pid;
worker_rlimit_nofile 8192;

events {
#工作程式的最大連線數量,根據硬體調整,和前面工作程式配合起來用,儘量大,但是別把cpu跑到100%就行
worker_connections 4096;
}

http {
include conf/mime.types;
#反向代理配置,可以開啟proxy.conf看看
include /etc/nginx/proxy.conf;
#fastcgi配置,可以開啟fastcgi.conf看看
include /etc/nginx/fastcgi.conf;

default_type application/octet-stream;
#日誌的格式
log_format main '$remote_addr - $remote_user [$time_local] $status '
'"$request" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
#訪問日誌
access_log logs/access.log main;
sendfile on;
tcp_nopush on;
#根據實際情況調整,如果server很多,就調大一點
server_names_hash_bucket_size 128; # this seems to be required for some vhosts

#這個例子是fastcgi的例子,如果用fastcgi就要仔細看
server { # php/fastcgi
listen 80;
#域名,可以有多個
server_name domain1.com www.domain1.com;
#訪問日誌,和上面的級別不一樣,應該是下級的覆蓋上級的
access_log logs/domain1.access.log main;
root html;

location / {
index index.html index.htm index.php;
}

#所有php字尾的,都通過fastcgi傳送到1025埠上
#上面include的fastcgi.conf在此應該是有作用,如果你不include,那麼就把fastcgi.conf的配置項放在這個下面。
location ~ .php$ {
fastcgi_pass 127.0.0.1:1025;
}
}

#這個是反向代理的例子
server { # simple reverse-proxy
listen 80;
server_name domain2.com www.domain2.com;
access_log logs/domain2.access.log main;

#靜態檔案,nginx自己處理
location ~ ^/(images|javascript|js|css|flash|media|static)/ {
root /var/www/virtual/big.server.com/htdocs;
#過期30天,靜態檔案不怎麼更新,過期可以設大一點,如果頻繁更新,則可以設定得小一點。
expires 30d;
}

#把請求轉發給後臺web伺服器,反向代理和fastcgi的區別是,反向代理後面是web伺服器,fastcgi後臺是fasstcgi監聽程式,當然,協議也不一樣。
location / {
proxy_pass http://127.0.0.1:8080;
}
}

#upstream的負載均衡,weight是權重,可以根據機器配置定義權重。據說nginx可以根據後臺響應時間調整。後臺需要多個web伺服器。
upstream big_server_com {
server 127.0.0.3:8000 weight=5;
server 127.0.0.3:8001 weight=5;
server 192.168.0.1:8000;
server 192.168.0.1:8001;
}

server {
listen 80;
server_name big.server.com;
access_log logs/big.server.access.log main;

location / {
proxy_pass http://big_server_com;
}
}
}

相關文章