Nginx基本配置与应用

一、准备

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
image

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 流程

image

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内容

image

基中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循环出现
image

3.2 防盗链

image

原理就是根据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用隐身模式打开

image

3.3 城市静态站点实现

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

静态站点目录如下

image

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

image
image
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,496评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,407评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,632评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,180评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,198评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,165评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,052评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,910评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,324评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,542评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,711评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,424评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,017评论 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,668评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,823评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,722评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,611评论 2 353

推荐阅读更多精彩内容

  • Page 1:nginx 服务器安装及配置文件详解 CentOS 6.2 x86_64 安装 nginx 1.1 ...
    xiaojianxu阅读 8,535评论 1 41
  • 1.ngnix介绍 ngnix www服务软件 俄罗斯人开发 开源 性能很高 本身是一款静态WWW软件 静态小文件...
    逗比punk阅读 2,090评论 1 6
  • nginx做负载均衡器以及proxy缓存配置 关于nginx的安装和基本配置请参考nginx,本文在原基础上完成以...
    meng_philip123阅读 1,598评论 1 16
  • Nginx简介 解决基于进程模型产生的C10K问题,请求时即使无状态连接如web服务都无法达到并发响应量级一万的现...
    魏镇坪阅读 2,003评论 0 9
  • 1.简介:  Nginx:engine X ,2002年,开源,商业版 http协议:web服务器(类似于ht...
    尛尛大尹阅读 1,867评论 0 3