nginx.conf配置文件详解

来源:互联网 发布:js获取div自动宽度 编辑:程序博客网 时间:2024/05/19 04:06


首先确保安装了 gcc openssl-devel pcre-devel 和 zlib-devel 等软件,其实主要是为了安装 Nginx 期间的编译等,如果不安装,出错的时候,按照提示一个一个安装也可以。

Linux 编译安装:

wget http://nginx.org/download/nginx-1.7.6.tar.gztar zxvf nginx-1.7.6.tar.gz./configure --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.confcd nginx-1.7.6makemake install

具体安装时的配置可以参考:http://nginx.org/en/docs/configure.html

Linux 下也可以用包管理器安装:

sudo yum install nginx || sudo apt-get install nginx

那么,我们来看看 nginx.conf 配置文件的结构,类似于:

{    main    events : {}    http   : {            main            server : {                    main                    location : {}                }        }}

最外面的块是 mainmain 包含 Events 和 HTTPHTTP 包含 upstream 和多个 ServerServer 又包含多个 location;

main 块设置的指令将影响其他所有设置,相当于全局设置;

server 块的指令主要用于指定主机和端口,设置网站,例如虚拟主机;

upstream 指令主要用于负载均衡,设置一系列的后端服务器;

location 块用于匹配网页位置,就是匹配网页的路径,匹配到的路径可以做一些事情,例如反代。

这四者之间的关系式:

server继承main,location继承server,upstream既不会继承其他设置也不会被继承。

在这四个部分当中,每个部分都包含若干指令,这些指令主要包含Nginx的主模块指令、事件模块指令、HTTP核心模块指令,同时每个部分还可以使用其他HTTP模块指令,例如Http SSL模块、HttpGzip Static模块和Http Addition模块等。

#定义 Nginx 运行的用户和用户组,默认由 nobody 账号运行, windows 下面可以注释掉。 user www www;# nginx 进程数,一般建议设置为1。起的太多了,可能会带来性能上的问题。但是如果是耗 CPU 的操作的话,建议等于 CPU 总核心数。# 这个还可以和 worker_cpu_affinity 配合worker_processes 1;# 全局错误日志定义类型,[ debug | info | notice | warn | error | crit ]error_log /var/log/nginx/error.log info;# 进程文件,windows 底下可以注释掉pid /var/run/nginx.pid;# 一个nginx进程打开的最多文件描述符(句柄)数目,理论值应该是最多打开文件数(系统的值ulimit -n)与nginx进程数相除,# 但是nginx分配请求并不均匀,所以建议与ulimit -n的值保持一致。worker_rlimit_nofile 65535;
  1. 关于 Linux 里面用户和用户组的概念,参见这里

  2. 关于workercpuaffinity,可以参见这里和这里

  3. 关于pid的资料,请参考这里

  4. 关于此处可以查看

工作模式与连接数上限

