Nginx配置

这里总结一下Nginx常用的一些配置文件,并加以说明,方便日后在工作中参考使用。
下面大部分的都是google出来的资料,加了一些自己的理解和配置。
仅供参考,如有错误希望大家积极指出。

nginx.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#定义Nginx运行的用户和用户组
user www www;

#为worker_processes增加参数auto。当设置成auto,tengine将自动启动与cpu数量相同的worker进程
worker_processes auto;

#为worker_cpu_affinity增加参数auto和off。当设置成auto时,tengine将根据worker的数量自动配置cpu绑定位图。
worker_cpu_affinity auto;

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

#进程文件
pid logs/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_connections 65535;
}
#设定http服务器
http
{
include mime.types; #文件扩展名与文件类型映射表
default_type application/octet-stream; #默认文件类型
charset utf-8; #默认编码
server_tag off; #关闭HTTP响应的Server头。
server_info off; #当打开server_info的时候,显示错误页面时会显示URL、服务器名称和出错时间。
server_names_hash_bucket_size 128; #服务器名字的hash表大小
client_header_buffer_size 32k; #上传文件大小限制
large_client_header_buffers 4 64k; #设定请求缓
sendfile on; #开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常把这个改成off。
#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; #压缩版本
gzip_comp_level 2; #压缩等级
gzip_types text/plain application/x-javascript text/css application/xml; #压缩类型,默认就已经包含text/html,所以下面就不用再写了,写上去也不会有问题,但是会有一个warn。
gzip_vary on;

include /usr/local/nginx/conf/vhost/*.conf; #虚拟主机配置文件
}

静态虚拟主机配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
server {
listen 80;
server_name domain ;
access_log logs/www.domain.access.log main;

location / {
root /www/domain;
index index.html,index.htm;
}

location ~ \.(jpg|jpeg|png|gif)$ {
expires 1d;
}

location ~ \.(js|css)$ {
expires 1h;
}

}

proxy.conf 反向代理配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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服务器传
}

PHP虚拟主机

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
server {
listen 80;
server_name www.abc.com ;
access_log logs/www.abc.com.access.log main;
root html;
index index.php index.html index.htm;

location ~ \.(jpg|jpeg|png|gif)$ {
expires 1d;
}

location ~ \.(js|css)$ {
expires 1h;
}

location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
include fastcgi_params;
}
}

屏蔽特定浏览器

搞过前端的估计都碰到最头疼的问题就是浏览器兼容性问题了,特别是针对IE浏览器。往往前端为了省事就搞一个页面提示用户升级浏览器或者显示简单的静态页面。那接下来就需要运维来配置nginx rewrite规则了。

在这里直接贴出配置实例

1
2
3
4
5
6
7
8
server {
listen 80;
server_name xxx.xxx.com;
root /www ;
if ( $http_user_agent ~* "MSIE [6-9].[0-9]") {
rewrite /ie.html break;
}
}

解释一下上面的配置

$http_user_agent 客户端agent信息(这个是浏览器的标识,如果你开了访问日志的话,可以去看一下。每种浏览器的标识可能都不一样。)

~* 使用正则表达式,并且不区分大小写

MSIE [6,7].[0-9] MSIE-IE浏览器的标识,这里匹配的是IE浏览器在版本在6到9的,例如6.1,7.0,8.2等等···

rewrite /ie6.html 只要匹配则返回指定的静态页面

break 停止执行当前这一轮的ngx_http_rewrite_module指令集

如果需要验证是否生效的话,这里提供一个比较方便的工具 IETester,可以模拟IE任意版本的浏览器来测试

反向代理nodejs

server {
listen 80;
server_name www.domain.com;
root /www/www.domain.com; #定义服务器的默认网站根目录位置
index index.html index.htm; #定义首页索引文件的名称
access_log logs/www.domain.com.access.log;

location ~ ^/(images/|img/|javascript/|js/|css/|stylesheets/|flash/|static/|robots.txt|humans.txt|favicon.ico) {
      root /www/www.domain.com/public ;
      access_log off;
      expires 1d;
    }

location / {
      include proxy.conf;
      proxy_pass http://10.169.135.22:5100;
  }

nginx日志切割

1
2
3
4
5
6
7
8
9
10
#/bin/bash
LOGS_PATH="/usr/local/nginx/logs"
YESTERDAY=$(date -d "yesterday" +%Y%m%d)
pid_path="/usr/local/nginx/logs/nginx.pid"
cd $LOGS_PATH
for log_name in ` ls *.access.log `
do
mv ${LOGS_PATH}/${log_name} ${LOGS_PATH}/${log_name}_${YESTERDAY}.log
done
kill -USR1 `cat ${pid_path}`

nginx全局变量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
$args #这个变量等于请求行中的参数。
$content_length #请求头中的Content-length字段。
$content_type #请求头中的Content-Type字段。
$document_root #当前请求在root指令中指定的值。
$host #请求主机头字段,否则为服务器名称。
$http_user_agent #客户端agent信息
$http_cookie #客户端cookie信息
$limit_rate #这个变量可以限制连接速率。
$request_body_file #客户端请求主体信息的临时文件名。
$request_method #客户端请求的动作,通常为GET或POST。
$remote_addr #客户端的IP地址。
$remote_port #客户端的端口。
$remote_user #已经经过Auth Basic Module验证的用户名。
$request_filename #当前请求的文件路径,由root或alias指令与URI请求生成。
$query_string #与$args相同。
$scheme #HTTP方法(如http,https)。
$server_protocol #请求使用的协议,通常是HTTP/1.0或HTTP/1.1。
$server_addr #服务器地址,在完成一次系统调用后可以确定这个值。
$server_name #服务器名称。
$server_port #请求到达服务器的端口号。
$request_uri #包含请求参数的原始URI,不包含主机名,如:”/foo/bar.php?arg=baz”。
$uri #不带请求参数的当前URI,$uri不包含主机名,如”/foo/bar.html”。
$document_uri #与$uri相同。
坚持原创技术分享,您的支持将鼓励我继续创作!