您的位置:首页 > 运维架构 > Nginx

Nginx基础配置

2016-10-30 22:46 351 查看




一、主配置文件结构

main block;#全局块配置全局生效

event{
#事件驱动相关配置
}

http{

#http/https协议相关配置段
server {
...
}:#每个server用于定义一个虚拟主机;
server {
...
server_name
root
alias
location [OPERATOR] URL {
...
if CONDITION {
...
}
}
}
}
stream{
#tcp协议配置段
}
1.全局块全局块主默认的配置文件从开始到event块之间的的一部分内容,主要设置影响Nginx整个配置指令,影响全局;2.event块event块主要涉及到的指令用于响应nginx服务器与用户的网络连接,常用于配置时间驱动等模块信息;3.http块http块是nginx配置文件中最为重要的块,http自己的全局块,包括大多数第三方模块配置也可以添加到这个模块中,server块也可以包含在这个快中,4.server块主要定义虚拟主机5.location块每个server块中可以包含多个location块,location块主要用于nginx服务器接收来自客户端的请求的URL字符串进行匹配处理,nginx许多功能都是在此进行配置的。

二、Nginx正常运行必备的配置:

user nobody;
pid /var/log/nginx/nginx.pid;
worker_processes auto;
worker_rlimit_nofile 1024;
master_process on;
error_log /var/log/nginx/error.log;
--------------------------------------------------------------------------------------------------------------------------
events {
worker_connections 1024;
use epoll;
accept_mutex on;
}
--------------------------------------------------------------------------------------------------------------------------
http{
include       mime.types;
default_type  application/octet-stream;
keepalive_timeout  65;
gzip  on;
server {
listen 80;
server_name app.liaoxz.com;
root   /usr/local/nginx/html/app/;
index  index.html index.htm;
}
server {
listen 8090;
server_name abb.liaoxz.com;
root /usr/local/nginx/html/abb/;
index index.html;
}
}
cpu优化相关参数1.设置worker process 数
worker process是nginx实现高并发的主要关键配置,这个建议与cpu核心数一致,配置work proccess的语法格式:worker_processes number;
2.worker_cpu_affinity cpumask ...;worker_cpu_affinity auto [cpumask];将worker进程与cpu进行绑定(绑定之后来自同一worker个进程的请求就会直接使用当前所绑定所指定的cpu)建议设置成auto,auto表示在服务启动时就将worker进程与之绑定。
用户及进程相关1.user uesr [group]设置工作进程所使用的用户或组2.pid /PATH/TO/PID_FILE;指定存储nginx主进程进程号码的文件路径;3.worker_priority number;指定worker进程的nice值,设定worker进程优先级;[-20,20]4.worker_rlimit_nofile number;worker进程所能够打开的文件数量上限;5.master_process on |off;是否以master/worker模型运行nginx
模块装载
1.include file | mask;指明包含进来的其它配置文件片断;2.load_module file;指明要装载的动态模块;
事件驱动相关events{1)worker_cnnections number;每个worker进程所能打开的最大并发连接数
2)use method epoll|select|poll;指明并发连接请求的处理方法;建议使用epolluse epoll;
3)accept_mutex on |off;处理新的连接请求方法;on意味着由各worker轮流处理新请求,off意味着每个新的请求会通知所有的worker进程}

三、Nginx Gzip压缩

Gzip功能由模块 ngx_http_gzip_module提供
官方站点介绍:http://nginx.org/en/docs/http/ngx_http_gzip_module.html
1.Syntax:gzip on | off;
Default:
gzip off;
Context:http, server, location, if in location
默认是off,不启动Gzip功能,设置为on为生效

2.Syntax:gzip_buffers number size;
Default:
gzip_buffers 32 4k|16 8k;
Context:http, server, location
用于指定缓冲区数量及每个缓存区的大小
number 缓冲区数量
size 缓存区大小

3.Syntax:gzip_comp_level level;
Default:
gzip_comp_level 1;
Context:http, server, location
指定压缩程度数字越高表示压缩效率越高,但是占用系统资源,建议适当设置

4.Syntax:gzip_disable regex ...;
Default:—
Context:http, server, location
This directive appeared in version 0.6.23.
针对不同类型客户端进行选择是否开启gzip功能
regex 表示浏览器类型 支持使用正则表达式
IE浏览器 MSIE [6-10]\;

