nginx配置

  1. #http://www.open-open.com/home/space-361-do-blog-id-5087.html  

  2. #user  nobody;  

  3.   

  4. #定义了nginx对外提供web服务时的worker进程数。最优值取决于许多因素,包括(但不限于)CPU核的数量、存储数据的硬盘数量  

  5. #及负载模式。不能确定的时候,将其设置为可用的CPU内核数将是一个好的开始(设置为“auto”将尝试自动检测它)。  

  6. worker_processes  2;  

  7. #更改worker进程的最大打开文件数限制。如果没设置的话,这个值为操作系统的限制。  

  8. #设置后你的操作系统和Nginx可以处理比“ulimit -a”更多的文件,所以把这个值设高,这样nginx就不会有“too many open files”问题了。  

  9. worker_rlimit_nofile 10000;  

  10.   

  11.   

  12. #error_log  logs/error.log;  

  13. #error_log  logs/error.log  notice;  

  14. error_log  logs/error.log  info;  

  15.   

  16. #pid        logs/nginx.pid;  

  17.   

  18.   

  19. events {  

  20.     worker_connections  2048;  #允许最大连接数  

  21.     multi_accept on;  #告诉nginx收到一个新连接通知后接受尽可能多的连接。  

  22.     #设置用于复用客户端线程的轮询方法。如果你使用Linux 2.6+,你应该使用epoll。如果你使用*BSD,你应该使用kqueue。  

  23.     #(值得注意的是如果你不知道Nginx该使用哪种轮询方法的话,它会选择一个最适合你操作系统的)  

  24.     #use epoll;  

  25. }  

  26.   

  27.   

  28. http {  

  29.     #server_tokens  并不会让nginx执行的速度更快,但它可以关闭在错误页面中的nginx版本数字,这样对于安全性是有好处的。  

  30.     #server_tokens off;  

  31.       

  32.     include       mime.types;  

  33.     default_type  application/octet-stream;  

  34.   

  35.     #定义日志格式(格式名字为main)  

  36.         #$remote_addr与$http_x_forwarded_for用以记录客户端的ip地址;  

  37.         #$remote_user:用来记录客户端用户名称;  

  38.     #$time_local: 用来记录访问时间与时区;  

  39.     #$request: 用来记录请求的url与http协议  

  40.     #$status: 用来记录请求状态;成功是200,  

  41.     #$body_bytes_s ent :记录发送给客户端文件主体内容大小;  

  42.     #$http_referer:用来记录从那个页面链接访问过来的;  

  43.     #$http_user_agent:记录客户毒啊浏览器的相关信息;  

  44.   

  45.     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '  

  46.                       '$status $body_bytes_sent "$http_referer" '  

  47.                       '"$http_user_agent" "$http_x_forwarded_for"';  

  48.   

  49.     access_log  logs/access.log  main;  

  50.   

  51.     #sendfile()可以在磁盘和TCP socket之间互相拷贝数据(或任意两个文件描述符)。  

  52.     #Pre-sendfile是传送数据之前在用户空间申请数据缓冲区。之后用read()将数据从文件拷贝到这个缓冲区,  

  53.     #write()将缓冲区数据写入网络。sendfile()是立即将数据从磁盘读到OS缓存。因为这种拷贝是在内核完成的,  

  54.     #sendfile()要比组合read()和write()以及打开关闭丢弃缓冲更加有效(更多有关于sendfile)。  

  55.     sendfile on;  

  56.   

  57.     #告诉nginx在一个数据包里发送所有头文件,而不一个接一个的发送。  

  58.     tcp_nopush on;  

  59.   

  60.     #告诉nginx不要缓存数据,而是一段一段的发送--当需要及时发送数据时,就应该给应用设置这个属性,这样发送一小块数据信息时就不能立即得到返回值。  

  61.     tcp_nodelay on;  

  62.       

  63.     fastcgi_intercept_errors on;  

  64.       

  65.     #开启gzip压缩,对网页文件、css、js、xml等启动gzip压缩,减少数据传输量,提高访问速度。  

  66.     #include gzip.conf;  

  67.       

  68.     #给客户端分配keep-alive链接超时时间。服务器将在这个超时时间过后关闭链接。我们将它设置低些可以让ngnix持续工作的时间更长。  

  69.     #设置为0,即为持续长久连接  

  70.     keepalive_timeout  65;  

  71.     #设置请求头和请求体(各自)的超时时间。我们也可以把这个设置低些。  

  72.     client_header_timeout 10;  

  73.     client_body_timeout 10;  

  74.     #告诉nginx关闭不响应的客户端连接。这将会释放那个客户端所占有的内存空间。  

  75.     reset_timedout_connection on;  

  76.     #指定客户端的响应超时时间。这个设置不会用于整个转发器,而是在两次客户端读取操作之间。  

  77.     #如果在这段时间内,客户端没有读取任何数据,nginx就会关闭连接。  

  78.     send_timeout 30;      

  79.       

  80.     #定义一个名为allips的limit_req_zone用来存储session,大小是10M内存,  

  81.     #以$binary_remote_addr为key,限制平均每秒的请求为20个,  

  82.     #1M能存储16000个状态,rate的值必须为整数,如果限制两秒钟一个请求,可以设置成30r/m  

  83.     #limit_req_zone $binary_remote_addr zone=allips:10m rate=20r/s;   

  84.             #限制每ip每秒不超过20个请求,漏桶数burst为5。(结合limit_req_zone使用)  

  85.             #brust的意思就是,如果第1秒、2,3,4秒请求为19个,  

  86.             #第5秒的请求为25个是被允许的。  

  87.             #但是如果你第1秒就25个请求,第2秒超过20的请求返回503错误。  

  88.             #nodelay,如果不设置该选项,严格使用平均速率限制请求数,  

  89.             #第1秒25个请求时,5个请求放到第2秒执行,  

  90.             #设置nodelay,25个请求将在第1秒执行。  

  91.     #limit_req zone=allips burst=5 nodelay;  

  92.     #设置用于保存各种key(比如当前连接数)的共享内存的参数。5m就是5兆字节,这个值应该被设置的足够大以存储(32K*5)32byte状态或者(16K*5)64byte状态。  

  93.     #该属性只能定义在http区域。  

  94.     #limit_conn_zone $binary_remote_addr zone=perip:1m;  

  95.     #为给定的key设置最大连接数。这里key是addr,我们设置的值是20,也就是说我们允许每一个IP地址最多同时打开有20个连接。  

  96.     #该属性可定义在http server location区域。  

  97.     #limit_conn perip 20;  

  98.   

  99.     #打开缓存的同时也指定了缓存最大数目,以及缓存的时间。我们可以设置一个相对高的最大时间,这样我们可以在它们不活动超过20秒后清除掉。  

  100.     open_file_cache max=10000 inactive=20s;  

  101.     #在open_file_cache中指定检测正确信息的间隔时间。  

  102.     open_file_cache_valid 30s;  

  103.     #定义了open_file_cache中指令参数不活动时间期间里最小的文件数。  

  104.     open_file_cache_min_uses 2;  

  105.     #指定了当搜索一个文件时是否缓存错误信息,也包括再次给配置中添加文件。  

  106.     #我们也包括了服务器模块,这些是在不同文件中定义的。如果你的服务器模块不在这些位置,你就得修改这一行来指定正确的位置。  

  107.     open_file_cache_errors on;  

  108.       

  109.     #关闭磁盘缓存,减少磁盘IO。  

  110.     proxy_max_temp_file_size 0;  

  111.     proxy_connect_timeout      2;  

  112.     proxy_send_timeout         10;  

  113.     proxy_read_timeout         10;  

  114.     proxy_buffer_size          16k;  

  115.     proxy_buffers              4 64k;  

  116.     #如果系统很忙的时候可以申请更大的proxy_buffers(官方推荐:proxy_buffers*2)  

  117.     #proxy_busy_buffers_size    128k;  

  118.     #proxy_temp_file_write_size 128k;  

  119.     #缓存proxy_temp_path和proxy_cache_path必须在同一个分区  

  120.     #proxy_temp_path docs/cache/temp;  

  121.     #该指令用于设置缓存文件的存放路径,设置缓存区名称为cache_one,  

  122.     #内存缓存空间大小为200M,自动清除超过1天没有被访问的缓存数据,硬盘缓存空间大小为30GB  

  123.     #proxy_cache_path docs/cache/path levels=1:2 keys_zone=cache_one:100m inactive=1d max_size=4g;  

  124.   

  125.     upstream localhost{  

  126.     server localhost:8080 weight=10;  

  127.     server localhost:8081 weight=10;  

  128.     #server localhost:10000 backup;  

  129.     #每个设备的状态设置为:  

  130.     #1.down表示单前的server暂时不参与负载  

  131.     #2.weight默认为1.weight越大,负载的权重就越大。  

  132.     #3.max_fails:允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream模块定义的错误  

  133.     #4.fail_timeout:max_fails次失败后,暂停的时间。  

  134.     #5.backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。  

  135.     }  

  136.     server {  

  137.         listen       80;  

  138.         server_name  localhost;  

  139.   

  140.         charset utf-8;  

  141.         access_log  logs/localhost.access.log  main;  

  142.       

  143.     error_page 404 /404.html;  

  144.     location = /404.html {  

  145.             root   html;  

  146.         }  

  147.         # redirect server error pages to the static page /50x.html  

  148.         error_page   500 502 503 504  /50x.html;  

  149.         location = /50x.html {  

  150.             root   html;  

  151.         }  

  152.   

  153.     #禁止访问WEB-INF目录文件  

  154.     location ~ ^/(WEB-INF)/{  

  155.         #不记录404错误日志  

  156.         #log_not_found off;  

  157.         deny all;  

  158.     }  

  159.       

  160.     #静态文件让nginx处理  

  161.     #location ~ .*.(gif|jpg|jpeg|png|bmp|swf|css|js|htm|html)$ {  

  162.     #   root html;  

  163.     #   access_log off;  

  164.     #   expires 30d;  

  165.     #}  

  166.       

  167.         location / {  

  168.         #限制下载速度256KB/秒  

  169.             #limit_rate 256k;  

  170.         #DNS解析服务器的IP地址,可以在IE 工具-Internet选项-连接-局域网设置-代理服务器 中设置代理服务器IP地址和端口  

  171.         resolver 8.8.8.8;  

  172.         # 默认主页目录在nginx安装目录的html子目录。  

  173.             root   html;  

  174.             index  index.jsp index.htm;  

  175.         proxy_pass  http://localhost;  

  176.         #3. 没有索引页时,罗列文件和子目录  

  177.         autoindex on;  

  178.         proxy_set_header Host $host;  

  179.         proxy_set_header X-Real-IP $remote_addr;  

  180.         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  

  181.         #使用http区域定义的cache名称。  

  182.         #proxy_cache cache_one;  

  183.         #访问出现错误,500,502等错误,转向另外tomcat  

  184.         #proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;  

  185.         #200 304状态缓存3小时  

  186.         proxy_cache_valid  200 304 3h;  

  187.         }  

  188.   

  189.         # proxy the PHP scripts to Apache listening on 127.0.0.1:80  

  190.         #  

  191.         #location ~ \.php$ {  

  192.         #    proxy_pass   http://127.0.0.1;  

  193.         #}  

  194.   

  195.         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000  

  196.         #  

  197.         #location ~ \.php$ {         

  198.         #    root           html;  

  199.         #    fastcgi_pass   127.0.0.1:9000;  

  200.         #    fastcgi_index  index.php;  

  201.         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;  

  202.         #    include        fastcgi_params;  

  203.         #}  

  204.   

  205.         # deny access to .htaccess files, if Apache's document root  

  206.         # concurs with nginx's one  

  207.         #  

  208.         #location ~ /\.ht {  

  209.         #    deny  all;  

  210.         #}  

  211.     #location /nginxStatus{  

  212.     #    sub_status on;  

  213.     #    access_log logs/nginxLog.log;  

  214.     #    auth_basic "nginxStatus";  

  215.        #auth_basic_user_file conf/htpasswd;  

  216.        #allow all;   #例:allow 10.0.0.1/24  

  217.        #deny all;  

  218.     #}  

  219.     }  

  220.       

  221.     # another virtual host using mix of IP-, name-, and port-based configuration  

  222.     #  

  223.     #server {  

  224.     #    listen       8000;  

  225.     #    listen       somename:8080;  

  226.     #    server_name  somename  alias  another.alias;  

  227.   

  228.     #    location / {  

  229.     #        root   html;  

  230.     #        index  index.html index.htm;  

  231.     #    }  

  232.     #}  

  233.   

  234.   

  235.     # HTTPS server  

  236.     #  

  237.     #server {  

  238.     #    listen       443 ssl;  

  239.     #    server_name  localhost;  

  240.   

  241.     #    ssl_certificate      cert.pem;  

  242.     #    ssl_certificate_key  cert.key;  

  243.   

  244.     #    ssl_session_cache    shared:SSL:1m;  

  245.     #    ssl_session_timeout  5m;  

  246.   

  247.     #    ssl_ciphers  HIGH:!aNULL:!MD5;  

  248.     #    ssl_prefer_server_ciphers  on;  

  249.   

  250.     #    location / {  

  251.     #        root   html;  

  252.     #        index  index.html index.htm;  

  253.     #    }  

  254.     #}  

  255. }  

 

