Nginx配置文件解析

Nginx的功能特性

  • 基本Http服务,可以作为Http代理服务器和反向代理服务器,支持通过缓存加速访问,可以完成简单的负载均衡和容错,支持包过滤功能,支持SSL
  • 高级Http服务,可以进行自定义配置,支持虚拟主机,支持URL重定向,支持网络监控,支持流媒体传输等
  • 邮件代理服务器,支持IMAP/POP3代理服务功能,支持内部SMTP代理服务功能

基本HTPP服务

  • 处理静态文件,如HTML网页以及请求
  • 打开并自行管理文件描述符缓存
  • 提供反向代理服务,并且可用缓存加速反向代理
  • 提供远程FastCGI(通用网关接口)服务的缓存机制
  • 模块化提供过滤器功能,gzip压缩 ranges支持 chunked响应 XSLT SSI 图像缩放 针对包含多个SSI的页面,经由反向代理SSI过滤器可并行处理
  • 支持安全套接协议SSL

高级HTTP服务

  • 支持基于名字和IP的虚拟主机设置
  • 支持HTTP/1.0 中的keep-alive模式和管线模型(PipelLined)连接
  • 支持重新升级时,无需中断正在处理的请求
  • 自定义访问日志格式、带缓存的日志写操作以及快速日志轮转
  • 提供3xx 5xx错误代码重定向功能
  • 支持重写模块扩展
  • 支持HTTP DAV模块,从而为HTTP DAV模块提供PUT DELETE MKCOL COPY MOVE方法
  • 支持FLV流和MP4流传输
  • 支持网络监控,访问控制、速度限制或请求限制等
  • 支持嵌入perl语言

邮件代理服务

  • 支持使用外部HTTP认证服务器重定向用户到IMAP/POP3后端,并支持IMAP认证方式和POP3认证方式
  • 支持使用外部HTTP认证服务器器认证用户后重定向连接到内部SMTP后端,并支持SMTP认证方式
  • 支持邮件代理服务下的安全套接层安全协议SSL
  • 支持纯文本通信协议的扩展协议STARTTLS

常用功能

HTTP代理和反向代理

请求稳定,后端转发与业务配合分离,配置灵活,支持判断表达式

负载均衡

  1. 将单一的重负载分担到多个网络节点上做并行处理,每个节点处理结束后将结果汇总给用户提高网络系统的处理能力
  2. 将大量的前端并发访问或者数据流量分担到多个后端网络节点上分别处理
  • nginx的负载均衡策略
  • 内置策略:轮询 加权轮询 IP HASH
  • 扩展策略 主要通过第三方模块实现,常见有url hash

WEB缓存

主要由Proxy_Cache(提供代理服务时,对后端服务器返回内容进行URL缓存) 和FastCGI_Cache(对FastCGI程序缓存)指令集构成


配置文件解析

nginx.conf文件结构

主要由三块组成 全局块 events块 http块,http块中包含http全局块和多个server块,每个server块中包含server全局块和多个location块,采用就近原则生效


全局块

配置一些影响整体运行的指令,通常有服务器用户组 允许生成的worker process nginx.pid存放路径 日志存放路径 类型 配置文件引入

events块

影响nginx服务器与用户的网络连接,这一块的设置对服务的性能影响较大。通常有是否开启对WP下的网络进行序列化 是否允许同时接受多个网络连接 事件驱动模型 每个WP可以同时支持处理的最大连接数

http块

重要组成部分,代理、缓存和日志定义、第三方模块的配置。 通常配置 文件引入 MIME-TYPE定义 日志自定义 是否使用sendfile传输文件 连接超时时间 单连接请求数上限

具体配置

由于nginx的配置大部分是不限定具体位置的(可以在多个地方配置),所以此处将依次进行列举

# 配置alleyz用户  admin组下有启停权限
user alleyz [admin];

#所有用户具有启停权限
user nobody nobody;

