Nginx完整配置说明

#用户 用户组 
user www www; 
#工作进程,根据硬件调整,有人说几核cpu,就配几个,我觉得可以多一点 
worker_processes5; 
#错误日志 
error_loglogs/error.log; 
#pid文件位置 
pidlogs/nginx.pid; 
worker_rlimit_nofile 8192; 

events { 
#工作进程的最大连接数量,根据硬件调整,和前面工作进程配合起来用,尽量大,但是别把cpu跑到100%就行 
worker_connections4096; 
} 

http { 
includeconf/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.logmain; 
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_namedomain1.com www.domain1.com; 
#访问日志,和上面的级别不一样,应该是下级的覆盖上级的 
access_log logs/domain1.access.logmain; 
root html; 

location / { 
indexindex.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_namedomain2.com www.domain2.com; 
access_log logs/domain2.access.logmain; 

#静态文件,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_passhttp://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 { 
listen80; 
server_name big.server.com; 
access_loglogs/big.server.access.log main; 

location / { 
proxy_passhttp://big_server_com; 
} 
} 
} 

上面说的include的几个文件,都没有必要改,用的时候include一下就可以。

fastcgi.conf

# fastcgi.conf 
fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name; 
fastcgi_paramQUERY_STRING $query_string; 
fastcgi_paramREQUEST_METHOD $request_method; 
fastcgi_paramCONTENT_TYPE $content_type; 
fastcgi_paramCONTENT_LENGTH $content_length; 
fastcgi_paramSCRIPT_NAME$fastcgi_script_name; 
fastcgi_paramREQUEST_URI$request_uri; 
fastcgi_paramDOCUMENT_URI $document_uri; 
fastcgi_paramDOCUMENT_ROOT$document_root; 
fastcgi_paramSERVER_PROTOCOL$server_protocol; 
fastcgi_paramGATEWAY_INTERFACECGI/1.1; 
fastcgi_paramSERVER_SOFTWAREnginx/$nginx_version; 
fastcgi_paramREMOTE_ADDR$remote_addr; 
fastcgi_paramREMOTE_PORT$remote_port; 
fastcgi_paramSERVER_ADDR$server_addr; 
fastcgi_paramSERVER_PORT$server_port; 
fastcgi_paramSERVER_NAME$server_name; 

fastcgi_indexindex.php;
 
# PHP only, required if PHP was built with --enable-force-cgi-redirect 
fastcgi_paramREDIRECT_STATUS200; 

proxy.conf

# proxy.conf 
proxy_redirectoff; 
proxy_set_headerHost$host; 
proxy_set_headerX-Real-IP $remote_addr; 
proxy_set_headerX-Forwarded-For $proxy_add_x_forwarded_for; 
client_max_body_size10m; 
client_body_buffer_size 128k; 
proxy_connect_timeout 90; 
proxy_send_timeout90; 
proxy_read_timeout90; 
proxy_buffers 32 4k;

mine.types

# mime.types 
types { 
text/html html htm shtml; 
text/csscss; 
text/xmlxml rss; 
image/gif gif; 
image/jpegjpeg jpg; 
application/x-javascriptjs; 
text/plaintxt; 
text/x-componenthtc; 
text/mathml mml; 
image/png png; 
image/x-iconico; 
image/x-jng jng; 
image/vnd.wap.wbmpwbmp; 
application/java-archivejar war ear; 
application/mac-binhex40hqx; 
application/pdf pdf; 
application/x-cocoa cco; 
application/x-java-archive-diff jardiff; 
application/x-java-jnlp-filejnlp; 
application/x-makeselfrun; 
application/x-perlpl pm; 
application/x-pilot prc pdb; 
application/x-rar-compressedrar; 
application/x-redhat-package-managerrpm; 
application/x-sea sea; 
application/x-shockwave-flash swf; 
application/x-stuffit sit; 
application/x-tcl tcl tk; 
application/x-x509-ca-certder pem crt; 
application/x-xpinstall xpi; 
application/zip zip; 
application/octet-streamdeb; 
application/octet-streambin exe dll; 
application/octet-streamdmg; 
application/octet-streameot; 
application/octet-streamiso img; 
application/octet-streammsi msp msm; 
audio/mpegmp3; 
audio/x-realaudio ra; 
video/mpegmpeg mpg; 
video/quicktime mov; 
video/x-flv flv; 
video/x-msvideo avi; 
video/x-ms-wmvwmv; 
video/x-ms-asfasx asf; 
video/x-mng mng; 
}