Nginx基本配置與應用
阿新 • • 發佈:2018-03-02
sed stat 3.3 star pes nlp lin 查看 mon
一、準備
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基本配置與應用