【转】nginx配置详解

    # 顶层配置信息管理服务器级别行为 
    worker_processes  1; 
     
    # event指令与事件模型有关,配置处理连接有关信息 
    events { 
        worker_connections  1024; 
    } 
     
    # http指令处理http请求 
    http { 
        # mime type映射 
        include       mime.types; 
        default_type  application/octet-stream; 
     
        sendfile        on; 
        #tcp_nopush     on; 
     
        #keepalive_timeout  0; 
        keepalive_timeout  65; 
     
        #gzip  on; 
     
        # server 表示一个虚拟主机,一台服务器可配置多个虚拟主机 
        server { 
            # 监听端口 
            listen       80; 
            # 识别的域名 
            server_name  localhost; 
     
            # 一个关键设置,与url参数乱码问题有关 
            charset utf-8; 
     
            #access_log  logs/host.access.log  main; 
     
            #location表达式: 
            #syntax: location [=|~|~*|^~|@] /uri/ { … } 
            #分为两种匹配模式,普通字符串匹配,正则匹配 
            #无开头引导字符或以=开头表示普通字符串匹配 
            #以~或~* 开头表示正则匹配,~*表示不区分大小写 
            #多个location时匹配规则 
            #总体是先普通后正则原则,只识别URI部分,例如请求为/test/1/abc.do?arg=xxx 
            #1. 先查找是否有=开头的精确匹配,即location = /test/1/abc.do {...} 
            #2. 再查找普通匹配,以 最大前缀 为规则,如有以下两个location 
            #   location /test/ {...} 
            #   location /test/1/ {...} 
            #   则匹配后一项 
            #3. 匹配到一个普通格式后,搜索并未结束,而是暂存当前结果,并继续再搜索正则模式 
            #4. 在所有正则模式location中找到第一个匹配项后,以此匹配项为最终结果 
            #   所以正则匹配项匹配规则受定义前后顺序影响,但普通匹配不会 
            #5. 如果未找到正则匹配项,则以3中缓存的结果为最终结果 
            #6. 如果一个匹配都没有,返回404 
             
            #location =/ {...} 与 location / {...} 的差别 
            #前一个是精确匹配,只响应/请求,所有/xxx类请求不会以前缀匹配形式匹配到它 
            #而后一个正相反,所有请求必然都是以/开头,所以没有其它匹配结果时一定会执行到它 
             
            #location ^~ / {...} ^~意思是非正则,表示匹配到此模式后不再继续正则搜索 
            #所有如果这样配置,相当于关闭了正则匹配功能 
            #因为一个请求在普通匹配规则下没得到其它普通匹配结果时,最终匹配到这里 
            #而这个^~指令又相当于不允许正则,相当于匹配到此为止 
            location / { 
                root   html; 
                index  index.html index.htm; 
                # deny all; 拒绝请求,返回403 
                # allow all; 允许请求 
            } 
             
            location /test/ { 
                deny all; 
            } 
             
            location ~ /test/.+\.jsp$ { 
               proxy_pass  http://192.168.1.62:8080; 
            } 
     
            location ~ \.jsp$ { 
               proxy_pass  http://192.168.1.61:8080; 
            } 
     
            # 定义各类错误页 
            error_page  404              /404.html; 
     
            # redirect server error pages to the static page /50x.html 
            # 
            error_page   500 502 503 504  /50x.html; 
            location = /50x.html { 
                root   html; 
            } 
             
            # @类似于变量定义 
            # error_page 403 http://www.jikexueyuan.com这种定义不允许,所以利用@实现 
            error_page 403 @page403; 
            location @page403 { 
                proxy_pass http://http://www.jikexueyuan.com; 
            } 
        } 
     
     
        # another virtual host using mix of IP-, name-, and port-based configuration 
         
        server { 
            listen       80; 
            #listen 9090 
            server_name  www.test.com test.com other.cc; 
     
            location / { 
                # root d:\\test; 注意,win下d:\test因转义符问题不允许 
                root d:/test 
                index  index.html index.htm; 
            } 
        } 
    } 

原博:http://jaychang.iteye.com/blog/2259694

猜你喜欢

转载自girl-luo.iteye.com/blog/2327517