events{  # 参考事件模型,use [ kqueue | rtsig | epoll | /dev/poll | select | poll ];   # epoll模型是Linux 2.6以上版本内核中的高性能网络I/O模型,如果跑在FreeBSD上面,就用kqueue模型。  use epoll;  connections 20000;  # 每个进程允许的最多连接数  # 单个进程最大连接数(最大连接数=连接数*进程数)该值受系统进程最大打开文件数限制,需要使用命令ulimit -n 查看当前设置  worker_connections 65535;}

关于事件模型,可以参考这里,这里,还有《深入浅出nodejs》中说的挺多的。

# 设定http服务器http {  include mime.types; # 文件扩展名与文件类型映射表  default_type application/octet-stream; # 默认文件类型

include 是个主模块指令,可以将配置文件拆分并引用,可以减少主配置文件的复杂度

default_type 属于HTTP核心模块指令,这里设定默认类型为二进制流,也就是当文件类型未定义时使用这种方式,例如在没有配置PHP环境时,Nginx是不予解析的,此时,用浏览器访问PHP文件就会出现下载窗口。

#charset utf-8; #默认编码sendfile on; #开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常把这个改 成off。autoindex on; #开启目录列表访问,合适下载服务器,默认关闭。keepalive_timeout 120; #长连接超时时间,单位是秒#gzip模块设置gzip on; #开启gzip压缩输出gzip_min_length 1k; #最小压缩文件大小gzip_buffers 4 16k; #压缩缓冲区gzip_http_version 1.0; #压缩版本(默认1.1,前端如果是squid2.5请使用1.0)gzip_comp_level 2; #压缩等级#压缩类型,默认就已经包含text/html,所以下面就不用再写了,写上去也不会有问题,但是会有一个warn。gzip_types text/plain application/x-javascript text/css application/xml;gzip_vary on;#limit_zone crawler $binary_remote_addr 10m; #开启限制IP连接数的时候需要使用

对于 gzip 可以参考这个文章了解下。

upstream www.ospring.pw {  # upstream 的负载均衡,weight 是权重,可以根据机器配置定义权重。weigth 参数表示权值,权值越高被分配到的几率越大。  server 192.168.60.121:80 weight=3;  server 192.168.60.122:80 weight=2;  server 192.168.60.123:80 weight=3;}

负载均衡详细配置请看这里

#虚拟主机的配置server {  # 监听端口  listen 80;  #域名可以有多个,用空格隔开  server_name www.ospring.pw ospring.pw;  ssi on; # Server Side Include,通常称为服务器端嵌入  index index.html index.htm index.php;  root /data/www/ospring.pw;  # 图片缓存时间设置  location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$ {    expires 10d;  }  # JS和CSS缓存时间设置  location ~ .*.(js|css)?$ {    expires 1h;  }  # 日志格式设定  log_format access '$remote_addr – $remote_user [$time_local] "$request" '  '$status $body_bytes_sent "$http_referer" '  '"$http_user_agent" $http_x_forwarded_for';  # 定义本虚拟主机的访问日志  access_log /var/log/nginx/ospring.pw.log access;

反向代理

# 对 “/” 启用反向代理location / {  proxy_pass http://127.0.0.1:3000;  # 设置要代理的 uri,注意最后的 /。可以是 Unix 域套接字路径,也可以是正则表达式。  proxy_redirect off; # 设置后端服务器“Location”响应头和“Refresh”响应头的替换文本  proxy_set_header X-Real-IP $remote_addr; # 获取用户的真实 IP 地址  #后端的Web服务器可以通过 X-Forwarded-For 获取用户真实IP,多个 nginx 反代的情况下,例如 CDN。参见:http://gong1208.iteye.com/blog/1559835 和 http://bbs.linuxtone.org/thread-9050-1-1.html  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  #以下是一些反向代理的配置,可选。  proxy_set_header Host $host; # 允许重新定义或者添加发往后端服务器的请求头。  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服务器传}# 本地动静分离反向代理配置# 所有 jsp 的页面均交由tomcat或resin处理location ~ .(jsp|jspx|do)?$ {  proxy_set_header Host $host;  proxy_set_header X-Real-IP $remote_addr;  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  proxy_pass http://127.0.0.1:8080;}# 所有静态文件由nginx直接读取不经过tomcat或resinlocation ~ .*.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)${  root    /data/www/ospring.pw/public;  expires 15d;}location ~ ^/(upload|html)/  {  root    /data/www/ospring.pw/public/html;  expires 30d;}include     vhosts/*.conf; 分割配置文件,方便管理}

ssi 请参考这里

举例:

location ^~ /service/ {  proxy_pass http://192.168.60.245:8080/;  proxy_redirect      default;  proxy_set_header    Host $host:$server_port;  proxy_set_header    X-Real-IP $remote_addr;  proxy_set_header    X-Forwarded-Host $host:$server_port;  proxy_set_header    X-Forwarded-Server $host:$server_port;  proxy_set_header    X-Forwarded-For  $proxy_add_x_forwarded_for;}

简化:

location ^~ /service/ {  proxy_pass http://192.168.60.245:8080/;  proxy_redirect      default;  proxy_set_header    Host $host  proxy_set_header    X-Real-IP $remote_addr;  proxy_set_header    X-Forwarded-For  $proxy_add_x_forwarded_for;}

更简化:

location ^~ /service/ {  proxy_pass http://192.168.60.245:8080/;  proxy_redirect      default;}

参考:

http://div.io/topic/1395


配置文件2


Nginx 配置文件详解


user nginx ;

#用户


worker_processes 8;

#工作进程,根据硬件调整,大于等于cpu核数


error_log logs/nginx_error.log crit;

#错误日志


pid logs/nginx.pid;

#pid放置的位置


worker_rlimit_nofile 204800;

#指定进程可以打开的最大描述符

这个指令是指当一个nginx进程打开的最多文件描述符数目,理论值应该是最多打开文

件数(ulimit -n)与nginx进程数相除,但是nginx分配请求并不是那么均匀,所以最好与ulimit -n 的值保持一致。

现在在linux 2.6内核下开启文件打开数为65535worker_rlimit_nofile就相应应该填写65535

这是因为nginx调度时分配请求到进程并不是那么的均衡,所以假如填写10240,总并发量达到3-4万时就有进程可能超过10240了,这时会返回502错误。


events


{

use epoll;

#使用epollI/O 模型

补充说明:

apache相类,nginx针对不同的操作系统,有不同的事件模型

A)标准事件模型

Selectpoll属于标准事件模型,如果当前系统不存在更有效的方法,nginx会选择selectpoll

B)高效事件模型

Kqueue:使用于FreeBSD 4.1+, OpenBSD 2.9+, NetBSD 2.0 和 MacOS X.使用双处理器的MacOS X系统使用kqueue可能会造成内核崩溃。

Epoll:使用于Linux内核2.6版本及以后的系统。

/dev/poll:使用于Solaris 7 11/99+, HP/UX 11.22+ (eventport), IRIX 6.5.15+ 和 Tru64 UNIX 5.1A+

Eventport:使用于Solaris 10. 为了防止出现内核崩溃的问题, 有必要安装安全补丁



worker_connections 204800;

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

每个进程允许的最多连接数, 理论上每台nginx服务器的最大连接数为worker_processes*worker_connections


keepalive_timeout 60;


keepalive超时时间。


client_header_buffer_size 4k;


客户端请求头部的缓冲区大小,这个可以根据你的系统分页大小来设置,一般一个请求头的大小不会超过1k,不过由于一般系统分页都要大于1k,所以这里设置为分页大小。

分页大小可以用命令getconf PAGESIZE 取得。

[root@web001 ~]# getconf PAGESIZE

4096

但也有client_header_buffer_size超过4k的情况,但是client_header_buffer_size该值必须设置为“系统分页大小”的整倍数。


open_file_cache max=65535 inactive=60s;


这个将为打开文件指定缓存,默认是没有启用的,max指定缓存数量,建议和打开文件数一致,inactive是指经过多长时间文件没被请求后删除缓存。


open_file_cache_valid 80s;


这个是指多长时间检查一次缓存的有效信息。


open_file_cache_min_uses 1;


open_file_cache指令中的inactive参数时间内文件的最少使用次数,如果超过这个数字,文件描述符一直是在缓存中打开的,如上例,如果有一个文件在inactive时间内一次没被使用,它将被移除。



}


#设定http服务器,利用它的反向代理功能提供负载均衡支持

http

{

include mime.types;

#设定mime类型,类型由mime.type文件定义

default_type application/octet-stream;

log_format main '$host $status [$time_local] $remote_addr [$time_local] $request_uri '

'"$http_referer" "$http_user_agent" "$http_x_forwarded_for" '

'$bytes_sent $request_time $sent_http_x_cache_hit';

log_format log404 '$status [$time_local] $remote_addr $host$request_uri $sent_http_location';

$remote_addr$http_x_forwarded_for用以记录客户端的ip地址;

$remote_user:用来记录客户端用户名称;

$time_local: 用来记录访问时间与时区;

$request: 用来记录请求的urlhttp协议;

$status: 用来记录请求状态;成功是200

$body_bytes_s ent :记录发送给客户端文件主体内容大小;

$http_referer:用来记录从那个页面链接访问过来的;

$http_user_agent:记录客户毒啊浏览器的相关信息;

通常web服务器放在反向代理的后面,这样就不能获取到客户的IP地址了,通过$remote_add拿到的IP地址是反向代理服务器的iP地址。反向代理服务器在转发请求的http头信息中,可以增加x_forwarded_for信息,用以记录原有客户端的IP地址和原来客户端的请求的服务器地址;

access_log /dev/null;

#用了log_format指令设置了日志格式之后,需要用access_log指令指定日志文件的存放路径

# access_log /usr/local/nginx/logs/access_log main;

server_names_hash_bucket_size 128;

#保存服务器名字的hash表是由指令server_names_hash_max_size server_names_hash_bucket_size所控制的。参数hash bucket size总是等于hash表的大小,并且是一路处理器缓存大小的倍数。在减少了在内存中的存取次数后,使在处理器中加速查找hash表键值成为可能。如果hash bucket size等于一路处理器缓存的大小,那么在查找键的时候,最坏的情况下在内存中查找的次数为2。第一次是确定存储单元的地址,第二次是在存储单元中查找键 值。因此,如果Nginx给出需要增大hash max size 或 hash bucket size的提示,那么首要的是增大前一个参数的大小.


client_header_buffer_size 4k;

客户端请求头部的缓冲区大小,这个可以根据你的系统分页大小来设置,一般一个请求的头部大小不会超过1k,不过由于一般系统分页都要大于1k,所以这里设置为分页大小。分页大小可以用命令getconf PAGESIZE取得。


large_client_header_buffers 8 128k;

客户请求头缓冲大小
nginx
默认会用client_header_buffer_size这个buffer来读取header值,如果

header过大,它会使用large_client_header_buffers来读取
如果设置过小HTTP/Cookie过大 会报400 错误nginx 400 bad request
求行如果超过buffer,就会报HTTP 414错误(URI Too Long)
nginx
接受最长的HTTP头部大小必须比其中一个buffer大,否则就会报400

HTTP错误(Bad Request)

open_file_cache max 102400

使用字段:http, server, location 这个指令指定缓存是否启用,如果启用,将记录文件以下信息: ·打开的文件描述符,大小信息和修改时间. ·存在的目录信息. ·在搜索文件过程中的错误信息 --没有这个文件,无法正确读取,参考open_file_cache_errors指令选项:
·max -
指定缓存的最大数目,如果缓存溢出,最长使用过的文件(LRU)将被移除
: open_file_cache max=1000 inactive=20s; open_file_cache_valid 30s; open_file_cache_min_uses 2; open_file_cache_errors on;

open_file_cache_errors
语法:open_file_cache_errors on | off 默认值:open_file_cache_errors off 使用字段:http, server, location 这个指令指定是否在搜索一个文件是记录cache错误.

open_file_cache_min_uses

语法:open_file_cache_min_uses number 默认值:open_file_cache_min_uses 1 使用字段:http, server, location 这个指令指定了在open_file_cache指令无效的参数中一定的时间范围内可以使用的最小文件数,如 果使用更大的值,文件描述符在cache中总是打开状态.
open_file_cache_valid

语法:open_file_cache_valid time 默认值:open_file_cache_valid 60 使用字段:http, server, location 这个指令指定了何时需要检查open_file_cache中缓存项目的有效信息.


client_max_body_size 300m;

设定通过nginx上传文件的大小


sendfile on;

#sendfile指令指定 nginx 是否调用sendfile 函数(zero copy 方式)来输出文件,
对于普通应用,必须设为on
如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络IO处理速度,降低系统uptime

tcp_nopush on;

此选项允许或禁止使用sockeTCP_CORK的选项,此选项仅在使用sendfile的时候使用


proxy_connect_timeout 90; 
#
后端服务器连接的超时时间_发起握手等候响应超时时间

proxy_read_timeout 180;

#连接成功后_等候后端服务器响应时间_其实已经进入后端的排队之中等候处理(也可以说是后端服务器处理请求的时间)

proxy_send_timeout 180;

#后端服务器数据回传时间_就是在规定时间之内后端服务器必须传完所有的数据

proxy_buffer_size 256k;

#设置从被代理服务器读取的第一部分应答的缓冲区大小,通常情况下这部分应答中包含一个小的应答头,默认情况下这个值的大小为指令proxy_buffers中指定的一个缓冲区的大小,不过可以将其设置为更小

proxy_buffers 4 256k;

#设置用于读取应答(来自被代理服务器)的缓冲区数目和大小,默认情况也为分页大小,根据操作系统的不同可能是4k或者8k

proxy_busy_buffers_size 256k;


proxy_temp_file_write_size 256k;

#设置在写入proxy_temp_path时数据的大小,预防一个工作进程在传递文件时阻塞太长

proxy_temp_path /data0/proxy_temp_dir;

#proxy_temp_pathproxy_cache_path指定的路径必须在同一分区
proxy_cache_path /data0/proxy_cache_dir levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;
#
设置内存缓存空间大小为200MB1天没有被访问的内容自动清除,硬盘缓存空间大小为30GB

keepalive_timeout 120;

keepalive超时时间。

tcp_nodelay on;

client_body_buffer_size 512k;
如果把它设置为比较大的数值,例如256k,那么,无论使用firefox还是IE浏览器,来提交任意小于256k的图片,都很正常。如果注释该指令,使用默认的client_body_buffer_size设置,也就是操作系统页面大小的两倍,8k或者16k,问题就出现了。
无论使用firefox4.0还是IE8.0,提交一个比较大,200k左右的图片,都返回500 Internal Server Error错误

proxy_intercept_errors on;

表示使nginx阻止HTTP应答代码为400或者更高的应答。


upstream img_relay {

server 127.0.0.1:8027;

server 127.0.0.1:8028;

server 127.0.0.1:8029;

hash $request_uri;

}

nginxupstream目前支持4种方式的分配

1、轮询(默认)

每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。

2weight
指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。
例如:
upstream bakend {
server 192.168.0.14 weight=10;
server 192.168.0.15 weight=10;
}

2ip_hash
每个请求按访问iphash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
例如:
upstream bakend {
ip_hash;
server 192.168.0.14:88;
server 192.168.0.15:80;
}

3fair(第三方)
按后端服务器的响应时间来分配请求,响应时间短的优先分配。
upstream backend {
server server1;
server server2;
fair;
}

4url_hash(第三方)

按访问urlhash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。

例:在upstream中加入hash语句,server语句中不能写入weight等其他的参数,hash_method是使用的hash算法

upstream backend {
server squid1:3128;
server squid2:3128;
hash $request_uri;
hash_method crc32;
}

tips:

upstream bakend{#定义负载均衡设备的Ip及设备状态
ip_hash;
server 127.0.0.1:9090 down;
server 127.0.0.1:8080 weight=2;
server 127.0.0.1:6060;
server 127.0.0.1:7070 backup;
}
在需要使用负载均衡的server中增加
proxy_pass http://bakend/;

每个设备的状态设置为:
1.down
表示单前的server暂时不参与负载
2.weight
默认为1.weight越大,负载的权重就越大。
3.max_fails
:允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream模块定义的错误
4.fail_timeout:max_fails
次失败后,暂停的时间。
5.backup
: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。

nginx支持同时设置多组的负载均衡,用来给不用的server来使用。

client_body_in_file_only设置为On 可以讲client post过来的数据记录到文件中用来做debug
client_body_temp_path
设置记录文件的目录 可以设置最多3层目录

locationURL进行匹配.可以进行重定向或者进行新的代理 负载均衡


server

#配置虚拟机

{

listen 80;

#配置监听端口

server_name image.***.com;

#配置访问域名

location ~* \.(mp3|exe)$ {

#对以“mp3exe”结尾的地址进行负载均衡

proxy_pass http://img_relay$request_uri;

#设置被代理服务器的端口或套接字,以及URL

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

#以上三行,目的是将代理服务器收到的用户的信息传到真实服务器上

}

location /face {

if ($http_user_agent ~* "xnp") {

rewrite ^(.*)$ http://211.151.188.190:8080/face.jpg redirect;

}

proxy_pass http://img_relay$request_uri;

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

error_page 404 502 = @fetch;

}

location @fetch {

access_log /data/logs/face.log log404;

#设定本服务器的访问日志

rewrite ^(.*)$ http://211.151.188.190:8080/face.jpg redirect;

}


location /image {

if ($http_user_agent ~* "xnp") {

rewrite ^(.*)$ http://211.151.188.190:8080/face.jpg redirect;

}

proxy_pass http://img_relay$request_uri;

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

error_page 404 502 = @fetch;

}

location @fetch {

access_log /data/logs/image.log log404;

rewrite ^(.*)$ http://211.151.188.190:8080/face.jpg redirect;

}

}

server

{

listen 80;

server_name *.***.com *.***.cn;

location ~* \.(mp3|exe)$ {

proxy_pass http://img_relay$request_uri;

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

}

location / {

if ($http_user_agent ~* "xnp") {

rewrite ^(.*)$ http://i1.***img.com/help/noimg.gif redirect;

}

proxy_pass http://img_relay$request_uri;

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

#error_page 404 http://i1.***img.com/help/noimg.gif;

error_page 404 502 = @fetch;

}

location @fetch {

access_log /data/logs/baijiaqi.log log404;

rewrite ^(.*)$ http://i1.***img.com/help/noimg.gif redirect;

}

#access_log off;

}


server

{

listen 80;

server_name *.***img.com;


location ~* \.(mp3|exe)$ {

proxy_pass http://img_relay$request_uri;

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

}


location / {

if ($http_user_agent ~* "xnp") {

rewrite ^(.*)$ http://i1.***img.com/help/noimg.gif;

}

proxy_pass http://img_relay$request_uri;

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

#error_page 404 http://i1.***img.com/help/noimg.gif;

error_page 404 = @fetch;

}

#access_log off;

location @fetch {

access_log /data/logs/baijiaqi.log log404;

rewrite ^(.*)$ http://i1.***img.com/help/noimg.gif redirect;

}

}

server

{

listen 8080;

server_name ngx-ha.***img.com;

location / {

stub_status on;

access_log off;

}

}

server {

listen 80;

server_name imgsrc1.***.net;

root html;

}

server {

listen 80;

server_name ***.com w.***.com;

# access_log /usr/local/nginx/logs/access_log main;

location / {

rewrite ^(.*)$ http://www.***.com/ ;

}

}

server {

listen 80;

server_name *******.com w.*******.com;

# access_log /usr/local/nginx/logs/access_log main;

location / {

rewrite ^(.*)$ http://www.*******.com/;

}

}

server {

listen 80;

server_name ******.com;

# access_log /usr/local/nginx/logs/access_log main;

location / {

rewrite ^(.*)$ http://www.******.com/;

}

}

location /NginxStatus {
stub_status on;
access_log on;
auth_basic "NginxStatus";
auth_basic_user_file conf/htpasswd;
}

#设定查看Nginx状态的地址


location ~ /\.ht {
deny all;
}

#禁止访问.htxxx文件

}


注释:变量

Ngx_http_core_module模块支持内置变量,他们的名字和apache的内置变量是一致的。

首先是说明客户请求title中的行,例如$http_user_agent,$http_cookie等等。

此外还有其它的一些变量

$args此变量与请求行中的参数相等

$content_length等于请求行的“Content_Length”的值。

$content_type等同与请求头部的”Content_Type”的值

$document_root等同于当前请求的root指令指定的值

$document_uri$uri一样

$host与请求头部中“Host”行指定的值或是request到达的server的名字(没有Host行)一样

$limit_rate允许限制的连接速率

$request_method等同于requestmethod,通常是“GET”或“POST”

$remote_addr客户端ip

$remote_port客户端port

$remote_user等同于用户名,由ngx_http_auth_basic_module认证

$request_filename当前请求的文件的路径名,由rootaliasURI request组合而成

$request_body_file

$request_uri含有参数的完整的初始URI

$query_string$args一样

$sheeme http模式(http,https)尽在要求是评估例如

Rewrite ^(.+)$ $sheme://example.com$; Redirect;

$server_protocol等同于request的协议,使用“HTTP/或“HTTP/

$server_addr request到达的serverip,一般获得此变量的值的目的是进行系统调用。为了避免系统调用,有必要在listen指令中指明ip,并使用bind参数。

$server_name请求到达的服务器名

$server_port请求到达的服务器的端口号

$uri等同于当前request中的URI,可不同于初始值,例如内部重定向时或使用index


参考:

http://my.oschina.net/duxuefeng/blog/34880

nginx中文维基百科 http://wiki.nginx.org/NginxChs

http://www.queryer.cn/DOC/nginxCHS/index.html


nginx.conf配置3


Nginx配置文件详细说明

在此记录下Nginx服务器nginx.conf的配置文件说明, 部分注释收集与网络.

#运行用户
user www-data;    
#启动进程,通常设置成和cpu的数量相等
worker_processes  1;

#全局错误日志及PID文件
error_log  /var/log/nginx/error.log;
pid        /var/run/nginx.pid;

#工作模式及连接数上限
events {
    use   epoll;             #epoll是多路复用IO(I/O Multiplexing)中的一种方式,但是仅用于linux2.6以上内核,可以大大提高nginx的性能
    worker_connections  1024;#单个后台worker process进程的最大并发链接数
    # multi_accept on; 
}

#设定http服务器,利用它的反向代理功能提供负载均衡支持
http {
     #设定mime类型,类型由mime.type文件定义
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    #设定日志格式
    access_log    /var/log/nginx/access.log;

    #sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,对于普通应用,
    #必须设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,以平衡磁盘与网络I/O处理速度,降低系统的uptime.
    sendfile        on;
    #tcp_nopush     on;

    #连接超时时间
    #keepalive_timeout  0;
    keepalive_timeout  65;
    tcp_nodelay        on;
    
    #开启gzip压缩
    gzip  on;
    gzip_disable "MSIE [1-6]\.(?!.*SV1)";

    #设定请求缓冲
    client_header_buffer_size    1k;
    large_client_header_buffers  4 4k;

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;

    #设定负载均衡的服务器列表
     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 {
    #侦听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自己处理
location ~ ^/(images|javascript|js|css|flash|media|static)/ {
root /var/www/virtual/htdocs;
#过期30天,静态文件不怎么更新,过期可以设大一点,如果频繁更新,则可以设置得小一点。
expires 30d;
}

这个设置里的root /var/www/virtual/htdocs;的含义是什么啊?
是否意味着假如/var/www/virtual/htdocs目录下有个a.jpg的文件,在页面通过http://localhost/a.jpg是可以访问到呢?
但我这样配置以后并访问不到,求楼主指点~~~
  

@eimsteim
可以尝试两种方法
1.location ~ ^/(images|javascript|js|css|flash|media|static)/ 
替换成location ~ .(htm|html|gif|jpg|jpeg|png|ico|rar|css|js|zip|txt|flv|swf|doc|ppt|xls|pdf)$
2.http://localhost/a.jpg 替换成http://localhost/images/a.jpg
refer to http://gitsea.com/2013/05/23/nginx-%E9%85%8D%E7%BD%AE%E8%AF%A6%E8%A7%A3%E4%BB%A5%E5%8F%8A%E4%B8%80%E4%BA%9B%E5%B8%B8%E7%94%A8%E8%AE%BE%E7%BD%AE%E6%8A%80%E5%B7%A7/
  

http://www.cnblogs.com/zhongshengzhen/p/nginx.html

参考:

http://www.cnblogs.com/xiaogangqq123/archive/2011/03/02/1969006.html

https://segmentfault.com/a/1190000002797601



一  nginx 简单配置:

环境:

centos 6.5   lnmp环境

php-fpm 5.3.25

nginx 1.8.1


任务: 从生产服务器上sftp  wwwrootweb下面的目录,把商家server  放到shop里面 、 fapi server里面,是从生产服务器上下载下来的里面的东西全部揉在里面。

需要新建一个WEB网站只是需要重新添加虚拟主机就可以。


第一步:

find / -name *.conf

[root@WIN-P11PIHOSG63 conf.d]# ls
api_server.conf    default.conf  fuwu_chaoshi.conf  mysql.conf  old_server.conf  time.conf

api_server.conf.bk  fapi.conf     fuwu.conf          old_ht.conf  old_shop.conf

cp  api_server.conf  old_shop.conf   old_server.conf

修改端口 号,修改主机(也可以)ifconfig eth0:0方式


[root@WIN-P11PIHOSG63 conf.d]# 


/usr/local/nginx/conf/conf.d/*.conf




server {
        listen  85;
        server_name  192.168.199.40;


        location / {
            root   /wwwroot/fapi.jishibang.cc/jishibang_fuwu;
            index  index.php index.html index.htm;
                if (!-e $request_filename) {
                 rewrite  ^(.*)$  /index.php?s=$1  last;
                }
        }


        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }


        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /wwwroot/fapi.jishibang.cc/jishibang_fuwu/$fastcgi_script_name;
            include        fastcgi_params;


        }


        location ~ /\.ht {
            deny  all;
        }
}


vim /usr/local/nginx/conf/conf.d

  old_server.conf

[root@WIN-P11PIHOSG63 conf.d]# cat old_server.conf 
server {
        listen  91;
        server_name  192.168.199.40;


        location / {
            root   /wwwroot/oldserver.jishibang.cc;
            index  index.php index.html index.htm;
                if (!-e $request_filename) {
                 rewrite  ^(.*)$  /index.php?s=$1  last;
                }
        }


        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }


        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /wwwroot/oldserver.jishibang.cc/$fastcgi_script_name;
            include        fastcgi_params;


        }


        location ~ /\.ht {
            deny  all;
        }
}
[root@WIN-P11PIHOSG63 conf.d]# 


cat 

[root@WIN-P11PIHOSG63 conf.d]# cat old_shop.conf 
server {
        listen  90;
        server_name  192.168.199.40;


        location / {
            root   /wwwroot/oldshop.jishibang.cc;
            index  index.php index.html index.htm;
                if (!-e $request_filename) {
                 rewrite  ^(.*)$  /index.php?s=$1  last;
                }
        }


        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }


        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /wwwroot/oldshop.jishibang.cc/$fastcgi_script_name;
            include        fastcgi_params;


        }


        location ~ /\.ht {
            deny  all;
        }
}
[root@WIN-P11PIHOSG63 conf.d]# 


第二步 把从生产服务器上的目录里的内容放到 如下目录即可。


mkdir  /wwwroot/oldserver.jish.cc 下 放shop内容

mkdir /wwwroot/oldshop.jish.cc 下 放server内容


第三步 输入 192.168.199.40、就可以了解析了!





0 0
原创粉丝点击