5.Syntax:gzip_types mime-type ...;
Default:
gzip_types text/html;
Context:http, server, location
压缩过滤器,仅对此处设定的MIME类型的内容启用压缩功能;

6.Syntax:gzip_proxied off | expired | no-cache | no-store | private | no_last_modified | no_etag | auth | any ...;
Default:
gzip_proxied off;
Context:http, server, location
Enables or disables gzipping of responses for proxied requests depending on the request and response. The fact that the request is proxied is determined by the presence of the “Via” request header field. The directive accepts multiple parameters:

off
disables compression for all proxied requests, ignoring other parameters;
expired
enables compression if a response header includes the “Expires” field with a value that disables caching;
no-cache
enables compression if a response header includes the “Cache-Control” field with the “no-cache” parameter;
no-store
enables compression if a response header includes the “Cache-Control” field with the “no-store” parameter;
private
enables compression if a response header includes the “Cache-Control” field with the “private” parameter;
no_last_modified
enables compression if a response header does not include the “Last-Modified” field;
no_etag
enables compression if a response header does not include the “ETag” field;
auth
enables compression if a request header includes the “Authorization” field;
any
enables compression for all proxied requests.

nginx作为代理服务器接收到从被代理服务器发送的响应报文后,在何种条件下启用压缩功能的;
off:对代理的请求不启用
no-cache, no-store,private:表示从被代理服务器收到的响应报文首部的Cache-Control的值为此三者中任何一个,则启用压缩功能;
其他相关请查阅的官方文档http://nginx.org/en/docs/http/ngx_http_gzip_module.html#gzip_disable

http{

include       mime.types;
default_type  application/octet-stream;
keepalive_timeout  65;
gzip  on;
gzip_comp_level 4;
gzip_disable MISE [4-6]\;
gzip_buffers 32 4k;
}

四、nginx rewrite功能配置

Rewrite 是nginx提供的一个重要功能,其在web服务器中必然会使用到的指令,例如在网站结构更改后,客户端任可以使用之前使用的URL来进行访问等操作,实现URL替换功能;

Rewrite功能是由ngx_http_rewrite_module模块提供;

