nginx 配置详解

user www www;#使用哪个用户启动nginx 前面是用户,后面是组
worker_processes 4;#nginx工作的进程数量
#[ debug | info | notice | warn | error | crit ]   错误日志的级别及位置
error_log /var/htdocs/logs/nginx_error.log crit;
pid /usr/local/nginx/nginx.pid;#进程文件
worker_rlimit_nofile 51200;#一个nginx进程打开的最多文件描述符数目,理论值应该是最多打开文件数(ulimit -n)与nginx进程数相除,但是nginx分配请求并不是那么均匀,所以最好与ulimit -n的值保持一致。
#工作模式及连接数上限
events
{
     # use [ kqueue | rtsig | epoll | /dev/poll | select | poll ];
     use epoll; #使用epoll(linux2.6的高性能方式)
     worker_connections 51200; #每个进程最大连接数(最大连接=连接数x进程数)
}
#设定http服务器
http
{
     include       mime.types;#文件扩展名与文件类型映射表
     default_type application/octet-stream;#默认文件类型
     #日志文件格式
     log_format main ‘$remote_addr – $remote_user [$time_local] $request ‘
                  ‘"$status" $body_bytes_sent "$http_referer" ‘
                  ‘"$http_user_agent" "$http_x_forwarded_for"’;
    log_format download ‘$remote_addr – $remote_user [$time_local] ‘
                             ‘"$request" $status $bytes_sent ‘
                             ‘"$http_referer" "$http_user_agent" ‘
                             ‘"$http_range" "$sent_http_content_range"’;
     charset gb2312,utf-8;#默认编码    
     server_names_hash_bucket_size 128;#服务器名字的hash表大小    
     sendfile on;     #开启高效文件传输模式
     #以下两个选项用于防止网络阻塞
     tcp_nopush     on;
     tcp_nodelay on;
     keepalive_timeout 300; #超时时间
     #FastCGI是为了改善网站的性能--减少资源占用,提高访问速度.有关fastCGI的详细资料请参阅:http://www.fastcgi.com
     fastcgi_connect_timeout 300;
     fastcgi_send_timeout 300;
     fastcgi_read_timeout 300;
     fastcgi_buffer_size 128k;
     fastcgi_buffers 4 256k;
     fastcgi_busy_buffers_size 256k;
     fastcgi_temp_file_write_size 256k;
     fastcgi_temp_path /dev/shm;
     gzip on;     #打开gzip压缩
     gzip_min_length 1k;      #最小压缩文件大小
     gzip_buffers     4 8k;     #压缩缓冲区
     gzip_http_version 1.1;      #压缩版本(默认1.1,前端为squid2.5使用1.0)
    #压缩类型,默认就已经包含text/html 所以下面就不用再写了,当然写上去的话,也不会有问题,但是会有一个warn
     gzip_types       text/plain application/x-javascript text/css text/html text/javascript application/xml;
     #错误页面
     error_page 404 http://www.freeopens.com;
     error_page 403 http://www.freeopens.com;
     client_max_body_size 20m;      #上传文件大小限制
     #设定请求缓
     client_header_buffer_size 16k;
     large_client_header_buffers 4 64k;
     #设定负载均衡的服务器列表
     #如果在同一台机器上,单独起4组独立的php-cgi进程(每组8个子进程),性能应该不如1组php-cgi进程(32个子进程),因为1组进程,eaccelerator的PHP二进制文件缓存是共享的,1组进程命中率较高。
     #不过好处是,碰到某组的php假死的话,其他端口就可以接管了,我实测下来似乎发生502错误的概率降低了很多,或者说我这样配置以后还没有遇到
     upstream mysvr {
             #weigth参数表示权值,权值越高被分配到的机率越大
             #本机上的Squid开启3128端口
             server 192.168.8.1:3128 weight=5;
             server 192.168.8.2:80   weight=1;
             server 192.168.8.3:80   weight=6;
     }
     #虚拟主机的配置
     server
     {
             listen       80;
             server_name www.freeopens.com;
             index index.html Index.html index.htm index.php;
             root /var/htdocs/freeopens;
             if (-d $request_filename)
             {
                    rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
             }
             #设定本虚拟主机的访问日志
             access_log logs/www.freeopens.com.access.log main;
             location ~ .*\.php?$
             {
                  include fcgi.conf;
                  fastcgi_pass 127.0.0.1:9000;
                  fastcgi_index index.php;
             }
             #如果访问 /img/*, /js/*, /css/* 资源,则直接取本地文件,不通过squid
             #如果这些文件较多,不推荐这种方式,因为通过squid的缓存效果更好
             location ~ ^/(img|js|css)/ {
                     root    /var/htdocs/freeopens;
                     expires 24h;
             }
             #对 "/" 启用负载均衡
             location / {
                     proxy_pass      http://127.0.0.1;
                     proxy_redirect          off;
                     proxy_set_header        Host $host;
                     proxy_set_header        X-Real-IP $remote_addr;
                     proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
                     client_max_body_size    10m;
                     client_body_buffer_size 128k;
                     proxy_connect_timeout   90;
                     proxy_send_timeout      90;
                     proxy_read_timeout      90;
                     proxy_buffer_size       4k;
                     proxy_buffers           4 32k;
                     proxy_busy_buffers_size 64k;
                     proxy_temp_file_write_size 64k;
             }
             #设定查看Nginx状态的地址
             location /NginxStatus {
                     stub_status             on;
                     access_log              on;
                     auth_basic              "NginxStatus";
                     auth_basic_user_file conf/htpasswd;
             }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章