一、准备

1.1 环境准备

CentOS7软件环境

1.2 tomcat多实例

把/etc/profile.d/tomcat.sh中的变量注释了

#export TOMCAT_HOME=/usr/local/tomcat
#export CATALINA_HOME=/usr/local/tomcat
#export CATALINA_BASE=/usr/local/tomcat
#export CATALINA_TMPDIR=/usr/local/tomcat/temp
#export TOMCAT_USER=tomcat unset TOMCAT_HOME
unset CATALINA_HOME
unset CATALINA_BASE
unset CATALINA_TMPDIR

复制tomcat目录

cd /opt/tomcat
cp -a apache-tomcat-8.5.16 tomcat8180
cp -a apache-tomcat-8.5.16 tomcat8280
cp -a apache-tomcat-8.5.16 tomcat8380

修改配置

# 创建部署目录
mkdir -p /data/webapps
chown -R tomcat:tomcat /data/webapps # 修改配置,通过脚本修改如下内容
# 行号 替换前 替换后
# 22 8005 => 8105
# 69 8080 => 8180
# 116 8009 => 8109
# 148 appBase="webapps" => appBase="/data/webapps" # 执行脚本a.sh
sh a.sh # 查看修改后的不同
diff /opt/tomcat/apache-tomcat-8.5.16/conf/server.xml /opt/tomcat/tomcat8180/conf/server.xml

a.sh

#!/bin/sh

for i in {1..3}
do
file=/opt/tomcat/tomcat8"$i"80/conf/server.xml
sed -i '22s/8005/8'"$i"'05/' $file
sed -i '69s/8080/8'"$i"'80/' $file
sed -i '116s/8009/8'"$i"'09/' $file
#sed -i '148s#appBase=".*"#appBase="/data/webapps"#' $file
done

启动多实例

# 以普通用户运行tomcat
# for i in {1..3};do /opt/tomcat/tomcat8"$i"80/bin/daemon.sh start;done
for i in {1..3};do /opt/tomcat/tomcat8"$i"80/bin/startup.sh;done
netstat -tunlp | grep 80 # 关闭
for i in {1..3};do /opt/tomcat/tomcat8"$i"80/bin/shutdown.sh;done

1.3 配置hosts

# 查看服务器ip
ifconfig # 修改hosts
C:\Windows\System32\drivers\etc
192.168.5.210 test.com
192.168.5.210 beijing.test.com
192.168.5.210 shanghai.test.com

二、负载均衡

2.2 流程

2.2 nginx配置

http://nginx.org/en/docs/http/ngx_http_upstream_module.html

/etc/nginx.conf

/usr/local/nginx/conf/nginx.conf

worker_processes  1;
events {
worker_connections 1024;
} # http最外层模块
http {
# 全局变量参数
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65; upstream web_pool {
server 127.0.0.1:8180 weight=1;
server 127.0.0.1:8280 weight=1;
server 127.0.0.1:8380 weight=2;
} # server相当于虚拟站点
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://web_pool;
index index.html index.htm;
}
}
}

解析

http:根元素
upstream:反向代理的域
server:虚拟站点 location # 请求的一个节点

location参数

root                         # 站点根路径
index # 首页
proxy_pass # 代理服务
proxy_redirect off; # 是否允许重写向
proxy_set_header Host $host; # 传header参数至后台端服务
proxy_set_header X-Forwarded-For $remote_addr;
proxy_connect_timeout 90; # 连接代理服务超时时间
proxy_send_timeout 90; # 请求改善最大时间
proxy_read_timeout 90; # 读取最大时间

upstream参数

service         # 反向服务地址加端口
weight # 权重
max_fails # 失败多少次后认为主机已经挂掉,踢出
fail_timeout # 踢出后重新探测时间
backup # 备用服务
max_conns # 允许最大连接数
slow_start # 当节点恢复,不立即加入 max_fails注重用户体验好就要配置低 server 127.0.0.1:8180 fail_timeout=5s slow_start=10s;

2.3 负载算法

ll+weight:根据权重轮询

ip_hash:hash(client_ip)%2=index,解决session一致性

url_hash:hash(url)%2=index,资源缓存服务

