#使用者,用戶、用戶組,如果沒有創建用戶則爲nobody,不過我在使用的時候創建了一個用戶www,和www組
user nobody nobody;
#啓動進程
worker_processes 5;
#全局錯誤日誌及PID文件
error_log logs/error.log notice;
pid logs/nginx.pid;
#工作模式及連接數上限
events {
#工作模式有:select(標準模式),poll(標準模式),kqueue(高效模式,適用FreeBSD 4.1+, OpenBSD 2.9+, NetBSD 2.0 and MacOS X),
#epoll(高效模式,本例用的。適用Linux 2.6+,SuSE 8.2,),
#/dev/poll(高效模式,適用Solaris 7 11/99+, HP/UX 11.22+ (eventport), IRIX 6.5.15+ 和 Tru64 UNIX 5.1A+)
use epoll;
#每個進程最大連接數(最大連接=連接數x進程數)
worker_connections 1024;
}
#設定http服務器,利用它的反向代理功能提供負載均衡支持
http {
#設定mime類型
include conf/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 10k;
large_client_header_buffers 44k;
#開啓gzip模塊,要求安裝gzip 在運行./config時要指定
gzip on;
gzip_min_length 1100;
gzip_buffers 48k;
gzip_types text/plain;
output_buffers 132k;
postpone_output 1460;
#設定訪問日誌
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;
#設定負載均衡的服務器列表
upstream backserver {
#weigth參數表示權值,權值越高被分配到的機率越大
#本例是指在同一臺服務器,多臺服務器改變ip即可
server 127.0.0.1:8081 weight=5;
server 127.0.0.1:8082;
server 127.0.0.1:8083;
}
#Deny access to any host other than (www).4535.com
server {
server_name _; #default
return 404;
}
#設定虛擬主機,默認爲監聽80端口,改成其他端口會出現問題
server {
listen 80;
server_name test.com www.test.com;
charset utf8;
#設定本虛擬主機的訪問日誌
access_log logs/test.com.log main;
#如果訪問 /images/*, /js/*, /css/* 資源,則直接取本地文件,不用轉發。但如果文件較多效果不是太好。
location ~ ^/(images|js|css)/ {
root /usr/local/testweb;
expires 30m;
}
#對 "/" 啓用負載均衡
location / {
proxy_pass http://backserver;
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 432k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;}
#設定查看Nginx狀態的地址,在運行./config 要指定,默認是不安裝的。
location /NginxStatus {
stub_status on;
access_log on;
auth_basic "NginxStatus";
#是否要通過用戶名和密碼訪問,測試時可以不加上。conf/htpasswd 文件的內容用 apache 提供的 htpasswd 工具來產生即可#auth_basic_user_file conf/htpasswd;
}
}
這是其中的一個nginx.conf的配置。