nginx 配置文件详解

#运行用户
user nobody;
#启动进程,通常设置成和cpu的数量相等
worker_processes  1;

#全局错误日志及PID文件
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

#工作模式及连接数上限
events {
    #epoll是多路复用IO(I/O Multiplexing)中的一种方式,
    #仅用于linux2.6以上内核,可以大大提高nginx的性能
    use   epoll;

    #单个后台worker process进程的最大并发链接数    
    worker_connections  1024;

    # 并发总数是 worker_processes 和 worker_connections 的乘积
    # 即 max_clients = worker_processes * worker_connections
    # 在设置了反向代理的情况下,max_clients = worker_processes * worker_connections / 4  为什么
    # 为什么上面反向代理要除以4,应该说是一个经验值
    # 根据以上条件,正常情况下的Nginx Server可以应付的最大连接数为:4 * 8000 = 32000
    # worker_connections 值的设置跟物理内存大小有关
    # 因为并发受IO约束,max_clients的值须小于系统可以打开的最大文件数
    # 而系统可以打开的最大文件数和内存大小成正比,一般1GB内存的机器上可以打开的文件数大约是10万左右
    # 我们来看看360M内存的VPS可以打开的文件句柄数是多少:
    # $ cat /proc/sys/fs/file-max
    # 输出 34336
    # 32000 < 34336,即并发连接总数小于系统可以打开的文件句柄总数,这样就在操作系统可以承受的范围之内
    # 所以,worker_connections 的值需根据 worker_processes 进程数目和系统可以打开的最大文件总数进行适当地进行设置
    # 使得并发总数小于操作系统可以打开的最大文件数目
    # 其实质也就是根据主机的物理CPU和内存进行配置
    # 当然,理论上的并发总数可能会和实际有所偏差,因为主机还有其他的工作进程需要消耗系统资源。
    # ulimit -SHn 65535

}


http {
    #设定mime类型,类型由mime.type文件定义
    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"';

    access_log  logs/access.log  main;

    #sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,
    #对于普通应用,必须设为 on,
    #如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,
    #以平衡磁盘与网络I/O处理速度,降低系统的uptime.
    sendfile     on;
    #tcp_nopush     on;

    #连接超时时间
    #keepalive_timeout  0;
    keepalive_timeout  65;
    tcp_nodelay     on;

    #开启gzip压缩
    gzip  on;
    gzip_disable "MSIE [1-6].";

    #设定请求缓冲
    client_header_buffer_size    128k;
    large_client_header_buffers  4 128k;


    #设定虚拟主机配置
    server {
        #侦听80端口
        listen    80;
        #定义使用 www.nginx.cn访问
        server_name  www.nginx.cn;

        #定义服务器的默认网站根目录位置
        root html;

        #设定本虚拟主机的访问日志
        access_log  logs/nginx.access.log  main;

        #默认请求
        location / {
            
            #定义首页索引文件的名称
            index index.php index.html index.htm;   

        }

        # 定义错误提示页面
        error_page   500 502 503 504 /50x.html;
        location = /50x.html {
        }

        #静态文件,nginx自己处理
        location ~ ^/(images|javascript|js|css|flash|media|static)/ {
            
            #过期30天,静态文件不怎么更新,过期可以设大一点,
            #如果频繁更新,则可以设置得小一点。
            expires 30d;
        }

        #PHP 脚本请求全部转发到 FastCGI处理. 使用FastCGI默认配置.
        location ~ .php$ {
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include fastcgi_params;
        }

        #禁止访问 .htxxx 文件
            location ~ /.ht {
            deny all;
        }

    }
}


真实文件

user  nobody;
worker_processes  2;

error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  51200;
    use                epoll;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    autoindex on;


proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Real-IP $remote_addr;

    log_format  main  '"$http_x_forwarded_for" - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$remote_addr" "$connection" "$msec" "$request_time" "$upstream_response_time"';

    access_log  logs/access.log  main;


    sendfile       on;
    tcp_nopush     on;
    tcp_nodelay    on;
    keepalive_timeout  60;

  fastcgi_connect_timeout 300;
  fastcgi_send_timeout 300;
  fastcgi_read_timeout 300;
  fastcgi_buffer_size 64k;
  fastcgi_buffers 4 64k;
  fastcgi_busy_buffers_size 128k;
  fastcgi_temp_file_write_size 256k;

  gzip on;
  gzip_min_length  1k;
  gzip_buffers     4 16k;
  gzip_http_version 1.0;
  gzip_comp_level 9;
  gzip_types   text/plain application/x-javascript text/css application/xml image/png image/gif image/jpeg application/x-shockwave-flash;
  gzip_vary on;