least_conn:最少连接

least_time:请求时间越少,权重越高

三、应用实战

在修改nginx.conf配置文件后

# 验证配置是否正确
nginx -t # 平滑启动
nginx -s reload

3.1 动静分离

nginx.conf

worker_processes  1;
events {
worker_connections 1024;
} http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65; upstream web_pool {
server 127.0.0.1:8180 weight=1;
server 127.0.0.1:8280 weight=1;
} upstream static_resource {
server 127.0.0.1:8380;
} server {
listen 80;
server_name localhost;
# 动态服务
location / {
proxy_pass http://web_pool;
index index.html index.htm;
}
# 静态服务
location ~* \.(gif|css|png|jpg|js|swf)(.*) {
proxy_pass http://static_resource;
}
}
}

tomcat内容

基中index.html

cat /opt/tomcat/tomcat8180/webapps/ROOT/index.html
<html>
<head>
<title>index</title>
</head>
<body>
<h1>Hello 8180</h1>
<img src="one-piece.png"/>
</body>
</html> cat /opt/tomcat/tomcat8280/webapps/ROOT/index.html
<html>
<head>
<title>index</title>
</head>
<body>
<h1>Hello 8280</h1>
<img src="one-piece.png"/>
</body>
</html>

浏览器访问test.com, Hello 8180与Hello 8280循环出现

3.2 防盗链

原理就是根据Referer防盗链

nginx.conf

worker_processes  1;
events {
worker_connections 1024;
} http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65; upstream web_pool {
server 127.0.0.1:8180 weight=1;
server 127.0.0.1:8280 weight=1;
} upstream static_resource {
server 127.0.0.1:8380;
} server {
listen 80;
server_name localhost;
# 动态服务
location / {
proxy_pass http://web_pool;
index index.html index.htm;
}
# 静态服务
location ~* \.(gif|css|png|jpg|js|swf)(.*) {
# 防盗链设置
valid_referers none blocked *.test.com;
if ($invalid_referer) {
rewrite ^/ http://7xkmkl.com1.z0.glb.clouddn.com/404.jpg;
}
proxy_pass http://static_resource;
}
}
}

用IP访问:重新打开浏览器或Chrome用隐身模式打开

3.3 城市静态站点实现

server {
listen 80;
server_name *.test.com;
root /data/www/$host;
access_log logs/$host.access.log;
location / {
index index.html;
}
}

静态站点目录如下

index.html

cat /data/www/beijing.test.com/index.html
beijing cat /data/www/shanghai.test.com/index.html
shanghai

流量器访问beijing.test.com shanghai.test.com

Nginx基本配置与应用的更多相关文章

  1. 如何在Nginx下配置PHP程序环境

    1.nginx与PHP的关系 首先来看nginx与php的关系, FastCGI的关系(而不是像apache那样安装成nginx的模块) FastCGI的意思是, 快速的通用网关接口:CGI Comm ...

  2. 从零开始学 Java - CentOS 下 Nginx + Tomcat 配置负载均衡

    为什么现在有非常多的聪明人都在致力于互联网? 最近在读埃隆·马斯克传记,他说「我认为现在有非常多的聪明人都在致力于互联网」. 仔细一想,好像真的是这样的. 我问了自己一个问题:如果你不敲代码了,你能做 ...

  3. Nginx主配置参数详解,Nginx配置网站

    1.Niginx主配置文件参数详解 a.上面博客说了在Linux中安装nginx.博文地址为:http://www.cnblogs.com/hanyinglong/p/5102141.html b.当 ...

  4. nginx + tomcat配置负载均衡

    目标:Nginx做为HttpServer,连接多个tomcat应用实例,进行负载均衡. 注:本例程以一台机器为例子,即同一台机器上装一个nginx和2个Tomcat且安装了JDK1.7. 1.安装Ng ...

  5. spring4+websocket+nginx详细配置

    实现的版本jdk1.7.0_25, tomcat7.0.47.0, Tengine/2.1.1 (nginx/1.6.2), servlet3.0, spring4.2.2 使用maven导入版本3. ...

  6. Nginx Location配置总结

    Nginx Location配置总结 语法规则: location [=|~|~*|^~] /uri/ { - }= 开头表示精确匹配^~ 开头表示uri以某个常规字符串开头,理解为匹配 url路径即 ...

  7. 理解nginx的配置

    Nginx配置文件主要分成四部分:main(全局设置).server(主机设置).upstream(上游服务器设置,主要为反向代理.负载均衡相关配置)和 location(URL匹配特定位置后的设置) ...

  8. nginx缓存配置的操作记录梳理

    web缓存位于内容源Web服务器和客户端之间,当用户访问一个URL时,Web缓存服务器会去后端Web源服务器取回要输出的内容,然后,当下一个请求到来时,如果访问的是相同的URL,Web缓存服务器直接输 ...

  9. 在nginx中配置如何防止直接用ip访问服务器web server及server_name特性讲解

    看了很多nginx的配置,好像都忽略了ip直接访问web的问题,不利于SEO优化,所以我们希望可以避免直接用IP访问网站,而是域名访问,具体怎么做呢,看下面. 官方文档中提供的方法: If you d ...

  10. Nginx下配置ThinkPHP的URL Rewrite模式和pathinfo模式支持

    前面有关于lnmp环境的搭建,在此就不在赘述.下面就简述thinkPHP如何在nginx下开启url_rewrite和pathinfo模式支持 主要有两个步骤: 一.更改php.ini将;cgi.fi ...

