Nginx配置基础

来源:互联网 发布:网络用语钓鱼是啥意思 编辑:程序博客网 时间:2024/05/01 12:15

nginx做反向代理,简单理解为为后端服务器做请求分发,能够拦截一些非法请求,动态扩充或者摘除服务器,负载均衡。

重点在nginx.conf配置文件

//以下为全局配置**********worker_processes 4;        //定义工作线程数,一般nginx都是独立的服务器,配置机器核心数error_log logs/error.log info;    //配置日志的输出和输出级别pid logs/nginx.pid;    //线程日志events {    ues epoll;    //对于linux2.6以上系统,用epoll模式,对于    worker_connections 1024;    //每个线程最大能处理的链接数,    //理论上max clients = worker_processes * worker_connections)//以下为http服务请求处理配置***********http {    //若干个upstream模块,每一个代表一个集群,里面定义若干个server项,每一项对应一个后端服务器    //upstream模块会内部会做负载均衡,默认机制是轮询。每个upstream模块在一个http模块内通用    upstream some_upstream_modle {        //还可以按照ip来做hash,只需要配置 ip_hash; 如果想按照url做hash,需要安装插件        server ip1:port1;    //定义一个后端服务器        server ip2:port2;    //定义一个后端服务器        //...    }    //若干个server模块,表示不同的虚拟机,一个虚拟机从语义上提供一类服务;    //不同的虚拟机是监听不同的端口,有不同的域名    server {        listen 8080;        server_name test.com;        //若干个location模块,用来匹配不同的uri,这是前缀匹配        //里面的proxy_pass表示这个匹配的uri会到转发到具体哪个服务器去处理        location /test0 {            //转发的地址,可以是某个域名,或是一个upstream            proxy_pass http://some_upstream_modle;            }        // ~ 表示开启正则匹配,匹配区分大小写        location ~ /Test1 {            proxy_pass http://www.test.com;        }        // ~* 表示开启正则匹配,匹配不区分大小写        location ~* /Test2 {            proxy_pass http://some_upstream_modle;        }    }}

对于http部分,重点是upstream模块,server模块,location模块
nginx对于location的匹配规则是先前缀匹配,找到所有前缀匹配中最长的,然后在去寻找模式匹配,如果找到模式匹配,则使用模式匹配,如果没有,则使用最长前缀匹配。

参考链接:
[1]http://nginx.org/en/docs/beginners_guide.html
[2]https://www.digitalocean.com/community/tutorials/understanding-nginx-http-proxying-load-balancing-buffering-and-caching
[3]http://www.cnblogs.com/xiaogangqq123/archive/2011/03/02/1969006.html

0 0