1、目录结构
...
events{
...
}
http{
server{
location ...{
...
}
upstream ...{
...
}
error_page ...;
}
}
2、gzip压缩
gzip_vary on;
gzip_min_length 1000;
gzip_comp_level 6;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
3、负载均衡(upstream)
#weight是权重,参数表示权值,权值越高被分配到的几率越大
#max_fails是检查最多失败次数
#fail_timeout是超时设置,超过时间会被当做执行失败
#当在fail_timeout的时间内,某个server连接失败了max_fails次,则nginx会认为该server不工作了。同时,在接下来的fail_timeout时间内,nginx不再将请求分发给失效的server。
upstream demo {
server 192.168.21.51:80 weight=3 max_fails=2 fail_timeout=30s;
server 192.168.21.52:80 weight=2 max_fails=2 fail_timeout=30s;
server 192.168.21.53:80 weight=3 max_fails=2 fail_timeout=30s;
}
**1、设置日志记录格式**
log_format指令用来设置日志的记录格式,语法如下:
log_format name format[format......]
具体日志格式如下:
log_format mylogformat '$http_x_forwardedfor-$remote_user[$time_local]'
'"Srequest"$statusSbody_byte_sent''"$httpreferer""Shttpuser_agent"'
$http_x_forwarded_for
$remote_user
$time_local
$request
$status
$body_bytes_sent
$http_referer
$http_user_agent
**2、定义日志**
access_log /data/log/$server_name.log mylogformat buffer=32k
**3、开启日志打开缓存**
对于每一条日志记录,日志文件都将先打开文件,再写入日志记录,然后马上关闭,为了提高包含变量的日志文件存放路径的性能,可以使用open_log_file_cache指令来设置,格式为:
open_log_file_cache max=N[inactive=time][min_uses=N][valid=time]|off
该指令默认是禁止的,等同于:
open_log_file_cache off;
max:设置缓存中的最大文件描述符数量
Inactive:设置一个时间,如果在设置的时间内没有使用此文件描述符,则自动删除此描述符
min_uses:在参数inactive指定的时间范围内,如果日志文件超过被使用的次数,则将该日志文件的描述符计入缓存,默认为10秒钟
valid:设置多长时间检查一次,看日志文件路径与文件名是否仍然存在,默认60秒
例:open_log_file_cache max=1000 inactive=20s min_uses=2 valid=1m |off
5、状态检查
location /nginx_status{
stub_status on;
access_log off;
}
6、目录身份验证
location /redhat{
root /web/html;
index index.html index.htm;
autoindex on;
auth_basic "AwstatAuth"
auth_basic_user_file /etc/nginx/.htpasswd;
allow 192.168.0.0/24;
allow 192.168.1.1;
deny all;
}
7、配置文件实例
worker_processes 4;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
access_log logs/access.log main buffer=32k;
sendfile on;
keepalive_timeout 65;
gzip_vary on;
gzip_min_length 1000;
gzip_comp_level 6;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
upstream demo {
server 192.168.21.51:80 weight=3;
server 192.168.21.52:80 weight=2;
server 192.168.21.53:80 weight=3;
}
server {
listen 80;
server_name localhost;
location / {
root /usr/local/itoo-217;
index index.html index.htm;
autoindex on;
}
location /auth-web/ {
limit_rate 256k;
proxy_buffer_size 128k;
proxy_buffers 128 128k;
proxy_busy_buffers_size 128k;
proxy_pass http://demo/auth-web/;
proxy_set_header Host $host:80;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /group1/ {
proxy_buffer_size 128k;
proxy_buffers 128 128k;
proxy_busy_buffers_size 128k;
proxy_pass http://tfjybj.com/group1/;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
8、其他常用设置
worker_processes 8;
worker_connections 65535;
client_header_buffer_size 32k;
large_client_header_buffers 4 64k;
client_max_body_size 8m;
autoindex on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 120;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;