随机推荐

  1. (stm32f103学习总结)—待机唤醒实验

    一.STM32待机模式介绍 1.1 STM32低功耗模式介绍 很多单片机具有低功耗模式,比如MSP430.STM8L等,我们的STM32 也不例外.默认情况下,系统复位或上电复位后,微控制器进入运行模 ...

  2. Episode 3:我们想要更好的社交网络

    我们为什么爱看评论?怎样的人类文字最有效率?更「好」的手机设计.APP 设计?APP Store 已经十年了?这是 WEB VIEW 的第三期节目<我们想要更好的社交网络>. 链接描述 s ...

  3. android webview与jquery mobile相互通信

    最近做android项目中遇到要在webview中做与js交互相关的东东,涉及到js中调用android本地的方法,于是查了资料整理了一下android和js互相调用的过程.如下demo,demo的主 ...

  4. idea 配置mapper.xml代码提示

    从代码跳转mapper文件的插件: 在mapper文件中添加dtd约束: 1.下载dtd约束文件 http://mybatis.org/dtd/mybatis-3-config.dtd   http: ...

  5. 每日所学之自学习大数据的Linux环境的配置

    今天开始配置环境,因为下载镜像文件需要很长时间,加上训练,所以Linux环境之配置了一半 VMware下载及安装教程(Window) 在安装虚拟机时需要下载镜像文件 下面是我下载的镜像文件的地址 Ce ...

  6. Java 实例 - 读取文件内容

    原文作者:菜鸟教程 原文链接:Java 实例 - 读取文件内容(建议前往原文以获得最佳体验) 按行读取文本文件 import java.io.*; public class Main { public ...

  7. python的编译和解释

    编译和解释 1.编译: 将源代码一次性转换成目标代码的过程 源代码 → 编辑器 →目标代码 →程序执行(同时程序输入)→结果输出 2.解释: 将源代码逐条转换成目标代码同时逐条运行的过程 源代码+程序 ...

  8. numpy教程02---ndarray数据和reshape重塑

    欢迎关注公众号[Python开发实战], 获取更多内容! 工具-numpy numpy是使用Python进行数据科学的基础库.numpy以一个强大的N维数组对象为中心,它还包含有用的线性代数,傅里叶变 ...

  9. crm单元测试使用

    Action使用 使用paramBag传递入参,填写入参名,入参值,后使用 serviceProvider传入插件. Assert.AreEqual(this.output["state&q ...

  10. i.MX rt 系列微控制器的学习记录

    杂记 前言 我总是很希望自己能产生一种感知电压变化的能力,就像B站上的教学动图中,电流从电源流出时导线就像LED亮起来一样,我将指尖触到导线上就能感受到实时的电压变化.我在上学和工作时经常由于无法理解 ...