knight_ka | 生活及学习笔记

Nginx1_1-Nginx基本配置

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# 指定使用的用户和组
#user www www;
#指定工作衍生的进程数(一般等于CPU总核数的两倍,4核CPU则为8)
worker_processes 1;
# 指定错误日志的存放路径,及错误日志记录级别[ debug | info | notice | warn | error | crit ]
# 不指定则为默认值
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
# 指定pid的存放路径 操作nginx进程使用。在平滑升级时需要使用
#pid logs/nginx.pid;
#指定文件描述符数量
#文件描述符在形式上是一个非负整数。实际上,它是一个索引值,指向内核为每一个进程所维护的该进程打开文件的记录表。当程序打开一个现有文件或者创建一个新文件时,内核向进程返回一个文件描述符。在程序设计中,一些涉及底层的程序编写往往会围绕着文件描述符展开。但是文件描述符这一概念往往只适用于UNIX、Linux这样的操作系统。
Worker_rlimit_nofile 51200;
# nginx事件配置
events {
# 使用的网络I/O模型,linux推荐使用epoll,FreeBSD推荐使用kqueue模型
use epoll;
# 允许的连接数
worker_connections 51200;
}
# http请求配置
http {
# 配置nginx需要解析的mime类型
include mime.types;
# 默认的mime类型
default_type application/octet-stream;
# 设置使用的字符集,如果一个网站中有多种字符集,请不要随便设置,应该在html的meta标签设置。
#charset gb2312
#日志格式log_format
# $http_x_forwarded_for 用于记录客户端ip
#$remote_addr和$http_forwarded_for代表客户端的真实ip
# $remote_user表示远程客户端的名称
# $time_local代表访问时间与时区
# $request 表示请求的URL和http协议 $status 请求状态码 $body_bytes_sent 发送给客户端的文件主体内容大小
# $http_referer 从哪个url访问过来的
# $http_user_agent 客户端浏览器信息
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
# 设置nginx的访问日志路径及记录级别 main表示用main格式的日志
#access_log path [formatname [buffer=size | off] ] 内存缓冲区大小
# 如果不想记录日志 access_log off;
# 可以使用默认的combined格式记录日志 access_log path combined;或者不指定format则使用的就是默认的格式
#日志路径 可以包含变量$server_name.log combined;
# 使用变量需要注意:1.启动进程时指定的用户是否有权限 2.缓冲将不会使用 3.每一条日志都会先打开文件再进行操作,为了变量日志文件提升性能,使用open_log_file_cache来指定经常使用的日志文件描述符缓存。
#open_log_file_cache off; 默认是禁止的
Open_log_file_cache max=1000 inactive=20s min_uses=2 vaild=60s;
#access_log logs/access.log main;
日志文件定时切割:
1.mv /usr/local/nginx/logs/access.log /usr/local/nginx/logs/44444.log
2.使用kill -USE1 进程号|`指定的pid路径` 来进行重新生成access.log
3.一般配合corntab命令 写一个shell脚本来实现日志的自动切割
# sendfile打开可以减少系统的用户态和内核态的切换,从而提升服务器性能
sendfile on;
#tcp_nopush on;
# 客户端请求头部缓冲区大小
client_header_buffer_size 4k;
#设置客户端上传文件的大小
Client_max_body_size 8m;
#keepalive_timeout 0;
# keepalive的超时时间
keepalive_timeout 65;
# 开启gzip压缩 压缩后页面大小可变为原来的30%甚至更小
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_types text/plain application/x-javascript text/css application/xml
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
//当不存在index文件时,可以自动列出目录:需要指定以下指令
autoindex on;
Autoindex_exact_size [ on | off ] 文件的大小
Autoindex_localtime [ on | off ]开启本地时间来显示文件时间
#root html;
#index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}

nginx主要配置文件主要有三部分组成:

1
2
3
4
5
总体配置...
events配置
http配置
server1
Server2...

nginx的虚拟主机配置:

虚拟主机是使用特殊的软硬件技术把一台运行在因特网上的服务器主机分割成一台台不同的虚拟主机,每个虚拟主机都可以运行一个独立的网站,可以有不同的域名。
使一台电脑可以提供多个web服务。

日志控制的相关配置

浏览器本地缓存设置:nginx将自动在http请求头添加Cache-Control:max-age=#。#为指定的秒数。

示例:对常见格式的图片,flash等文件浏览器缓存30天,对js、css文件缓存1小时。

1
2
3
4
5
6
7
8
9
Location ~ .*\.(git|jpg|jpeg|png|bmp|swf) $
{
expire 30d;
}
location ~ .*\.(js|css)?$ 注意?表示懒惰模式,匹配到一个后 则进行下一段匹配
{
expire 1h;
}