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 /var/www/html;
index index.html index.htm;
<span class="hljs-comment"># 命名为main的日志格式</span>
log_format main <span class="hljs-string">'$remote_addr - $remote_user [$time_local] "$request" '</span>
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
# 访问的日志
access_log logs/access.log main;
<span class="hljs-comment">#【文件模块】 </span>
sendfile <span class="hljs-literal">on</span>; <span class="hljs-comment">#开启文件从硬盘到网络的传输,不需要通过缓存(减少IO,平缓硬盘和网络的处理速度)</span>
tcp_nopush <span class="hljs-literal">on</span>; <span class="hljs-comment">#此选项允许或禁止使用socke的TCP_CORK的选项,此选项仅在使用sendfile的时候使用</span>
<span class="hljs-comment">#【响应设置,缓存和响应】</span>
<span class="hljs-comment">#在配置文件中设置自定义缓存以限制缓冲区溢出攻击的可能性</span>
client_max_body_size <span class="hljs-number">50</span>m; <span class="hljs-comment">#请求体最大值</span>
client_header_buffer_size <span class="hljs-number">4</span>k; <span class="hljs-comment">#请求头缓存最大值</span>
client_body_buffer_size <span class="hljs-number">256</span>k; <span class="hljs-comment">#请求体缓存最大值</span>
large_client_header_buffers <span class="hljs-number">8</span> <span class="hljs-number">128</span>k; <span class="hljs-comment">#客户请求头缓冲大小</span>
client_header_timeout <span class="hljs-number">3</span>m; <span class="hljs-comment">#请求头超时</span>
client_body_timeout <span class="hljs-number">3</span>m; <span class="hljs-comment">#请求体超时</span>
reset_timedout_connection on; <span class="hljs-comment">#关闭不响应的客户端连接。这将会释放那个客户端所占有的内存空间</span>
send_timeout <span class="hljs-number">3</span>m; <span class="hljs-comment">#response的时候,超时时间</span>
keepalive_timeout <span class="hljs-number">65</span>; <span class="hljs-comment">#连接时间(秒)</span>
tcp_nodelay <span class="hljs-literal">on</span>;
upstream tomcat{
ip_hash;
server <span class="hljs-number">192.168</span><span class="hljs-number">.217</span><span class="hljs-number">.1</span>:<span class="hljs-number">8080</span> ;
server <span class="hljs-number">192.168</span><span class="hljs-number">.217</span><span class="hljs-number">.2</span>:<span class="hljs-number">8080</span> ;
}
server {
listen <span class="hljs-number">80</span>;
server_name tomcat.com;
<span class="hljs-comment">#动态代理</span>
location =/{
proxy_redirect <span class="hljs-literal">off</span>;
proxy_pass http:<span class="hljs-regexp">//</span>tomcat;
}
<span class="hljs-comment">#错误</span>
error_page <span class="hljs-number">500</span> <span class="hljs-number">502</span> <span class="hljs-number">503</span> <span class="hljs-number">504</span> /<span class="hljs-number">50</span>x.html;
location =/<span class="hljs-number">50</span>x.html{
}
<span class="hljs-comment">#静态文件</span>
location ~*.*.(js|css)?$ {
expires <span class="hljs-number">7</span>d; <span class="hljs-comment">#保存7天</span>
access_log <span class="hljs-literal">off</span>; <span class="hljs-comment">#关闭访问日志</span>
}
location ~*.*.(png|jpg|gif|jpeg|bmp|ico)?$ {
expires <span class="hljs-number">7</span>d;
access_log <span class="hljs-literal">off</span>;
}
location ~* .*.(zip|rar|exe|msi|iso|gho|mp3|rmvb|mp4|wma|wmv|rm)?$ 
{
deny all; <span class="hljs-regexp">#</span>禁止这些文件下载,大家可以根据自己的环境来配置
}
}
}
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_read_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; #压缩类型
mime.types文件参考配置
```java types { text/html html htm shtml; text/css css; text/xml xml rss; image/gif gif; image/jpeg jpeg jpg; application/x-javascript js; application/atom+xml atom; text/mathml mml;
text/plain txt;
text/vnd.sun.j2me.app-descriptor jad;
text/vnd.wap.wml wml;
text/x-component htc;
image/png png;
image/tiff tif tiff;
image/vnd.wap.wbmp wbmp;
image/x-icon ico;
image/x-jng jng;
image/x-ms-bmp bmp;
image/svg+xml svg svgz;
application/java-archive jar war ear;
application/json json;
application/mac-binhex40 hqx;
application/msword doc;
application/pdf pdf;
application/postscript ps eps ai;
application/rtf rtf;
application/vnd.ms-excel xls;
application/vnd.ms-powerpoint ppt;
application/vnd.wap.wmlc wmlc;
application/vnd.google-earth.kml+xml kml;
application/vnd.google-earth.kmz kmz;
application/x-7z-compressed 7z;
application/x-cocoa cco;
application/x-java-archive-diff jardiff;
application/x-java-jnlp-file jnlp;
application/x-makeself run;
application/x-perl pl pm;
application/x-pilot prc pdb;
application/x-rar-compressed rar;
application/x-redhat-package-manager rpm;
application/x-sea sea;
application/x-shockwave-flash swf;
application/x-stuffit sit;
application/x-tcl tcl tk;
application/x-x509-ca-cert der pem crt;
application/x-xpinstall xpi;
application/xhtml+xml xhtml;
application/zip zip;
application/octet-stream bin exe dll;
application/octet-stream deb;
application/octet-stream dmg;
application/octet-stream eot;
application/octet-stream iso img;
application/octet-stream msi msp msm;
application/ogg ogx;
audio/midi mid midi kar;
audio/mpeg mpga mpega mp2 mp3 m4a;
audio/ogg oga ogg spx;
audio/x-realaudio ra;
audio/webm weba;
video/3gpp 3gpp 3gp;
video/mp4 mp4;
video/mpeg mpeg mpg mpe;
video/ogg ogv;
video/quicktime mov;
video/webm webm;
video/x-flv flv;
video/x-mng mng;
video/x-ms-asf asx asf;
video/x-ms-wmv wmv;
video/x-msvideo avi;
}
原文地址:https://my.oschina.net/u/2246410/blog/618818