码迷,mamicode.com
首页 > 其他好文 > 详细

Nginx配置段(3)

时间:2019-01-08 12:23:48      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:根据   报文   ane   install   文件的   变量   keep   表示   web服务器   

安装方法: rpm及源码安装: # ./configure --prefix=/usr/local/nginx --conf-path=/etc/nginx/nginx.conf --user=nginx --group=nginx --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module --with-http_mp4_module --http-client-body-temp-path=/var/tmp/nginx/client --http-proxy-temp-path=/var/tmp/nginx/proxy --http-fastcgi-temp-path=/var/tmp/nginx/fastcgi # make && make install Tmalloc, gperftools

配置段:
nginx:
main配置段
http {
}

http配置:http core 配置一个静态web服务器
    ngx_http_core_module

    配置框架:
    http {
        upstream {
            .,..
        }

        server {
            listen IP:PORT;
            # 虚拟主机
            location /URL {
                if ...{
                    ...
                }    #类似于httpd中的<Location>,用于定义URL与本地文件系统的映射关系;
                root "/path/to/somewhere";
                ...
            }  
        }       #每个server类似于httpd中的一个<VirtualHost>

        server {
            ,,. 
        }  
    }

        注意:与http配置相关的指令必须放在http、server、location、upstream、if块中;

    虚拟主机相关的配置:
        1、server {}
            定义一个虚拟主机;

            server {
                listen 8080;
                server_name www.zhanx.wang;
                root "/vhosts/web1";
            }

        2、listen
            监听的端口
            完整格式 :listen address[:port] [default_server] [ssl] [spdy] [proxy_protocol] [setfib=number] [fastopen=number] [backlog=number] [rcvbuf=size] [sndbuf=size] [accept_filter=filter] [deferred] [bind] [ipv6only=on|off] [so_keepalive=on|off|[keepidle]:[keepintvl]:[keepcnt]];

            listen address[:port] [default_server] ssl 

            backlog=number: 指明TCP协议backlog队列的大小。默认为-1,表示不设置;
            rcvbuf=size:设定监听句柄的SO_RCVBUF参数;

            例如:
                listen 172.16.100.8:8080

        3、server_name name [...];
            后可跟多个主机名;名称还可以使用通配符和正则表达式(~);

            (1) 先做精确匹配;www.zhanxwang: 
            (2) 左侧通配符匹配,例如:*.zhanx.wang; 
            (3) 右侧通配符匹配,例如:www.*;
            (4) 正则表达式匹配,例如: ~^.*\.zhanx\.wang$
            (5) default_server

        4、location [=|~|~*|^~] /uri {...}
            location @name
            功能:允许根据用户请求的URI来匹配定义的各location,匹配到时,此请求将被相应的location块中的配置所处理;

                =: 精确匹配检查;
                ~: 正则表达式模式匹配,区分字符大小写;
                ~*:正则表达式模式 匹配,不区分字符大小写;
                ^~:URI的前半部分匹配,不检查正则表达式;

            匹配优先级:精确匹配(=)、^~、~和~*、由不带符号的URL进行左侧匹配;

            server {
                listen 80
                server_name www.zhanx.wang
                location / {
                        root "/vhosts/web1";
                    }

                location /images/ {
                        root "vhosts/images";
                    }

                location ~*\.php${
                        fcgipass
                    }
                }

        5、root
            设置web资源路径映射;用于指明请求的URL所对应的文档的根目录路径;

            location /images/ {
                root "/web/imgs/";
            }

        6、alias path
            用于location配置段,定义路径别名 

            location /images/ {
                alias /www/pictures/;
            }
            http://www.zhanx.wang/image/a.jpg  <--- /www/pictures/a.jpg

            注意:root表示指明路径为对应location的“ /” URL;
            alias表示路径映射,即location中的URL是相对于alias所指明的路径而言;

        7、index file
            默认主页面
                index  index.php  index.html; 

        8、error_page code [...] [=code] URI | @name
            根据http状态码重定向错误页面
            error_page  404   /404.html

            =[code]: 以指定的响应码进行响应;省略code表示以新资源的响应码为响应码;

        9、try_files
            try_files path1[,path2,...] URI 

        10、基于IP地址的访问控制;
            allow IP/Network;
            deny  IP/Network;

        11、基于用户的访问控制;
            basic,digest

            auth_basic  "";
            auth_basic_user_file "/PATH/TO/PASSWORD_FILE"
                账号密码文件建议使用htpasswd来创建;

        12、https服务
            生成私钥

        13、stub_status {on|off};
            仅能用于location上下文;

            location /status {
                stub_status on;
                allow 172.16.0.0/16;
                deny  all;
            }

            结果示例:

            Active connection: 6 #当前所有处于打开状态的连接数;
            server accepts handled requests
            241    241     431
            (1)已经接受的连接
            (2)已经处理过的连接数
            (3)已经处理过的请求数:在“保持连接”模式下,请求数量可能会多于连接数量;
            Reading:0 Writing:1 Waiting:5
                Reading:正处于接收请求状态的连接数;
                Writing:请求已经接收完成,正在处理请求或发送响应的过程中的连接数;
                waiting:保持连接模式,且处于活动状态的连接数;

        14、rewrite regex replacement flag;

            例如:
                ...
                rewrite ^/images/(.*\.jpg)$ /imgs/$1 last;
                rewrite ^/imgs/(.*\.jpg)$ /images/$1 last;

                ...
                rewrite ^/images/(.*\.jpg)$ /imgs/$1 break;
                ...

                http://www.zhanx.wang/image/a/b/c/1.jpg --> /imgs/a/b/c/1.jpg

            flag:
                last:一旦此rewrite规则重写完成后,就不再被后面其它的rewrite规则进行处理,而是由user
                    Agent重新对重写后的url再一次发起请求,并从头开始执行类似的过程
                break:一旦此rewrite规则重写完成后,由user 
                    Agent 对新的URl重新发起请求,且不再会被当前location内的任何rewrite规则所检查
                redirect:以302响应码(临时重定向)返回新的URL;
                permanent:以301响应码(永久重定向)返回新的URL;

        15、if
            语法:if(condition){...}
            应用环境:server,location

            condition:
                (1) 变量名:
                    变量值为空串,或者以“0”开始,则为false;其他的均为true;
                (2) 以变量为操作数构成的比较表达式
                    可使用=,!=类似的比较操作符进行测试
                (3) 正则表达式的模式匹配操作
                    ~:区分大小写的模式匹配检查
                    ~*:不区分大小写的模式匹配检查
                    !~和!~*: 对上面两种测试取反
                (4) 测试路径为文件可能性:-f,!-f
                (5) 测试指定路径为目录的可能性:-d, !-d
                (6) 测试文件的存在性: -e, !-e
                (7) 检查文件是否有执行权限:-x,!-x

            例如:
                if ($http_user_agent ~* MSIE) {
                    rewrite ^(.*)$ /msie/$1 break;
                }

        16、防盗链
            location ~* \.(jpg|gif|jpeg|png)$ {
                valid_referer none blocked www.zhanx.wang
                if ($invalid_referer) {
                    rewrite ^/ http://www.zhanx.wang/403.html      #(盗链提示)
                }
            }

        17、定制访问日志格式
                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  /var/log/nginx/access.log  main;

            注意:此处可用变量为Nginx各模块内建变量;

    网络连接相关的配置:
        1、keepalive_timeout time;
            保持连接的超时时长,默认为75s;

        2、keepalive_requests #;
            在一次保持连接上允许承载最大资源请求数;

        3、keepalive_disable [msie6|safari|none]
            为指定类型的浏览器禁用长连接;

        4、tcp_nodelay on|off
            对长连接是否使用TCP_NODELAY选项;

        5、client_header_timeout time;
            读取http请求报文首部的超时时长;

        6、client_body_timeout time;
            读取http请求报文body部分的超时时长;

        7、send_timeout time;
            发送响应报文的超时时长;

Nginx配置段(3)

标签:根据   报文   ane   install   文件的   变量   keep   表示   web服务器   

原文地址:http://blog.51cto.com/zhanx/2339979

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!