配置运行生成的worker process
# 如果设置为数字,启动以后就具有多少个worker process
# 设置为auto则自动检测
worker_processes number | auto;

配置pid文件存放路径
默认存放在logs/nginx.pid

#必须包含文件名称
pid logs/nginx_alleyz.pid;

配置错误日志文件的存放路径
可在全局块、http块 server块 location块配置

error_log file | stderr [ DEBUG | info | notice | warn | error | crit | alert | emerg];

配置文件引入 任意地方
include file; 

设置网络连接的序列化
惊群问题当某刻只有一个请求进来时,会唤醒多个睡眠的进程,造成性能的损耗

accept_mutex on | off;

设置是否允许同事接受多个连接 events
multi_accept on |  off; #默认关闭

事件驱动模型的选择 events
可选择的内容有:select poll kqueue epoll rtsig /dev/poll eventport

use epoll;

配置最大连接数 events
允许每一个WP同事开启的最大连接数。此值不能大于操作系统支持的打开的最大文件句柄数.

worker_connections number; #默认值 512

MIME-Type http server location
default_type mime-type; #默认为 text/plain

自定义服务日志
access_log 可在http server location中配置; log_format只能在http中配置。 
注意 前文提到的error_log 配置的是nginx的运行日志,此处指的是应答前端请求的服务日志。可以对日志的格式、大小、输出进行配置,有access_log log_format指令, log_format 的string整体需要用'括起来,变量名称使用双引号括起来

log_format name string;
access_log path [format [buffer=size]];


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;

#如果取消日志记录
access_log off;

配置sendfile方式传输文件
相关指令 sendfile sendfile_max_chunk,默认开启

sendfile on | off;
sendfile_max_chunk size; #设置为0为限制

配置连接超时时间
http块 server块 location块,设置用户会话连接的保持时间

keepalive_timeout timeout[header_timeout];

单连接请求上限
server location 用户与服务端建立连接后,通过此连接发送的请求次数

keepalive_requests number;

配置网络监听
配置监听IP地址
listen address[:port] [default_server] [setfib=number] [backlog=number] 
        [rcvbuf=size] [sendbuf=size] [deferred] [accept_filter=filter] [bind] [ssl];

配置监听端口
listen port [default_server] [setfib=number] [backlog=number] [rcvbuf=size] 
        [sendbuf=size] [accept_filter=filter] [deferred] [bind] [ipv6only=on | off] [ssl];

配置Unix Domain Socket 很少用
listen unix:path [default_server] [setfib=number] [backlog=number] [rcvbuf=size] 
        [sendbuf=size] [accept_filter=filter] [deferred] [bind] [ssl];

平常使用:

listen 3021;
listen 10.8.177.21;
listen 10.8.177.21:3022 default_server backlog==1024;

参数说明: 
- address ip地址,如果是ipv6需要使用[fe13:..]格式 
- port 端口号,如果只有ip则默认80 
- path socket文件路径 
- default_server, 标识符,将ip:port设置为默认的虚拟主机 
- backlog 设置监听函数listen()最多允许多少网络连接同时处于挂起的状态,默认511(FreeBSD 为-1) 
- rcvbuf 设置监听socket接受缓存区大小 
- sendbuf 设置监听socket发送缓存区大小 
- deferred 标识符,将accept()设置为Deferred模式 
- bind 标识符,使用独立的bind()处理此adress:port,一般情况下端口相同而IP地址不同,只使用一个 
- ssl 标识符,设置会话使用ssl模式(https) 
- accept_filter 设置监听端口的请求过滤,被过滤的内容不能被接受和处理(只在FreeBSD NetBSD中有用) 
- setfib 为监听socket关联路由表,只对FreeBSD起作用

基于名称的虚机主机设置
可以配置多个name
server_name name name1 name2 ....;

可以使用通配符* 只能位于三段字符串组成的首尾或者两段字符串的尾部
server_name *.alleyz.com www.alleyz.*

使用正则表达式,~作为正则开始的标记,并且正则支持捕获
server_name ~([a-zA-Z\d]{1,4})\.alleyz.com$;

