1.1 Nginx

Nginx使用场景

高并发场景

  • 高并发是指在同一个时间点,有很多用户同时的访问同一 API 接口或者 Url 地址
  • 会给服务器和硬件环境带来很大的压力
  • 例如大学选修课 游乐场购票 过去购买火车票
  • 过去通过池来解决问题:
    刚开始的时候创建多个请求等待使用
    使用完毕后并不会销毁,而是重新归还到池
    如果业务量比较大,我们通过池控制最大连接的数
  • 负载均衡(load Balance )
    将请求/数据【均匀】分摊到多个操作单元上执行,负载均衡的关键在于【均匀】


Nginx简介

  • Nginx是一款轻量级的Web 服务器/反向代理服务器
  • 占有内存少,并发能力强
  • 官方测试nginx能够支撑5万并发链接,并且CPU、内存等资源消耗却非常低,运行非常稳定。

Tengine淘宝基于Nginx研发的web服务器
http://tengine.taobao.org/
http://tengine.taobao.org/book/



Nginx安装&命令

(1).安装Nginx相关依赖

依赖: gcc openssl-devel pcre-devel zlib-devel
--语法:
yum install gcc pcre-devel zlib-devel openssl-devel -y

(2).解压文件tar.gz

使用源码安装
将安装包上传到Linux虚拟机
解压,并把Nginx存放到指定目录

--语法:
tar -zxf nginx-1.8.1.tar.gz

(3).configure配置 - 配置安装路径

进入解压后的源码目录(nginx-1.8.1),然后执行configure命令进行配置

./configure --prefix=/opt/sxt/nginx

configure是配置文件 无需修改
–prefix前置
/opt/sxt/nginx 我们将其安装到这个目录(前面只是解压并非安装)


(4).编译并安装nginx

--语法:
make && make install

安装好后,会在/opt/sxt目录下生成nginx目录(这是刚才编译前指定的),这个目录就是nginx的软件了。
在这里插入图片描述

nginx命令

进入nginx启动项目录
--语法:
cd /opt/sxt/nginx/sbin

在这里插入图片描述
安装目录
conf — 配置文件
html — 静态资源文件
sbin — 可执行文件
logs — 日志文件


启动nginx
--语法: 
./nginx

启动后,访问虚拟机的80端口,可查看到以下界面
在这里插入图片描述

Nginx默认监听80端口,当出现以上信息,说明安装启动成功。


一旦nginx启动,就可以通过调用带有-s参数的可执行文件来控制它。使用语法:nginx -s 信号


信号可以是下列之一:
stop - 快速关机
quit - 优雅的关机
reload - 重新加载配置文件
reopen - 重新打开日志文件
例如,要停止nginx进程并等待工作进程完成当前请求的服务,可以执行以下命令:nginx -s quit


常用命令
nginx -s reload|quit

负载均衡搭建

准备三台虚拟机
安装JDK和Tomcat

安装jdk 的 rpm包
rpm -ivh jdk-7u67-linux-x64.rpm

安装tomcat的tar包
tar -zxf apache-tomcat-7.0.61.tar.gz

编辑配置文件并复制
vim /etc/profile

在最后面追加
export JAVA_HOME=/usr/java/jdk1.7.0_67
export PATH=$JAVA_HOME/bin:$PATH

拷贝配置文件
scp root@192.168.241.211:/etc/profile /etc/profile

加载配置文件
source /etc/profile

创建安装文件目录 并拷贝文件
mkdir -p /opt/sxt
cp -r apache-tomcat-7.0.61 /opt/sxt


负载均匀

  • 请求轮询
    依次转发给配置的服务器
  • 增加权重(言出必行 点石成金)
    weight=3
  • 最少连接数
    least_conn;
  • 确保来自同一客户端的请求将始终定向到同一台服务器,除非此服务器不可用
    ip_hash;

nginx.conf配置文件详细介绍

# user 指定运行 nginx 的用户和组(第一个参数为用户第二个为组,这里只有用户)
#user nobody;
# 指定工作进程数(一般设置为CPU核数)
worker_processes 1;   

# 指定错误日志为 logs/ 目录下的 error.log 文件
#error_log logs/error.log;
# 指定错误日志,并指定写入格式为 notice
#error_log logs/error.log notice;
# 指定错误日志,并指定写入格式为 info  
#error_log logs/error.log info;

# 指定 pid 文件(存放主进程 pid 号)
#pid       logs/nginx.pid;

# nginx 连接配置模块
events {
  # 指定每个工作进程最大连接数为 1024
  worker_connections 1024;
}

