nginx偏运维, 不过作为开发应该了解它能做什么事情, 其作为技术架构的一部分必不可少

正向代理和反向代理

正向代理是代理的客户端, 反向代理是代理的服务端. nginx就是一款可以作反向代理的web服务器.

常见的Web服务器

Apache, Nginx,Tomcat,WebLogic, iis, jetty, undertowe, resin

Apache,Nginx 是同一类型的服务器, 都可以提供反向代理功能.

web服务器按提供的内容可以划分为动态web服务器和静态web服务器, 静态web服务器提供静态的文件内容, 比如html,css,js,image等,
动态web服务器提供动态内容, 如jsp,asp等经过服务器程序运行输出的页面.

Nginx的安装

* tar -zxvf 安装包
* ./configure --prefix=/user/nginx 没有则默认安装到/user/local/nginx
* 如果报缺少pcre包, 需要安装sudo apt-get install libpcre3 libpcre3-dev
* 如果缺少zlib包, 需要安装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的方式.

多线程模型: 可能有并发问题

异步方式: 每个进程采用异步非阻塞模型处理请求.

配置

主要分三部分:

* main work_processes 2; //工作进程数, 一般为cpu核数 work_cpu_affinity 01 10 error_log
/var/log/nginx/error.log warn; //错误日志 worker_rlimit_nofile 10240; // 最大打开文件数
* event 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
虚拟主机

* 基于域名
下面配置一个虚拟主机:
server{ listen 80; server_name www.my.com; location / { root html/domain;
index index.html index.htm; } }
* 基于IP
* 基于端口 server{ listen 8080; server_name localhost; location / { root
html/domain; index index.html index.htm; } }
nginx日志配置
log_format formatName '.....' //日志格式 access_log logs/logfile.log formatName
//指定日志路径和格式
日志切割

mv access.log access.log.001 然后重启生成, 可以写个运维脚本, 定时执行

location 的语法和匹配规则

= 是精确匹配, 如=/mypage.html 优先级最高(类似servlet规范的url的优先级规则)

一般匹配, 优先级高于正则匹配, 如:/myroot/mydir/mypage/

正则匹配, 如^~ /prefix/my/jsp
location[~|=|^~|~*]/uri{ }
rewrite 使用

使用 rewrite 支持 url 重写. 支持 if,return语句.

只能在server/location/if中使用. 只能对域名后除去参数外的字符串起作用.

if(条件){}: 条件可以是 = 或者 ~ 后者表示一个正则, 如if($request_uri~*\.sh){ return 403; }
表示如果请求是.sh结尾的, 则返回403.

rewrite的语法

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 } }
Gzip 压缩
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
配置https

生成证书:

* 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" />
nginx+keepalived (相当于nginx主从)

基于 VRRP(虚拟路由器冗余协议);

* 下载解压keepalived包
* 配置./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 } } }

友情链接
KaDraw流程图
API参考文档
OK工具箱
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:[email protected]
QQ群:637538335
关注微信