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. JS 设计模式四 -- 模块模式

    概念 模块模式的思路 就是 就是单例模式添加私有属性和私有方法,减少全局变量的使用. 简单的代码结构: var singleMode = (function(){ // 创建私有变量 var priv ...

  2. Appium could not connect to server are you sure it's running appium desktop

    use remote host value : 127.0.0.1 switch to Custom server to Automatic server adb kill-server adb st ...

  3. springboot aop 拦截接口执行时间

    /** * @description: 记录接口执行时间日志的记录 * @author: * @create 2018-12-27 16:32 */ @Target(ElementType.METHO ...

  4. Lodop提示BarCode Type(ena13)Invalid!

    前段时间遇到过一个奇怪的问题,就是代码里本身都是ENA13大写,却提示条码类型无效,而且进入打印设计后,选中打印项,条码类型变成了code39,但是实际是还有条码类型参数都是正确的,代码看上去没有问题 ...

  5. Kafka如何保证消息的顺序性

    1. 问题 比如说我们建了一个 topic,有三个 partition.生产者在写的时候,其实可以指定一个 key,比如说我们指定了某个订单 id 作为 key,那么这个订单相关的数据,一定会被分发到 ...

  6. ISOMAP

    转载 https://blog.csdn.net/dark_scope/article/details/53229427# 维度打击,机器学习中的降维算法:ISOMAP & MDS 降维是机器 ...

  7. odoo 权限问题

    odoo 权限问题 权限组问题 权限组是为了将人员按组划分同一分配权限.权限组的建立是基于每个应用来实现的 建立一个应用的分组(可省略,主要用于创建用户时有选择项) 建立一条record记录model ...

  8. Oracle字符串行拆分成列的三种方式

    Oracle字符串行拆分成列的三种方式 --muphy 开发过程中经常会遇到将前台多个值用逗号连接一同传递到后台查询,这个用逗号连接的字符串分隔的每个字符串分别对应Oracle数据库表的不同行. 如下 ...

  9. [LOJ3087][GXOI/GZOI2019]旅行者——堆优化dijkstra

    题目链接: [GXOI/GZOI2019]旅行者 我们考虑每条边的贡献,对每个点求出能到达它的最近的感兴趣的城市(设为$f[i]$,最短距离设为$a[i]$)和它能到达的离它最近的感兴趣的城市(设为$ ...

  10. v-for 在 VSCode 中出现 Elements in iteration expect to have 'v-bind:key' directives.

    在 VSCode 中编辑代码时,在有 v-for 的语句下面有一条红色波浪线,鼠标放上去有提示 Elements in iteration expect to have 'v-bind:key' di ...