1、rewrite regex replacement [flag]将用户请求的URI基于regex所描述的模式进行检查,匹配到时将其替换为replacement指定的新的URI;注意:如果在同一级配置块中存在多个rewrite规则,那么会自下而下逐个检查;被某条件规则替换完成后,会重新一轮的替换检查,因此,隐含有循环机制;[flag]所表示的标志位用于控制此循环机制;如果replacement是以http://或https://开头,则替换结果会直接以重向返回给客户端;301:永久重定向;[flag]:last:重写完成后停止对当前URI在当前location中后续的其它重写操作,而后对新的URI启动新一轮重写检查;提前重启新一轮循环;
break:重写完成后停止对当前URI在当前location中后续的其它重写操作,而后直接跳转至重写规则配置块之后的其它配置;结束循环;
redirect:重写完成后以临时重定向方式直接返回重写后生成的新URI给客户端,由客户端重新发起请求;不能以http://或https://开头;
permanent:重写完成后以永久重定向方式直接返回重写后生成的新URI给客户端,由客户端重新发起请求;
location / { rewrite ^/ http://abb.liaoxz.com/abb; root html; index index.html index.htm; }
2.if (condition) { ... }引入一个新的配置上下文 ;条件满足时,执行配置块中的配置指令;server, location;condition:比较操作符:==!=~:模式匹配,区分字符大小写;~*:模式匹配,不区分字符大小写;!~:模式不匹配,区分字符大小写;!~*:模式不匹配,不区分字符大小写;文件及目录存在性判断:-e, !-e-f, !-f-d, !-d-x, !-x
3.returnreturn code [text];return code URL;return URL;Stops processing and returns the specified code to a client. 停止处理并将指定的代码返回给客户端。 非标准代码444关闭连接而不发送响应报头。 4.rewrite_log on | off;是否开启重写日志;

5.set $variable value;用户自定义变量

五、nginx 后端服务器组配置指令详解

nginx支持设置一组服务器作为后端服务器,做为反代或负载均衡都会涉及到

模块由Module ngx_http_upstream_module模块提供
http://nginx.org/en/docs/http/ngx_http_upstream_module.html

1.Syntax:upstream name { ... }
Default:—
Context:http
Defines a group of servers. Servers can listen on different ports. In addition, servers listening on TCP and UNIX-domain sockets can be mixed.
用于指定后端服务器组,upstream 后面为自定义的后端服务器组,默认为轮询调度对后端服务器进行转发处理请求,如果后端某一台服务器发生故障调度器服务器会自动将其从组中踢除,待恢复后又自动加回组中。

2.Syntax:server address [parameters];
Default:—
Context:upstream
Defines the address and other parameters of a server. The address can be specified as a domain name or IP address, with an optional port, or as a UNIX-domain socket path specified after the “unix:” prefix. If a port is not specified, the port 80 is used. A domain name that resolves to several IP addresses defines multiple servers at once.

The following parameters can be defined:

weight=number
sets the weight of the server, by default, 1.
max_conns=number
该指令用于指定设置组内服务器
address 服务器地址
[parameters]
weight=number
为组内添加权重,权重值越高服务器被优先用于服务器,组内权重默认为1,如果不设置则组内主句轮询处理;
max_fails=number
设置一个请求失败的次数,在一定时间范围内当对组内服务器请求失败可允许超过次数,如果请求失败次数超过该值时则认为该服务器为无效,默认值也是为1,
fail_timeout=number
该时间设置有两个作用。一是当多长时间尝试去连接已失效服务器,第二个是在max_fails里面说到的在一定时间内对组内服务器请求失败的时间;默认为1;
backup ;
将组内某台服务器设置为备用服务器,只有所有服务器都处于无效情况下,该服务器才会被用来处理请求
down;
将组内某台服务器设置为永久无效状态。
示例:
http {
upstream baks {
server 10.1.45.61:80 weight=1 max_fails=2 fail_timeout=30s;
server 10.1.45.62:80 weight=3;
server 127.0.0.1:80 backup;

}
}

3.Syntax:ip_hash;Default:—Context:upstreamSpecifies that a group should use a load balancing method where requests are distributed between servers based on client IP addresses. The first three octets of the client IPv4 address, or the entire IPv6 address, are used as a hashing key. The method ensures that requests from the same client will always be passed to the same server except when this server is unavailable. In the latter case client requests will be passed to another server. Most probably, it will always be the same server as well.该指令用于实现会话保持功能,将来自于某一客户端的请求定向到组内任何一台服务器上,保证客户端与服务器之间建立稳定的会话,只有在该服务器在无效情况下才会被下一个服务器接收和处理实例如下:http {ip_hash;upstream baks {server 10.1.45.61:80 weight=1 max_fails=2 fail_timeout=30s;server 10.1.45.62:80 weight=3;server 127.0.0.1:80 backup;
}}
4.Syntax:keepalive connections;Default:—Context:upstreamThis directive appeared in version 1.1.4.Activates the cache for connections to upstream servers.The connections parameter sets the maximum number of idle keepalive connections to upstream servers that are preserved in the cache of each worker process. When this number is exceeded, the least recently used connections are closed.该指令用于设置为每个worker进程保留的空闲的长连接数量。
5.Syntax:least_conn;Default:—Context:upstreamThis directive appeared in versions 1.3.1 and 1.2.2.Specifies that a group should use a load balancing method where a request is passed to the server with the least number of active connections, taking into account weights of servers. If there are several such servers, they are tried in turn using a weighted round-robin balancing method.最少连接调度算法,当server拥有不同的权重时其为wlc;

6.Syntax:hash key [consistent];Default:—Context:upstreamThis directive appeared in version 1.7.2.Specifies a load balancing method for a server group where the client-server mapping is based on the hashed key value. The key can contain text, variables, and their combinations. Note that adding or removing a server from the group may result in remapping most of the keys to different servers. The method is compatible with the Cache::Memcached Perl library.
If the consistent parameter is specified the ketama consistent hashing method will be used instead. The method ensures that only a few keys will be remapped to different servers when a server is added to or removed from the group. This helps to achieve a higher cache hit ratio for caching servers. The method is compatible with the Cache::Memcached::Fast Perl library with the ketama_points parameter set to 160.基于指定的key的hash表来实现对请求的调度,此处的key可以直接文本、变量或二者的组合;作用:将请求分类,同一类请求将发往同一个upstream server;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息