?nginx的常规配置
nginx的使用非常简单,只需要配置好我们需要的各种指令,就能跑起来。如果你需要添加模块,还需要添加模块方面的配置。一般在使用中,都是将nginx.conf拆分成几个小块的文件,一边管理。
1、nginx.conf配置
#user nobody;
#Linux存放线程的pid,每次启动生成
#pid logs/nginx.pid;
#和cpu核心一样
worker_processes 1;
#错误日志
error_log logs/error.log error;
#error_log logs/notice.log notice;
#error_log logs/info.log info;
#error_log logs/debug.log debug;
#error_log logs/warn.log warn;
events {
worker_connections 1024;
}
http {
include mime.types; #include 用户加载另外的配置文件,例如其它配置内容过的
include gzip.conf;
include proxy.conf;
default_type application/octet-stream;
server_tokens off; #关闭在错误页面中的nginx版本数字
charset UTF-8;
limit_conn addr 100; #每个IP连接100个连接
root D:\website
index index.html index.htm;
# 命名为main的日志格式
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;
#【文件模块】
sendfile on; #开启文件从硬盘到网络的传输,不需要通过缓存(减少IO,平缓硬盘和网络的处理速度)
tcp_nopush on; #此选项允许或禁止使用socke的TCP_CORK的选项,此选项仅在使用sendfile的时候使用
#【响应设置,缓存和响应】
#在配置文件中设置自定义缓存以限制缓冲区溢出攻击的可能性
client_max_body_size 50m; #请求体最大值
client_header_buffer_size 4k; #请求头缓存最大值
client_body_buffer_size 256k; #请求体缓存最大值
large_client_header_buffers 8 128k; #客户请求头缓冲大小
client_header_timeout 3m; #请求头超时
client_boddy_timeout 3m; #请求体超时
reset_timeout_connection #关闭不响应的客户端连接。这将会释放那个客户端所占有的内存空间
send_timeout 3m; #response的时候,超时时间
keepalive_timeout 65; #连接时间(秒)
tcp_nodelay on;
upstream tomcat{
ip_hash;
server 192.168.217.1:8080 ;
server 192.168.217.2:8080 ;
}
server{
listen 80;
server_name tomcat.com;
#动态代理
localhost =/{
proxy_redirect off;
proxy_pass http://tomcat;
}
#错误
error_page 500 502 503 504 /50x.html;
localhost =/50x.html{
}
#静态文件
localhost ~*.*\.(js|css)?${
expires 7d; #保存7天
access_log off; #关闭访问日志
}
localhost ~*.*\(png|jpg|gif|jpeg|bmp|ico)?${
expires 7d;
access_log off;
}
location ~* .*\.(zip|rar|exe|msi|iso|gho|mp3|rmvb|mp4|wma|wmv|rm)?$
{
deny all; //禁止这些文件下载,大家可以根据自己的环境来配置
}
}
}
2、proxy.conf配额制
################ 设置传送给后台服务器的请求头(主要是为了session) #####
proxy_set_header Host $host; #表示客户端请求头部中的Host字段
proxy_set_header X-Real-IP $remote_addr; #客户端IP地址
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #设置头转发
######### proxy_request 设置 ##################
proxy_connect_timeout 75s; #nginx跟后端服务器请求时间
proxy_rend_timeout 75s; #连接后,等候后端服务器响应时间处理时间。可能在连接表中
########## proxy_response 设置 ##################
proxy_send_timeout 75s; #连接成功后,后端服务器数据回传时间_就是在规定时间之内后端服务器必须传完所有的数据
proxy_buffer_size 64k; #代理服务器(nginx)保存用户头的缓冲区
proxy_buffers 4 32k; #proxy_buffers缓冲区,网页平均在32k以下
proxy_busy_buffers_size 64k; #高负荷下缓冲大小(proxy_buffers*2)
proxy_temp_file_write_size 64k; #设定缓存文件大小,大于这个值,将从后端服务器传送,不用通过nginx缓存
proxy_ignore_client_abort on; #如果客户端断开请求,也保持与后端服务器的连接,防止服务器出现BUG
3、gzip.conf配置
#【压缩】
gzip on;
gzip_disable "msie6"; #ie6一下不压缩
#gzip_static on #告诉nginx在压缩资源之前,先查找是否有预先gzip处理过的资源。这要求你预先压缩你的文件(在这个例子中被注释掉了),从而允许你使用最高压缩比,这样nginx就不用再压缩这些文件了
gzip_proxied any; #允许或者禁止压缩基于请求和响应的响应流
gzip_min_length 1000; #最小的压缩文件,小于这个不压缩
gzip_comp_level 4; #压缩等级(1-9)
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript; #压缩类型