此时,如果通过xisuo.alleyz.com访问的话,可使用$1捕获xisuo; 
一个名称若被多次匹配的访问优先级: 
- 匹配方式不同时 
1. 精确匹配 
2. 通配符在开始 
3. 通配符在结尾 
4. 正则表达式匹配 
- 相同匹配方式时,首次处理优先(顺序)

基于IP的虚拟主机设置
需设置网卡能监听多个IP地址

# 临时生效
ifconfig em1:0 10.8.177.21 netmask 255.255.0.0 up
ifconfig em1:1 10.8.177.22 netmask 255.255.0.0 up
# 永久生效
echo "ifconfig em1:0 10.8.177.21 netmask 255.255.0.0 up" >> /etc/rs.local

# vi nginx.conf
...
server {
  listen       3022;
  server_name  10.8.177.32;
  ...
}
server {
  listen       3022;
  server_name  10.8.177.21;
  ...
}
kill -HUP `cat ../logs/nginx_alleyz.pid`

配置location块
location [ = | ~ | ~* | ^~] uri {...}
1
=用于普通uri之前,表示严格匹配
~ uri包含正则表达式,并且区分大小写
~* 表示包含正则表达式,并且不区分大小写
^~ 如果找到与uri匹配度最高的location,立即处理请求。 会对uri进行反编码
配置请求的根目录 http server location
服务器收到请求后查找资源的根目录路径,可以使用nginx预设的大多变量,唯$document_root $realpath_root不能使用;通常在location块中使用。

root path;

更改location的URI
除了使用root指定根目录,还可以使用alias指令改变location接收到的请求路径

alias path;

location ~ ^/data/(.+\.(htm|html))${
    alias html/data/other/$1;
}

设置网站的默认首页
可以针对不同的访问设置不同的首页

index index.html index.htm;

设置网站的错误页面 http server location
如果uri是path的话,则path是以nginx的安装目录为根路径的;

error_page code ... [=[response]] uri;
error_page 404 /404.html; 
error_page 404 http://someone.com/404.html;

如果希望错误路径指向自定的位置:

error_page 404 /404.html
location /404.html{
    root /home/alleyz/html;
}

基于IP配置访问权限 http server location
allow address | CIDR | all;
deny address | CIDR | all;

location / {
    root html;
    index index.html;
    deny 10.8.177.26;
}

基于密码配置访问权限
基于HTTP Basic Authentication 协议认证;文件中密码得加密!

# 生成密文,-c创建密码文件 -d采用crypt加密 -b 命令行指定密码
htpasswd -cdb  passwd alleyz 123456

# string 开启认证功能,并设置提示信息
auth_basic string | off;

#file 包含用户名信息的文件路径
auth_basic_user_file file;

我自己的测试配置
user  alleyz;
worker_processes  2;

pid        logs/nginx_alleyz.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main1  '$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  main1;

    sendfile        on;

    keepalive_timeout  65;

    server {
        listen       3022;
        server_name  10.8.177.32;

        deny 10.8.177.26;
        location / {
            auth_basic "it`s auth test msg!";
            auth_basic_user_file passwd;
            root   html/22;
            index  index.html index.htm;
        }

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

    }



    server {
        listen       3022;
        server_name  10.8.177.21;


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

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

    }
}

一.正则表达式匹配,其中:

~ 为区分大小写匹配
~* 为不区分大小写匹配
!~和!~*分别为区分大小写不匹配及不区分大小写不匹配

二.文件及目录匹配,其中:

 -f和!-f用来判断是否存在文件
 -d和!-d用来判断是否存在目录
 -e和!-e用来判断是否存在文件或目录
 -x和!-x用来判断文件是否可执行

三.rewrite指令的最后一项参数为flag标记,flag标记有:

last 相当于apache里面的[L]标记,表示rewrite。
break本条规则匹配完成后,终止匹配,不再匹配后面的规则。
redirect 返回302临时重定向,浏览器地址会显示跳转后的URL地址。
permanent 返回301永久重定向,浏览器地址会显示跳转后的URL地址。
使用last和break实现URI重写,浏览器地址栏不变。
使用alias指令必须用last标记;
使用proxy_pass指令时,需要使用break标记。
Last标记在本条rewrite规则执行完毕后,会对其所在server{......}标签重新发起请求
break标记则在本条规则匹配完成后,终止匹配。

四.NginxRewrite 规则相关指令

    · break指令
    使用环境:server,location,if;
    该指令的作用是完成当前的规则集,不再处理rewrite指令。
    · if指令
    使用环境:server,location
    该指令用于检查一个条件是否符合,如果条件符合,则执行大括号内的语句。If指令不支持嵌套,不支持多个条件&&和||处理。
    · return指令
      语法:return code ;
      使用环境:server,location,if;
      该指令用于结束规则的执行并返回状态码给客户端。
      示例:如果访问的URL以".sh"或".bash"结尾,则返回403状态码
      location ~ .*\.(sh|bash)?$
      {
        return 403;
      }
    · rewrite 指令
      语法:rewriteregex replacement flag
      使用环境:server,location,if
      该指令根据表达式来重定向URI,或者修改字符串。指令根据配置文件中的顺序来执行。注意重写表达式只对相对路径有效。如果你想配对主机名,你应该使用if语句,示例如下:
      if ( $host ~* www\.(.*) )
      {
        set  $host_without_www $1;
        rewrite  ^(.*)$   http://$host_without_www$1 permanent;
      }
    · Set指令
      语法:setvariable value ; 默认值:none; 使用环境:server,location,if;
      该指令用于定义一个变量,并给变量赋值。变量的值可以为文本、变量以及文本变量的联合。
      示例:set $varname "hello world";
    · Uninitialized_variable_warn指令
      语法:uninitialized_variable_warnon|off
      使用环境:http,server,location,if
      该指令用于开启和关闭未初始化变量的警告信息,默认值为开启。