add_header via $hostname;
server_tokens off;
fastcgi_intercept_errors on;

limit_zone  one $binary_remote_addr 30m;
limit_req_zone $http_x_forwarded_for zone=allips:30m rate=50r/s;


    server {
        listen       80;
        server_name  172.27.5.41;
        #charset koi8-r;


        client_max_body_size            6m;
        client_body_timeout             10m;

        #access_log  logs/host.access.log  main;

if ($http_user_agent ~ ^$) {
return 503;
}



        location / {
        root   /home/web;
        index  index.php index.html index.htm index.shtml;
limit_conn  one 10;
limit_req zone=allips burst=5 nodelay;



      rewrite ^/jifen/131101\/?$ /exchange/zhuanti/iphone/index.php last;  #积分活动
          rewrite ^/jifen\/?$      /exchange/index.php last;                        #积分首页
          rewrite ^/jifen/guize\/?$  /exchange/guize.php last;                  #积分规则页
          rewrite ^/jifen/([0-9]+)\.html$ /exchange/product.php?pid=$1 last;   #积分商品详情页
          rewrite ^/jifen/success.html$ /exchange/success.php?s=$1 last;      #兑换成功页


        #小区
        rewrite "^/xiaoqu/(\d+)\.html$" /xq.php?g=$1 last;
        rewrite "^/xiaoqu/(\w)/$" /xiaoqu/all.php?g=$1 last;
        rewrite "^/xiaoqu/(\w)/pg(\d+)/$" /xiaoqu/all.php?g=$1&p=$2 last;
        rewrite "^/xiaoqu/([^0-9].*)" /xiaoqu/index.php?g=$1 last;
        rewrite ^/c-(.*)/kpj/$ /xiangqing/kpj.php last;
        rewrite ^/c-(.*)/kpj/pg([\d]+)/$ /xiangqing/kpj.php?pg=$2 last;
        rewrite "^/c-(.*)/kpj/([\w]{1,33})\.shtml$" /xiangqing/kpjdetail.php?id=$2&canshu=$1 last;
        rewrite "^/c-(.*)/(xq|zf|esf|pt|jiage|tu|xxs)/$" /xiangqing/$2.php?allget=$1 last;
        rewrite "^/c-(.*)/(xq|zf|esf|pt|jiage|tu|xxs)/(.*)/$" /xiangqing/$2.php?allget=$1&others=$3 last;
        rewrite "^/c-(.*)/$" /xiangqing/index.php?allget=$1 last;

        #问答   
                rewrite "^/ask/key-(.*)/d([\w]+)/pg([\w]+)/$" /ask/search.php?g=$1&d=$2&pg=$3 last;
                rewrite "^/ask/key-(.*)/pg([\w]+)/$" /ask/search.php?g=$1&pg=$2 last;
                rewrite "^/ask/key-(.*)/d([\w]+)/$" /ask/search.php?g=$1&d=$2 last;
                rewrite "^/ask/key-(.*)/$" /ask/search.php?g=$1 last;
                rewrite "^/ask/id([\w]+)/d([\w]+)/pg([\w]+)/$" /ask/all.php?id=$1&d=$2&pg=$3 last;
                rewrite "^/ask/id([\w]+)/pg([\w]+)/$" /ask/all.php?id=$1&pg=$2 last;
                rewrite "^/ask/id([\w]+)/d([\w]+)/$" /ask/all.php?id=$1&d=$2 last;
                rewrite "^/ask/id([\w]+)/$" /ask/all.php?id=$1 last;
                rewrite "^/ask/(esf|zl)-([\w]+).html$" /ask/xq.php?fl=$1&g=$2 last;

        #经纪人
        rewrite ^/jingjiren/(esf|zf|ask)-([A-Z0-9]+)/(.*)$ /jingjiren/redirection.php?m=$1&id=$2&params=$3 last;
        rewrite ^/jingjiren/(h|esf|zf|jianjie|ask)-([A-Z0-9]+)/$ /jingjiren/redirection.php?m=$1&id=$2 last;       
        rewrite ^/jingjiren/(\w)/$ /jingjiren/all.php?g=$1 last;
        rewrite ^/jingjiren/(\w)/pg(\d+)/$ /jingjiren/all.php?g=$1&p=$2 last;
        rewrite ^/jingjiren/(.*)/$ /jingjiren/index.php?allget=$1 last;

        #added by masx 2012-01-17 for pinggu
        rewrite ^/pinggu/p([0-9]+)$ /pinggu/pinggu.php?bdm_id=$1 last;
        rewrite ^/pinggu/p([0-9]+)_([0-9]+)$ /pinggu/pinggu_baogao.php?id=$2 last;
        rewrite ^/zhuanti/zhenshifangyuan/$ /zhuanti/zhenfangyuant1231.html permanent;
        rewrite ^/zhuanti/lingjuli.html /zhuanti/gtlingjuli permanent;
        rewrite "^/zhuanti/fabuhui/$"  /zhuanti/zhenfangyuan/index.php last;
        rewrite "^/sitemap/$" /sitemap/index.php last;
        rewrite "^/about/about_fenbu/(.*)$" /about/about_fenbu.php?g=$1 last;       

        #add by cuisy at 20120801
        rewrite  ^/help/zc_([0-9]+)\.shtml$  /help/zhengce/content.php?aid=$1 last;
        rewrite  ^/help/jz_([0-9]+)\.shtml$  /help/jz/content.php?aid=$1 last;
        rewrite  ^/help/shuifei\/?$  /help/daikuan/list.php?type=sf last;
        rewrite  ^/help/daikuan/gongjijin\/?$  /help/daikuan/list.php?type=gjj last;
        rewrite  ^/help/daikuan/shangye\/?$  /help/daikuan/list.php?type=sy last;
        rewrite  ^/help/daikuan/hunhe\/?$  /help/daikuan/list.php?type=hh last;
        rewrite  ^/help/hetong/zufang/?$  /help/htxz/list.php?type=zf last;
        rewrite  ^/help/hetong/maifang\/?$  /help/htxz/list.php?type=mm last;
        rewrite  ^/help/htzf_([0-9]+)\.shtml$  /help/htxz/content.php?aid=$1&type=zf last;
        rewrite  ^/help/htmf_([0-9]+)\.shtml$  /help/htxz/content.php?aid=$1&type=mm last;
        rewrite ^/help/fangchan/esf\/?$  /help/fangchan/list.php?type=esf last;
        rewrite ^/help/fangchan/sf\/?$  /help/fangchan/list.php?type=sf last;
        rewrite ^/help/fangchan/zf\/?$  /help/fangchan/list.php?type=zf last;
        rewrite ^/help/fangchan/maimai/?$  /help/fangchan/list.php?type=maimai last;
        rewrite ^/help/fcgh_([0-9]+)\.shtml$  /help/fangchan/content.php?aid=$1&type=esf last;
        rewrite ^/help/fcsf_([0-9]+)\.shtml$  /help/fangchan/content.php?aid=$1&type=sf last;
        rewrite ^/help/fczf_([0-9]+)\.shtml$  /help/fangchan/content.php?aid=$1&type=zf last;
        rewrite ^/help/fcmm_([0-9]+)\.shtml$  /help/fangchan/content.php?aid=$1&type=maimai last;
        rewrite ^/help/zhuyi/esf\/?$  /help/zhuyi/list.php?type=esf last;
        rewrite ^/help/zhuyi/sf\/?$  /help/zhuyi/list.php?type=sf last;
        rewrite ^/help/zhuyi/zf\/?$  /help/zhuyi/list.php?type=zf last;
        rewrite ^/help/zhuyi/maimai/?$  /help/zhuyi/list.php?type=maimai last;
        rewrite ^/help/zygh_([0-9]+)\.shtml$  /help/zhuyi/content.php?aid=$1&type=esf last;
        rewrite ^/help/zysf_([0-9]+)\.shtml$  /help/zhuyi/content.php?aid=$1&type=sf last;
        rewrite ^/help/zyzf_([0-9]+)\.shtml$  /help/zhuyi/content.php?aid=$1&type=zf last;
        rewrite ^/help/zymm_([0-9]+)\.shtml$  /help/zhuyi/content.php?aid=$1&type=maimai last;
        rewrite ^/help/dksf_([0-9]+)\.shtml$  /help/daikuan/content.php?aid=$1&type=sf last;
        rewrite ^/help/dkgj_([0-9]+)\.shtml$  /help/daikuan/content.php?aid=$1&type=gjj last;
        rewrite ^/help/dksy_([0-9]+)\.shtml$  /help/daikuan/content.php?aid=$1&type=sy last;
        rewrite ^/help/dkhh_([0-9]+)\.shtml$  /help/daikuan/content.php?aid=$1&type=hh last;

        #二手房
        rewrite "^/ershoufang/([\w]{1,12})\.shtml$" /ershoufang_detail/details.php?g=$1 last;
        rewrite ^/ershoufang/pinglun/pg([\d]+)/([\w]+)\.shtml$ /ershoufang_detail/fypl.php?pg=$1&g=$2 last;
        rewrite ^/ershoufang/pinglun/([\w]+)\.shtml$ /ershoufang_detail/fypl.php?g=$1 last;
        rewrite "^/ershoufang/duibi/$" /ershoufang/duibi.php last;
        rewrite "^/ershoufang/ditie/(.*)$" /ershoufang/ditie.php?g=$1 last;
        rewrite "^/ershoufang/(.*[^/])$" /ershoufang/$1/ permanent;
        rewrite "^/ershoufang/(.*)$" /ershoufang/index.php?g=$1 last;

        #已售
        rewrite "^/sold$" /sold/ permanent;
        rewrite "^/sold/([\w]{1,12})\.shtml$" /yishou/detail.php?g=$1 last;
        rewrite "^/sold/(.*)$" /yishou/index.php?g=$1 last;

        #租房
        rewrite "^/zufang/([\w]{1,12})\.html$" /ziroom/details.php?g=$1 last;
        rewrite "^/zufang/hezufang/(.*)$" /ziroom/index.php?g=$1 last;

        rewrite "^/zufang/([\w]{1,12})\.shtml$" /zufang_detail/details.php?g=$1 last;
        rewrite "^/zufang/pinglun/pg([\d]+)/([\w]+)\.shtml$" /zufang_detail/fypl.php?pg=$1&g=$2 last;
        rewrite "^/zufang/pinglun/([\w]+)\.shtml$" /zufang_detail/fypl.php?g=$1 last;
        rewrite "^/zufang/duibi/$" /zufang/duibi.php last;
        rewrite "^/zufang/ditie(.*)$" /zufang/ditie.php?g=$1 last;
        rewrite "^/zufang/(.*[^/])$" /zufang/$1/ permanent;
        rewrite "^/zufang/(.*)$" /zufang/index.php?g=$1 last;



   
        #学区房
        rewrite "^/school/list/(.*)$" /school/list.php?g=$1 last;
        rewrite "^/school/([\w]+)\.shtml$" /school_detail/xqf_intro.php?g=$1 last;
        rewrite "^/school/zsjz/([\w]+)\.shtml$" /school_detail/xqf_zsjz.php?g=$1 last;
        rewrite "^/school/xxpl/([\w]+)\.shtml$" /school_detail/xqf_xxpl.php?g=$1 last;
        rewrite "^/school/cjjl/([\w]+)\.shtml$" /school_detail/xqf_cjjl.php?g=$1 last;
        rewrite "^/school/hpxq/([\w]+)\.shtml$" /school_detail/xqf_hpxq.php?g=$1 last;
        rewrite "^/school/esfy/([\w]+)/([\w]+)\.shtml$" /school_detail/xqf_esfy.php?other=$1&g=$2 last;
        rewrite "^/school/esfy/([\w]+)\.shtml$" /school_detail/xqf_esfy.php?g=$1 last;
        rewrite "^/school/zf/([\w]+)/([\w]+)\.shtml$" /school_detail/xqf_zf.php?other=$1&g=$2 last;
        rewrite "^/school/zf/([\w]+)\.shtml$" /school_detail/xqf_zf.php?g=$1 last;
        rewrite "^/school/xxpl/pg([\d]+)/([\w]+)\.shtml$" /school_detail/xqf_xxpl.php?pg=$1&g=$2 last;
        rewrite "^/school/cjjl/pg([\d]+)/([\w]+)\.shtml$" /school_detail/xqf_cjjl.php?pg=$1&g=$2 last;
        rewrite "^/school/xxxx/([\w]+)\.shtml$" /school_detail/xqf_xxxx.php?g=$1 last;

        #商铺
            rewrite "^/shangpuchushou/([\w]{1,12})\.shtml$" /shangpu_detail/details_chushou.php?g=$1 last;
            rewrite "^/shangpuchuzu/([\w]{1,12})\.shtml$" /shangpu_detail/details_chuzu.php?g=$1 last;
            rewrite "^/shangpuchushou/pinglun/([\w]+)\.shtml$" /shangpu_detail/fypl_chushou.php?g=$1 last; 
        rewrite "^/shangpuchuzu/pinglun/([\w]+)\.shtml$" /shangpu_detail/fypl_chuzu.php?g=$1 last; 
        rewrite "^/shangpuchushou/duibi/$" /shangpu/duibi_chushou.php?g=$1 last;
        rewrite "^/shangpuchuzu/duibi/$" /shangpu/duibi_chuzu.php?g=$1 last;
            rewrite "^/shangpuchuzu/(.*)$" /shangpu/chuzu.php?g=$1 last;
        rewrite "^/shangpuchushou/(.*)$" /shangpu/chushou.php?g=$1 last;
       

        #news
        rewrite "^/news/([0-9]+)\.html$" /news/details.php?g=$1 last;
        rewrite "^/news/([\w]+)/pg([0-9]+)/$" /news/list.php?aid=$1&pg=$2 last;
            rewrite "^/news/([\w]+)\/(.*)/pg([0-9]+)/$" /news/list.php?aid=$1&cid=$2&pg=$3 last;
            rewrite "^/news/([\w]+)/(.*)$" /news/list.php?aid=$1&cid=$2 last;
        rewrite "^/news/rs(.*)/pg([0-9]+)/$" /news/list.php?key=$1&pg=$2 last;
        rewrite "^/news/rs(.*)/$" /news/list.php?key=$1 last;
        rewrite "^/news/([\w]+)/$" /news/list.php?aid=$1 last;
        ssi on;
        ssi_silent_errors off;
        ssi_types text/shtml;

        }

    #location   /zhuanti/ {
       autoindex on;
       autoindex_exact_size off;
       autoindex_localtime on;
        }



 error_page  404 = http://beijing.homelink.com.cn/404/index.shtml;
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
       

        location ~* ^/data/.*\.(php|php5)$ {

                deny all;
        }

        location ~ ^/status$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/php5.3/share/php/fpm$fastcgi_script_name;
            include        fastcgi_params;

        }





        location ~ \.php$ {
            root           web;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /home/web$fastcgi_script_name;
            include        fastcgi_params;

        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
          deny  all;
        #}


        location /Ngs {
           stub_status on;
           access_log on;
           auth_basic "NginxStatus";
        }
        location ~* \.(js|css|jpg|jpeg|gif|png|swf)$ {
                        root /home/web;
                        expires    2h;
        #add_header X-Cache    '$host';
   valid_referers none blocked  *.homelink.com.cn;
                 if ($invalid_referer) {
                 return 404;                 
                   }

        }

    }