# http 配置模块
http {
  # 通过 include 加载 mime.types 文件,里面的 types {} 模块将文件扩展名映射到响应的 MIME 类型
  include       mime.types;
  # 定义响应的默认 MIME 类型
  default_type application/octet-stream;

  # 写入格式 main 的内容格式如下
  #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  #                 '$status $body_bytes_sent "$http_referer" '
  #                 '"$http_user_agent" "$http_x_forwarded_for"';

  # 指定访问日志和写入格式为 main
  #access_log logs/access.log main;

  # 启用或者禁用 sendfile()
  sendfile       on;
  # 启用或者禁用使用套接字选项(仅在 sendfile 使用时使用)
  #tcp_nopush     on;

  # 0 值禁用保持活动的客户端连接
  #keepalive_timeout 0;
  # 65 s 超时
  keepalive_timeout 65;

  # 启用或者禁用 gzip
  #gzip on;

  # 虚拟主机配置模块
  server {
      # 监听 80 端口
      listen       80;
      # 监听域名为 localhost
      server_name localhost;
      # 将指定的 charset 添加到 “Content-Type” 响应头字段。如果此charset与source_charset指令中指定的charset不同,则执行转换。
      #charset koi8-r;

      # 指定该虚拟主机的访问日志
      #access_log logs/host.access.log main;

      # 将特定的文件或目录重新定位,如 php 文件,image 目录等
      location / {
          # 设置请求的根目录
          root   html;
          # 定义索引,按顺序匹配
          index index.html index.htm;
      }

      # 定义显示 404 错误的 uri
      #error_page 404             /404.html;

      # redirect server error pages to the static page /50x.html
      #
      error_page   500 502 503 504 /50x.html;
      # location 精准匹配 '/50x.html'
      location = /50x.html {
          root   html;
      }

      # proxy the PHP scripts to Apache listening on 127.0.0.1:80
      # 正则表达式匹配 php 文件
      #location ~ \.php$ {
          # 设置代理服务器的协议和地址,以及应该映射位置的可选URI。作为协议,可以指定“http”或“https”。该地址可以指定为一个域名或IP地址,以及一个可选端口
      #   proxy_pass   http://127.0.0.1;
      #}

      # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
      #
      #location ~ \.php$ {
      #   root           html;
            # 设置 FastCGI 服务器的地址。地址可以指定为一个域名或 IP 地址,以及一个端口
      #   fastcgi_pass   127.0.0.1:9000;
            # 设置将在以斜杠结尾的URI之后追加的文件名,
      #   fastcgi_index index.php;
            # 设置一个应该传递给FastCGI服务器的参数。
      #   fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
            # 加载 conf/fastcgi_params 文件
      #   include       fastcgi_params;
      #}

      # deny access to .htaccess files, if Apache's document root
      # concurs with nginx's one
      #
      #location ~ /\.ht {
      #   deny all;
      #}
  }


  # another virtual host using mix of IP-, name-, and port-based configuration
  #
  #server {
  #   listen       8000;
  #   listen       somename:8080;
  #   server_name somename alias another.alias;

  #   location / {
  #       root   html;
  #       index index.html index.htm;
  #   }
  #}


  # HTTPS server
  #
  # ssl 配置,要启用 ssl 模块需要在编译 nginx 时加上 --with-http_ssl_module 参数
  #server {
  #   listen       443 ssl;
  #   server_name localhost;

  #   ssl_certificate     cert.pem;
  #   ssl_certificate_key cert.key;

  #   ssl_session_cache   shared:SSL:1m;
  #   ssl_session_timeout 5m;

  #   ssl_ciphers HIGH:!aNULL:!MD5;
  #   ssl_prefer_server_ciphers on;

  #   location / {
  #       root   html;
  #       index index.html index.htm;
  #   }
  #}

}

nginx.conf示例代码:

在这里插入图片描述



资源静态化

配置静态资源的路径

location ^~ /static/ {
	root html;
}



代理

  • 正向代理
    买家 代理买家
  • 反向代理
    买家 代理卖家


Session一致性

内存数据库

  • 安装数据库
yum install memcached -y
  • 开启数据库服务
service memcached status
service memcached start
chkconfig memcached on

Tomcat

  • 拷贝jar包到当前服务器lib目录下
  • 配置tomcat/conf/context.xml
<Manager 
    className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
    memcachedNodes="n1:192.168.241.211:11211"
    sticky="true"
    lockingMode="auto"
    sessionBackupAsync="false"
    requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
    sessionBackupTimeout="1000"          transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory" />

             重启Tomcat服务器

  • 访问发现同一个server是可以共享session的,但是不同server因为涉及到跨域的问题
  • session还是不同的,需要设置cookie的domain
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值