nginx偏运维, 不过做为开发应该了解它能作什么事情, 其做为技术架构的一部分必不可少css
正向代理是代理的客户端, 反向代理是代理的服务端. nginx就是一款能够做反向代理的web服务器.html
Apache, Nginx,Tomcat,WebLogic, iis, jetty, undertowe, resinnginx
Apache,Nginx 是同一类型的服务器, 均可以提供反向代理功能.web
web服务器按提供的内容能够划分为动态web服务器和静态web服务器, 静态web服务器提供静态的文件内容, 好比html,css,js,image等, 动态web服务器提供动态内容, 如jsp,asp等通过服务器程序运行输出的页面.算法
tar -zxvf 安装包
./configure --prefix=/user/nginx
没有则默认安装到/user/local/nginxsudo apt-get install libpcre3 libpcre3-dev
sudo apt-get install zlib1g-dev
make
以及 make install
./nginx -c /user/data/nginx/conf/nginx.conf
-c 表示指定nginx.conf文件, 默认为NGINX-HOME/conf/nginx.conf./nginx -s stop
./nginx -s quit
./nginx -s reload
kill -QUIT pid
pid是进程号. 安全中止, 可使正在处理的中止kill -TERM pid
多进程模型: 主进程fork出一个进程处理请求.nginx的方式.浏览器
多线程模型: 可能有并发问题缓存
异步方式: 每一个进程采用异步非阻塞模型处理请求.tomcat
主要分三部分:安全
work_processes 2; //工做进程数, 通常为cpu核数 work_cpu_affinity 01 10 error_log /var/log/nginx/error.log warn; //错误日志 worker_rlimit_nofile 10240; // 最大打开文件数
event{ use epoll ; // 选项有 select poll epoll kqueue等 work_connections 10240; //链接数 accept_mutex off; //off 能够提升效率 } http{ include mime.types; default_type application/octet-stream; //默认mime charset utf-8; access_log off; sendfile on; gzip on; .... proxy_temp_path /data/ proxy_cache_path /data/cache; level=1:2 inclued /etc/nginx/conf.d/*.conf; }
下面配置一个虚拟主机:服务器
server{ listen 80; server_name www.my.com; location / { root html/domain; index index.html index.htm; } }
server{ listen 8080; server_name localhost; location / { root html/domain; index index.html index.htm; } }
log_format formatName '.....' //日志格式 access_log logs/logfile.log formatName //指定日志路径和格式
日志切割
mv access.log access.log.001
而后重启生成, 能够写个运维脚本, 定时执行
=
是精确匹配, 如=/mypage.html
优先级最高(相似servlet规范的url的优先级规则)
通常匹配, 优先级高于正则匹配, 如:/myroot/mydir/mypage/
正则匹配, 如^~ /prefix/my/jsp
location[~|=|^~|~*]/uri{ }
使用 rewrite 支持 url 重写. 支持 if
,return
语句.
只能在server/location/if中使用. 只能对域名后除去参数外的字符串起做用.
if(条件){}
: 条件能够是 =
或者 ~
后者表示一个正则, 如if($request_uri~*\.sh){ return 403; }
表示若是请求是.sh结尾的, 则返回403.
rewrite regex replacement[flag]{last/break/redirect/permant}
last 中止处理后续的rewrite 指令集, 而后对当前重写的uri在 rewrite 指令集上从新查找; break则不会从新查找;
//重定向到百度 location / { rewrite ^/ http://www.baidu.com ; }
location / { rewrite '^/images/([a-z]{3})/(.*)\.(png)$' /images?file=$2.$3 ; set $image_file $2; set $image_type $3; } loation /images { root html; try_files /$arg_file /image404.html; } location =/image404.html{ return 404 "image not found"; }
expires 60s
s|m|h|d 可用的时间单位
server{ listen 80; server_name localhost; location /{ root html; index index.html index.htm } location ~ \.(png|jpg|js|css|gif)${ root html/images; expires 60s } }
server{ ... gzip on; gzip_buffers 4 16k //16k为单位, 4倍申请 gzip_comp_level 4; //0-9 gzip_min_length 500; //最小压缩大小,小于此大小不压缩 gzip_types text/css; //压缩的mime类型 ... }
为了方便单独配置, 新增 proxy.conf
而后在主文件中的http
节点添加include /path/proxy.conf
固然也能够直接修改nginx.conf
location =/s { proxt_set_header Host $host proxt_set_header X-Real_IP $remote_addr // 设置请求的真实ip proxy_set_header interface_version $Host //版本号 proxy_pass http://www.baidu.com; }
upstream tomcatserver{ ip_hash; // 也能够不配置,不配置则轮训策略, 或者配置为fair(按响应时间),utl_hash server 192.168.1.122:8080; server 192.168.1.123:8080 weight=4; //若是是轮训, 则权重起做用 } // 修改proxy_pass节点为负载均衡 prox_pass http://tomcatserver
生成证书:
openssl genrsa -des3 -out server.key 1024
openssl req -new -key server.key -out server.csr
cp server.key server.key.org
openssl rsa -in server.key.org -out server.key
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
配置:
server{ listen 443; server_name www.myhost.com; ssl on; ssl_certificate /my/host/conf/server.crt; ssl_certificate_key /my/host/conf/server.key; location / { proxy_pass http://myhost; } }
修改tomcat配置(最后两个配置新增): 这个能够不配置, 走http
<Connector port="8080" protocal="HTTP/1.1" connectionTimeout="20000" proxyPort="443" redirectPort="443" />
基于 VRRP(虚拟路由器冗余协议);
./configure --prefix=/mydir/keepalived/ --sysconf=/etc
最后一个参数是代表把配置文件存储到指定目录下make & make install
ln -s /.../sbin/keepalived /sbin
cp /etc/init.d/keepalived /etc/init.d/
chkconfig --add keepalived
chkconfig keeepalived on
修改keepalived.conf文件
vrrp_instance_VI_1{ state MASTER // 另外一台为BACKUP interface eth0 //网卡端口, ifconfig后修改成正确端口 virtual_router_id 51 //主从保持一致 priority 100 //master必须大于backup, 大的节点会变成master ... virtual_ipaddress{ //虚拟服务器ip 192.168.1.123 192.168.1.124 } } vitrual_server 192.168.1.123{ delay_loop 6 lb_glgo rr //loadbalance 算法 ... real_server 192.168.11.123{ weight 1 //权重 TCP_CHECK{ connect_timeout 3 delay_before_retry 3 connect_port 80 } } }