server {
    listen       80;
    server_name  *.beijing.homelink.com.cn;

    location / {
    root   /home/web/jingjiren_domain;
        index  index.php index.html index.htm index.shtml;

    rewrite ^/(esf|zf|ask|jianjie)/(.*)$ /router.php?m=$1&others=$2 last;
    rewrite ^/(esf|zf|ask|jianjie)/$ /router.php?m=$1 last;
    rewrite ^/(esf|zf|ask|jianjie)$ /$1/ permanent;
    rewrite ^/$ /router.php last;

        ssi on;
        ssi_silent_errors off;
        ssi_types text/shtml;

    }
    error_page  404 = http://beijing.homelink.com.cn/404/index.shtml;

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }

    location ~ \.php$ {
        root           web;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  router.php;
        fastcgi_param  SCRIPT_FILENAME  /home/web/jingjiren_domain$fastcgi_script_name;
        include        fastcgi_params;

    }

}

    # HTTPS server
    #
    server {
        listen       80;
        server_name  wwwtest.homelink.com.cn;


        location / {
            root   /home/html/webroot;
            index  index.php index.html index.htm;

rewrite "^/news/([0-9]+).html$" /news/index.php?id=$1 last;
rewrite "^/news/([\d]+)_([\d]*).html$" /news/index.php?id=$1&p=$2 last;


        }

    location   /zhuanti/ {
        autoindex on;
        autoindex_exact_size off;
        autoindex_localtime on;
         }

        location ~ \.php$ {
            root           webroot;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /home/html/webroot$fastcgi_script_name;
            include        fastcgi_params;

        }

    }

    server {
        listen       80;
        server_name  status.homelink.com.cn;

access_log  logs/status.access.log  main;
error_log   logs/status.error.log;

    #location / {
      root    /usr/local/php5.3/share/php/fpm;
      index    index.php index.html;
    #}


        location ~ ^/status$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;

        }
   
        location ~ \.php$ {
            root           /usr/local/php5.3/share/php/fpm;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;

        }

    }

}




posted @ 2014-05-15 10:48  张三_zhangsan  阅读(622)  评论(0编辑  收藏  举报