Gzip.conf代码  收藏代码

  1. #gzip模块设置  

  2.   

  3. gzip on; #告诉nginx采用gzip压缩的形式发送数据  

  4.   

  5. #指定的客户端禁用gzip功能。我们设置成IE6或者更低版本以使我们的方案能够广泛兼容。  

  6. gzip_disable 'msie6';  

  7.   

  8. #告诉nginx在压缩资源之前,先查找是否有预先gzip处理过的资源。这要求你预先压缩你的文件(在这个例子中被注释掉了),  

  9. #从而允许你使用最高压缩比,这样nginx就不用再压缩这些文件了  

  10. #gzip_static on;  

  11.   

  12. #允许或者禁止压缩基于请求和响应的响应流。我们设置为any,意味着将会压缩所有的请求。  

  13. gzip_proxied any;  

  14.   

  15. #最小压缩文件大小。如果一个请求小于1000字节,我们最好不要压缩它,因为压缩这些小的数据会降低处理此请求的所有进程的速度。  

  16. gzip_min_length 1k;  

  17.   

  18. #设置数据的压缩等级。这个等级可以是1-9之间的任意数值,9是最慢但是压缩比最大的。我们设置为4,这是一个比较折中的设置。  

  19. gzip_comp_level 4;  

  20.   

  21. #设置需要压缩的数据格式  

  22. gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript; 


转载于:https://my.oschina.net/u/2533897/blog/538767

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值