五.Nginx的Rewrite规则编写实例

  • 当访问的文件和目录不存在时,重定向到某个php文件
        if ( !-e $request_filename )     {       rewrite ^/(.*)$ index.php last;     }

  • 目录对换 /123456/xxxx ====> /xxxx?id=123456
        rewrite ^/(\d+)/(.+)/ /$2?id=$1 last;

  • 如果客户端使用的是IE浏览器,则重定向到/ie目录下
        if( $http_user_agent ~ MSIE)     {       rewrite ^(.*)$ /ie/$1 break;     }

  • 禁止访问多个目录
        location ~ ^/(cron|templates)/     {       deny all;       break;     }

  • 禁止访问以/data开头的文件
        location ~ ^/data     {       deny all;     }

  • 禁止访问以.sh,.flv,.mp3为文件后缀名的文件
        location ~ .*\.(sh|flv|mp3)$     {       return 403;     }

  • 设置某些类型文件的浏览器缓存时间
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$     {       expires 30d;     }     location ~ .*\.(js|css)$     {       expires 1h;     }

  • 给favicon.ico和robots.txt设置过期时间;
      这里为favicon.ico为99天,robots.txt为7天并不记录404错误日志
      location ~(favicon.ico)   {     log_not_found off;     expires 99d;     break;   }   location ~(robots.txt)   {     log_not_found off;     expires 7d;     break;   }

  • 设定某个文件的过期时间;这里为600秒,并不记录访问日志
      location ^~ /html/scripts/loadhead_1.js   {     access_log off;     root /opt/lampp/htdocs/web;     expires 600;     break;   }

  • 文件反盗链并设置过期时间
      这里的return412 为自定义的http状态码,默认为403,方便找出正确的盗链的请求   “rewrite ^/ http://img.linuxidc.net/leech.gif;” 显示一张防盗链图片   “access_log off;” 不记录访问日志,减轻压力   “expires 3d” 所有文件3天的浏览器缓存   location ~*^.+\.(jpg|jpeg|gif|png|swf|rar|zip|css|js)$   {     valid_referers none blocked *.linuxidc.com*.linuxidc.net localhost 208.97.167.194;     if ($invalid_referer)     {       rewrite ^/ http://img.linuxidc.net/leech.gif;       return 412;       break;     }     access_log off;     root /opt/lampp/htdocs/web;     expires 3d;     break;   }

  • 只允许固定ip访问网站,并加上密码
      root /opt/htdocs/www;   allow 208.97.167.194;   allow 222.33.1.2;   allow 231.152.49.4;   deny all;   auth_basic “C1G_ADMIN”;   auth_basic_user_file htpasswd;

  • 将多级目录下的文件转成一个文件,增强seo效果
      /job-123-456-789.html 指向/job/123/456/789.html   rewrite ^/job-([0-9]+)-([0-9]+)-([0-9]+)\.html$ /job/$1/$2/jobshow_$3.html last;

  • 文件和目录不存在的时候重定向:
      if (!-e $request_filename)    {     proxy_pass http://127.0.0.1;   }

  • 将根目录下某个文件夹指向2级目录
      如/shanghaijob/ 指向 /area/shanghai/
      如果你将last改成permanent,那么浏览器地址栏显是/location/shanghai/
      rewrite ^/([0-9a-z]+)job/(.)$ /area/$1/$2last;
      上面例子有个问题是访问/shanghai时将不会匹配
      rewrite ^/([0-9a-z]+)job$ /area/$1/ last;
      rewrite ^/([0-9a-z]+)job/(.
    )$ /area/$1/$2last;
      这样/shanghai 也可以访问了,但页面中的相对链接无法使用,
      如./list_1.html真实地址是/area/shanghia/list_1.html会变成/list_1.html,导至无法访问。
      那我加上自动跳转也是不行咯
      (-d $request_filename)它有个条件是必需为真实目录,而我的rewrite不是的,所以没有效果   if (-d $request_filename)   {     rewrite ^/(.*)([^/])$ http://$host/$1$2/permanent;   }   知道原因后就好办了,让我手动跳转吧   rewrite ^/([0-9a-z]+)job$ /$1job/permanent;   rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2last;

  • 域名跳转
      server   {     listen 80;     server_name jump.linuxidc.com;     index index.html index.htm index.php;     root /opt/lampp/htdocs/www;     rewrite ^/ http://www.linuxidc.com/;     access_log off;   }

  • 多域名转向
      server_name www.linuxidc.com www.linuxidc.net;   index index.html index.htm index.php;   root /opt/lampp/htdocs;   if ($host ~ "linuxidc\.net") {     rewrite ^(.*) http://www.linuxidc.com$1permanent;   }

六.nginx全局变量

  • arg_PARAMETER #这个变量包含GET请求中,如果有变量PARAMETER时的值。
  • args #这个变量等于请求行中(GET请求)的参数,如:foo=123&bar=blahblah;
  • binary_remote_addr #二进制的客户地址。
  • body_bytes_sent #响应时送出的body字节数数量。即使连接中断,这个数据也是精确的。
  • content_length #请求头中的Content-length字段。
  • content_type #请求头中的Content-Type字段。
  • cookie_COOKIE #cookie COOKIE变量的值
  • document_root #当前请求在root指令中指定的值。
  • document_uri #与uri相同。
  • host #请求主机头字段,否则为服务器名称。
  • hostname #Set to themachine’s hostname as returned by gethostname
  • http_HEADER
  • is_args #如果有args参数,这个变量等于”?”,否则等于”",空值。
  • http_user_agent #客户端agent信息
  • http_cookie #客户端cookie信息
  • limit_rate #这个变量可以限制连接速率。
  • query_string #与args相同。
  • request_body_file #客户端请求主体信息的临时文件名。
  • request_method #客户端请求的动作,通常为GET或POST。
  • remote_addr #客户端的IP地址。
  • remote_port #客户端的端口。
  • remote_user #已经经过Auth Basic Module验证的用户名。
  • request_completion #如果请求结束,设置为OK. 当请求未结束或如果该请求不是请求链串的最后一个时,为空(Empty)。
  • request_method #GET或POST
  • request_filename #当前请求的文件路径,由root或alias指令与URI请求生成。
  • request_uri #包含请求参数的原始URI,不包含主机名,如:”/foo/bar.php?arg=baz”。不能修改。
  • scheme #HTTP方法(如http,https)。
  • server_protocol #请求使用的协议,通常是HTTP/1.0或HTTP/1.1。
  • server_addr #服务器地址,在完成一次系统调用后可以确定这个值。
  • server_name #服务器名称。
  • server_port #请求到达服务器的端口号。

七.Apache和Nginx规则的对应关系

Apache的RewriteCond对应Nginx的if
Apache的RewriteRule对应Nginx的rewrite
Apache的[R]对应Nginx的redirect
Apache的[P]对应Nginx的last
Apache的[R,L]对应Nginx的redirect
Apache的[P,L]对应Nginx的last
Apache的[PT,L]对应Nginx的last

例如:允许指定的域名访问本站,其他的域名一律转向www.linuxidc.net
Apache:

RewriteCond %{HTTP_HOST} !^(.*?)\.aaa\.com$[NC]
RewriteCond %{HTTP_HOST} !^localhost$ 
RewriteCond %{HTTP_HOST}!^192\.168\.0\.(.*?)$
RewriteRule ^/(.*)$ http://www.linuxidc.net[R,L]

Nginx:

if( $host ~* ^(.*)\.aaa\.com$ )
{
  set $allowHost '1';
}
if( $host ~* ^localhost )
{
  set $allowHost '1';
}
if( $host ~* ^192\.168\.1\.(.*?)$ )
{
  set $allowHost '1';
}
if( $allowHost !~ '1' )
{
  rewrite ^/(.*)$ http://www.linuxidc.netredirect ;
}

nginx conf 配置文件

#运行用户
user www-data;    

#nginx进程数,建议设置为等于CPU总核心数
worker_processes 8;

#全局错误日志定义类型,[ debug | info | notice | warn | error | crit ]
error_log /var/log/nginx/error.log info;

#进程文件
pid /var/run/nginx.pid;

#一个nginx进程打开的最多文件描述符数目,理论值应该是最多打开文件数(系统的值ulimit -n)与nginx进程数相除,但是nginx分配请求并不均匀,所以建议与ulimit -n的值保持一致。
worker_rlimit_nofile 65535;

工作模式与连接数上限
events
{
  #参考事件模型,use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]; epoll模型是Linux 2.6以上版本内核中的高性能网络I/O模型,如果跑在FreeBSD上面,就用kqueue模型。
  use epoll;
  
  #单个进程最大连接数(最大连接数=连接数*进程数),单个后台worker process进程的最大并发链接数
  worker_connections 65535;
}

#设定http服务器,利用它的反向代理功能提供负载均衡支持
http
{
    #设定mime类型,类型由mime.type文件定义
    include mime.types; #文件扩展名与文件类型映射表
    default_type application/octet-stream; #默认文件类型
    
    #设定日志格式
    access_log    /var/log/nginx/access.log;
    
    #charset utf-8; #默认编码
    
    server_names_hash_bucket_size 128; #服务器名字的hash表大小
    client_header_buffer_size 32k; #上传文件大小限制
    large_client_header_buffers 4 64k; #设定请求缓
    client_max_body_size 8m; #设定请求缓
    
    #开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常把这个改成off。
    sendfile on; 
        
    
    autoindex on; #开启目录列表访问,合适下载服务器,默认关闭。
    tcp_nopush on; #防止网络阻塞
    tcp_nodelay on; #防止网络阻塞
    keepalive_timeout 120; #长连接超时时间,单位是秒
    
    #FastCGI相关参数是为了改善网站的性能:减少资源占用,提高访问速度。下面参数看字面意思都能理解。
    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    fastcgi_buffer_size 64k;
    fastcgi_buffers 4 64k;
    fastcgi_busy_buffers_size 128k;
    fastcgi_temp_file_write_size 128k;
    
    #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; #压缩等级
    gzip_types text/plain application/x-javascript text/css application/xml;
    #压缩类型,默认就已经包含text/html,所以下面就不用再写了,写上去也不会有问题,但是会有一个warn。
    gzip_vary on;
    #limit_zone crawler $binary_remote_addr 10m; #开启限制IP连接数的时候需要使用

#设定负载均衡的服务器列表
upstream blog.ha97.com {
    #upstream的负载均衡,weight是权重,可以根据机器配置定义权重。weigth参数表示权值,权值越高被分配到的几率越大。
    server 192.168.80.121:80 weight=3;
    server 192.168.80.122:80 weight=2;
    server 192.168.80.123:80 weight=3;

}

虚拟主机的配置
server
{
    listen 80;    #监听端口
    server_name aa.cn www.aa.cn ; #server_name end  #域名可以有多个,用空格隔开
    index index.html index.htm index.php;  # 设置访问主页
    set $subdomain '';  # 绑定目录为二级域名 bbb.aa.com  根目录 /bbb  文件夹
    if ( $host ~* "(?:(\w+\.){0,})(\b(?!www\b)\w+)\.\b(?!(com|org|gov|net|cn)\b)\w+\.[a-zA-Z]+" ) { set $subdomain "/$2"; }

    root /home/wwwroot/aa.cn/web$subdomain;# 访问域名跟目录
      
    include rewrite/dedecms.conf; #rewrite end   #载入其他配置文件
    
    location ~ .*.(php|php5)?$
        {
          fastcgi_pass 127.0.0.1:9000;
          fastcgi_index index.php;
          include fastcgi.conf;
        }
    #图片缓存时间设置
    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/ha97access.log access;

#对 "/" 启用反向代理
location / {

    proxy_pass http://127.0.0.1:88;
    proxy_redirect off;
    proxy_set_header X-Real-IP $remote_addr;
    #后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
    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服务器传
}

#设定查看Nginx状态的地址
location /NginxStatus {
    stub_status on;
    access_log on;
    auth_basic "NginxStatus";
    auth_basic_user_file conf/htpasswd;
    #htpasswd文件的内容可以用apache提供的htpasswd工具来产生。
}

#本地动静分离反向代理配置
#所有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或resin
location ~ .*.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$
{   expires 15d;   }
location ~ .*.(js|css)?$
{   expires 1h;   }

}

#禁止访问 .htxxx 文件
location ~ /\.ht {
        deny all;
}


nginx 在thinkphp 的url 重写

在/usr/local/nginx/conf/vhost/你的域名配置文件 中添加
location / {
  if (!-e $request_filename) {
  rewrite ^/(.*)/(.*)/(.*)/*$ /index.php?m=$1&c=$2&a=$3 last;  # thinkphp 的配置文件中 'URL_MODEL' => 1   PATHINFO模式

       #或者  rewrite  ^(.*)$  /index.php?s=$1  last;     # thinkphp 的配置文件中 'URL_MODEL' =>3   兼容模式

       #或者  rewrite  /(.*)$  /index.php/$1  last;          # thinkphp 的配置文件中 'URL_MODEL' => 2  REWRITE模式

  break;
  }
}

路径 pathinfo 模式[ thinkphp ] 添加
location ~ \.php(.*)$ {
  fastcgi_pass 127.0.0.1:9000;
  fastcgi_index index.php;
  fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  fastcgi_param PATH_INFO $fastcgi_path_info;
  fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
  include fastcgi_params;
}

重写 url +省略index.php
location / {

  try_files $uri /index.php?$uri;

}

nginx -s reload 或者 /usr/local/nginx/sbin/nginx -s reload 重新加载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服务器传

       }

     }
}