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
}
}
}

分布式系列十三: nginx的更多相关文章

  1. 后端分布式系列:分布式存储-HDFS 与 GFS 的设计差异

    「后端分布式系列」前面关于 HDFS 的一些文章介绍了它的整体架构和一些关键部件的设计实现要点. 我们知道 HDFS 最早是根据 GFS(Google File System)的论文概念模型来设计实现 ...

  2. 分布式系列九: kafka

    分布式系列九: kafka概念 官网上的介绍是kafka是apache的一种分布式流处理平台. 最初由Linkedin开发, 使用Scala编写. 具有高性能,高吞吐量的特定. 包含三个关键能力: 发 ...

  3. 分布式系列四: HTTP及HTTPS协议

    分布式系列四: HTTP及HTTPS协议 非常全面的一篇HTTP的文章: 关于HTTP协议,一篇就够了 还有一个帮助理解HTTPS的文章: 也许,这样理解HTTPS更容易 本文的一些描述摘自这篇文章 ...

  4. 分布式系列六: WebService简介

    WebSerice盛行的时代已经过去, 这里只是简单介绍下其基本概念, 并用JDK自带的API实现一个简单的服务. WebSerice的概念 WebService是一种跨平台和跨语言的远程调用(RPC ...

  5. 分布式系列 - dubbo服务telnet命令【转】

    dubbo服务发布之后,我们可以利用telnet命令进行调试.管理.Dubbo2.0.5以上版本服务提供端口支持telnet命令,下面我以通过实例抛砖引玉一下: 1.连接服务 测试对应IP和端口下的d ...

  6. nginx高性能WEB服务器系列之八--nginx日志分析与切割

    nginx系列友情链接:nginx高性能WEB服务器系列之一简介及安装https://www.cnblogs.com/maxtgood/p/9597596.htmlnginx高性能WEB服务器系列之二 ...

  7. nginx高性能WEB服务器系列之七--nginx反向代理

    nginx系列友情链接:nginx高性能WEB服务器系列之一简介及安装https://www.cnblogs.com/maxtgood/p/9597596.htmlnginx高性能WEB服务器系列之二 ...

  8. nginx高性能WEB服务器系列之六--nginx负载均衡配置+健康检查

    nginx系列友情链接:nginx高性能WEB服务器系列之一简介及安装https://www.cnblogs.com/maxtgood/p/9597596.htmlnginx高性能WEB服务器系列之二 ...

  9. struts2官方 中文教程 系列十三:利用通配符选择方法

    介绍 在本教程中,我们将介绍如何在struts.xml中配置action节点以达到仅使用一个action节点将几个不同的url关联到特定action类的特定方法.这样做的目的是减少struts.xml ...

随机推荐

  1. 初识gauge自动化测试框架(二)

    看到一些同学对该工具有点一兴趣,那么我将继续介绍Gauge自动化测试工具. Gauge本质上一个BDD(Behavior Driven Development)测试框架.所以,首先你要了解BDD的操作 ...

  2. 随心测试_职场面试_001<SX的面试观点>

    快速理解_求职面试:必不可少的嘴 +  双向沟通交流 = 人与人之间的心理游戏 ps:以下为_面试题回答套路_案例,仅供参考,不挖坑 常见的面试题: 你是如何看待软件测试这个行业的? 说说你对软件测试 ...

  3. [Bilingual] Different proofs of Jordan cardinal form (Jordan标准型的几种证明)

  4. codeblocks-17.12mingw-nosetup(mingw编译,绿色免安装版)的下载、安装及设置一

    一.先进入网址:http://www.codeblocks.org/downloads/,选择Download the binary release. 二.转换网页后,选择codeblocks-17. ...

  5. ansible roles

    roles 特点 目录结构清晰 重复调用相同的任务 目录结构相同 web - tasks - install.yml - copfile.yml - start.yml -  main.yml - t ...

  6. 在oracle表中增加字段,并调整字段的顺序

    增加字段的语句很简单,以用户身份连接oracle服务: alter table tablename add(colname coltype); # 填上表名.字段名.字段类型 修改字段顺序前,查看表中 ...

  7. Spring通过注释配置Bean2 关联关系

    接着我们讲讲关联关系的配置,我们耳熟能详的MVC结构,Controller关联着Service,Service关联着UserRepository,接着上一节的代码,完成上诉功能 在Main方法里,我们 ...

  8. Spring Boot 2.x 编写 RESTful API (二) 校验

    用Spring Boot编写RESTful API 学习笔记 约束规则对子类依旧有效 groups 参数 每个约束用注解都有一个 groups 参数 可接收多个 class 类型 (必须是接口) 不声 ...

  9. HDU 6468 zyb的面试

    http://acm.hdu.edu.cn/showproblem.php?pid=6468 题目 今天zyb参加一场面试,面试官听说zyb是ACMer之后立马抛出了一道算法题给zyb:有一个序列,是 ...

  10. Nginx CONTENT阶段 concat模块

    L67 concat_delimiter : 根据js 指定 分隔符 比如 “|” 那么每个文件分隔符为 “|” concat_types : 指定要合并文件的类型 concat_unique : s ...