#运行用户
user www-data;
#启动进程,一般设置成和cpu的数量相等
worker_processes 1;php#全局错误日志及PID文件
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;css#工做模式及链接数上限
events {
use epoll; #epoll是多路复用IO(I/O Multiplexing)中的一种方式,可是仅用于linux2.6以上内核,能够大大提升nginx的性能
worker_connections 1024;#单个后台worker process进程的最大并发连接数
# multi_accept on;
}html#设定http服务器,利用它的反向代理功能提供负载均衡支持
http {
#设定mime类型,类型由mime.type文件定义
include /etc/nginx/mime.types;
default_type application/octet-stream;
#设定日志格式
access_log /var/log/nginx/access.log;java#sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,对于普通应用,
#必须设为 on,若是用来进行下载等应用磁盘IO重负载应用,可设置为 off,以平衡磁盘与网络I/O处理速度,下降系统的uptime.
sendfile on;
#tcp_nopush on;node#链接超时时间
#keepalive_timeout 0;
keepalive_timeout 65;
tcp_nodelay on;
#开启gzip压缩
gzip on;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";linux#设定请求缓冲
client_header_buffer_size 1k;
large_client_header_buffers 4 4k;nginxinclude /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;web#设定负载均衡的服务器列表
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;
}apache
server {
#侦听80端口
listen 80;
#定义使用www.xx.com访问
server_name www.xx.com;#设定本虚拟主机的访问日志
access_log logs/www.xx.com.access.log main;#默认请求
location / {
root /root; #定义服务器的默认网站根目录位置
index index.php index.html index.htm; #定义首页索引文件的名称fastcgi_pass www.xx.com;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}# 定义错误提示页面
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /root;
}#静态文件,nginx本身处理
location ~ ^/(images|javascript|js|css|flash|media|static)/ {
root /var/www/virtual/htdocs;
#过时30天,静态文件不怎么更新,过时能够设大一点,若是频繁更新,则能够设置得小一点。
expires 30d;
}
#PHP 脚本请求所有转发到 FastCGI处理. 使用FastCGI默认配置.
location ~ \.php$ {
root /root;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /home/www/www$fastcgi_script_name;
include fastcgi_params;
}
#设定查看Nginx状态的地址
location /NginxStatus {
stub_status on;
access_log on;
auth_basic "NginxStatus";
auth_basic_user_file conf/htpasswd;
}
#禁止访问 .htxxx 文件
location ~ /\.ht {
deny all;
}
}
}
以上是一些基本的配置,使用Nginx最大的好处就是负载均衡
若是要使用负载均衡的话,能够修改配置http节点以下:
#设定http服务器,利用它的反向代理功能提供负载均衡支持
http {
#设定mime类型,类型由mime.type文件定义
include /etc/nginx/mime.types;
default_type application/octet-stream;
#设定日志格式
access_log /var/log/nginx/access.log;#省略上文有的一些配置节点
#。。。。。。。。。。
#设定负载均衡的服务器列表
upstream mysvr {
#weigth参数表示权值,权值越高被分配到的概率越大
server 192.168.8.1x:3128 weight=5;#本机上的Squid开启3128端口
server 192.168.8.2x:80 weight=1;
server 192.168.8.3x:80 weight=6;
}upstream mysvr2 {
#weigth参数表示权值,权值越高被分配到的概率越大server 192.168.8.x:80 weight=1;
server 192.168.8.x:80 weight=6;
}#第一个虚拟服务器
server {
#侦听192.168.8.x的80端口
listen 80;
server_name 192.168.8.x;#对aspx后缀的进行负载均衡请求
location ~ .*\.aspx$ {root /root; #定义服务器的默认网站根目录位置
index index.php index.html index.htm; #定义首页索引文件的名称proxy_pass http://mysvr ;#请求转向mysvr 定义的服务器列表
#如下是一些反向代理的配置可删除.
proxy_redirect off;
#后端的Web服务器能够经过X-Forwarded-For获取用户真实IP
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; #nginx跟后端服务器链接超时时间(代理链接超时)
proxy_send_timeout 90; #后端服务器数据回传时间(代理发送超时)
proxy_read_timeout 90; #链接成功后,后端服务器响应时间(代理接收超时)
proxy_buffer_size 4k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小
proxy_buffers 4 32k; #proxy_buffers缓冲区,网页平均在32k如下的话,这样设置
proxy_busy_buffers_size 64k; #高负荷下缓冲大小(proxy_buffers*2)
proxy_temp_file_write_size 64k; #设定缓存文件夹大小,大于这个值,将从upstream服务器传}
}
}
Nginx是一个轻量级高性能的web服务器,它是为快速响应大量静态文件请求和高效利用系统资源而设计的。与apache使用面向进程或线程的方式处理请求不一样,nginx使用异步事件驱动模型在负载下性能更突出。
虽然nginx能高效地服务静态文件,但也有人认为nginx处理动态内容并不理想。不像apache服务器,nginx没用使用内嵌解释器的方式来处理动态内容。相反,动态内容被丢给cgi,fastcgi或者像apache这样的web服务器,而后把处理结果返回给nginx,nginx在返给浏览器。这种方式就致使部署起来会更复杂一些。出于这些缘由,使用和配置nginx可能会晦涩。nginx的配置感受更复杂或者不直接。
本文的前提是你使用nginx安装来安装nginx,若是你使用其它方法或者系统自带包安装,那么你的配置文件的位置和下面讲的配置文件位置可能不一样。
nginx的强大都是靠配置文件来实现,nginx就是一个二进制文件nginx读入一个配置文件nginx.conf(nginx.conf可能include包含若干子配置文件)来实现各类各样的功能。
nginx使用嵌套的花括号语法来定义选项。安装完成后nginx的主配置文件被放在/usr/local/nginx/nginx.conf。同时一个默认的备份配置文件存在/usr/local/nginx/nginx.conf.default。其它子配置文件一样有一个.default结尾的备份配置文件。.default的做用是当你修改配置文件错误的时候,你能够快速回到上一个好用的状态。建议你们常常对配置好的功能的配置文件作备份,能够以日期文件作结尾。好比你可使用以下指令完成,修改日期为你所备份的日期。
cp /usr/local/nginx/nginx.conf /usr/local/nginx/nginx.conf-20121224每次修改完nginx.conf都要从新加载配置文件
/usr/loca/nginx/nginx -tkill -HUP `cat /usr/local/nginx/nginx.pid`咱们分片断一点点的介绍默认的配置文件
#user nobody;worker_processes 1;#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;}这些是配置文件开始的默认行。一般的环境下,你不须要修改这些选项。这一部分有几个方面须要咱们注意:
events
能够包含多个子指令做为参数。这些子指令以花括号包围。下面咱们继续读配置文件
http { include mime.types; 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;"http { }"块的开头像配置文件的开头同样都是标准配置不须要修改。这里咱们须要把注意力放在这些元素上:
使用gizp压缩并非没有代价的。在下降带宽的同时也增长了CPU的使用。gzip_cop_level的参数取值范围1-9,9表明最用CPU和1表明最少用CPU,其默认值是1.
另外,请注意上面的片断"http { "并不包含结尾"}",其他部分解下面继续。
咱们家是nginx.conf接下来的配置文件是这样
server { listen 80; server_name localhost; access_log logs/localhost.access.log main; location / { root html; index index.html index.htm; } } }咱们能够看到http{ }块到此结束。
server指令块,像上面例子中那个同样,是咱们nginx用户主要配置本身虚拟主机的地方。在server块里有许多重要的指令。listen指令告诉nginx在一个特定的hostname,ip或者tcp端口监听链接。默认,http服务运行在80端口。一下这些listen指令都是有效的:
listen 127.0.0.1:80;listen localhost:80;listen 127.0.0.1:8080;listen localhost:8080;listen 192.168.3.105:80;listen 192.168.3.105:8080;listen 80;listen *:80;listen 8080;listen *:8080;listen 12.34.56.77:80;listen 12.34.56.78:80;listen 12.34.56.79:80;在这些例子中,咱们能够看到不少不一样表达方式:
server_name指令能够设置基于域名的虚拟主机,根据请求头部的内容,一个ip的服务器能够配置多个域名。下面这些server_name的参数是有效的:
server_name nginx.cn;server_name nginx.cn www.nginx.cn;server_name *.nginx.cn;server_name .nginx.cn;server_name nginx.*;server_name nginx.cng bucknell.net brackley.org;server_name localhost litchfield bleddington;server_name "";多个域名之间以空格分隔。nginx容许一个虚拟主机有一个或多个名字,也可使用通配符"*"来设置虚拟主机的名字。上面的例子咱们看到了不少特殊的地方:
咱们继续分析接下来的server指令块,看看access_log指令。
access_log logs/nginx.access.log;access_log /srv/http/ducklington.org/logs/access.log;access_log /var/log/nginx/access/ducklington.org;access_log off;第一个例子,日志使用相对路径,log文件放在与配置文件同级的目录中,也就是日志存储在/usr/local/nginx/logs/nginx.access.log;接下来的两个例子定义了完整的绝对路径;最后一个例子是关闭access log,不会记录访问日志到文件。
server块的最后部分是location指令块,对于client的不一样请求目标,location是用来配置服务器的不一样响应。
就像server_name指令配置nginx处理请求使用包含在请求中的信息同样,location指令配置如何响应不一样位置资源的请求。例如:
location / { }location /images/ { }location /blog/ { }location /planet/ { }location /planet/blog/ { }location ~ IndexPage\.php$ { }location ~ ^/BlogPlanet(/|/index\.php)$ { }location ~* \.(pl|cgi|perl|prl)$ { }location ~* \.(md|mdwn|txt|mkdn)$ { }location ^~ /images/IndexPage/ { }location ^~ /blog/BlogPlanet/ { }location = / { }前五个例子是按字面逐字匹配,匹配请求url域名后面开始的部分。假设一个请求http://www.nginx.cn,咱们假设server_name已经匹配www.nginx.cn,那么"location /"指令将捕获这个请求。nginx使用匹配度最高的location。好比http://ducklington.org/planet/blog/和http://ducklington.org/planet/blog/about/会匹配"location /planet/blog",而不是"location /planet/"
对于特定的请求,一旦nginx匹配一个location来处理。那么这个请求的响应内容就会由这个location块中的指令决定。咱们先来看一个最基本的locaiton配置块。
location / { root html; index index.html index.htm;}在这个例子中文档根(doucument root)位于html/目录。根据nginx的安装目录/usr/local/nginx,这个location的完整路径是/usr/local/nginx/html。假设一个请求访问位于/blog/includes/styles.css文件同时没有别的location块匹配,那么nginx会用位于文件系统的/usr/local/nginx/html/blog/includes/styles.css响应。固然你也能够用绝对路径设置root指令。
index指令会告诉nginx使用哪一个资源若是请求中没有文件名。所以,若是请求http://.ducklington.org/将会补全资源位置为/usr/local/nginx/html/index.html。若是index配置了多个文件,nginx会按顺序处理直到找到第一个存在的补全资源。若是index.html在相关目录中没有,那么将使用index.htm。若是两个都不存在,会返回404错误。
让咱们看另一个location指令的例子,这些location指令都在ducklington.org的server指令块里。
root /srv/www/ducklington.org/public_html;location / { index index.html index.htm;}location ~ \.php$ { gzip off; include fastcgi_params; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.pl; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;}在这个例子中,全部以.php结尾的请求都被第二个location块处理。第二个语句块对全部请求指定了一个fastcgi句柄。其它的请求,nginx会使用第一个location块来处理。
请求http://ducklington.org/将会返回/srv/www/ducklington.org/public_html/index.html若是存在的话,若是不存在这返回/srv/www/ducklington.org/public_html/index.htm,若是两个都不存在则返回404错误。
请求 http://ducklington.org/blog/将会返回/srv/www/ducklington.org/public_html/blog/index.html若是存在的话,若是不存在则返回/srv/www/ducklington.org/public_html/blog/index.htm,若是两个都不存在则返回404错误。
请求http://ducklington.org/tasks.php将会被发给fastcgi去执行位于/srv/www/ducklington.org/public_html/tasks.php的文件
请求 http://ducklington.org/squire/roster.php也将使用fastcgi句柄执行位于 /srv/www/ducklington.org/public_html/squire/roster.pl的文件,并返回结果。
具体的location匹配规则这就不说了,不明白的能够看这里nginx location匹配
上面这些例子和解释应该足够让你可以配置本身的nginx server了。下面列出配置nginx server最佳实践。
首先,把具体某一个要配置的server指令块的全部指令放在一个文件中,而后使用include语句把它包含到配置文件中。
例如绑定ducklington.org域名,首先把ducklington.org的 server指令块配置放在/srv/www/ducklington.org/nginx.conf。而后增长指令include /srv/www/ducklington.org/nginx.conf;到配置文件的http块中。像这样:
http { # [...] include /srv/www/ducklington.org/nginx.conf; # [...]}