nginx 单台/多台动静分离
1.Zabbix-mysql单台web server实现动静分离
server {
listen 80;
server_name _;
root /soft/code;
index index.html;
location ~* .*\.(png|jpg|gif)$ {
root /soft/code/images; 如果location没有路径,那么就回去局部作用域中找
}
}
[root@zabbix-mysql soft]# pwd. 根下的soft
/soft
[root@zabbix-mysql soft]# ll
总用量 4
drwxr-xr-x 3 root root 4096 11月 1 09:52 code
[root@zabbix-mysql soft]# tree .
.
└── code
├── images
│ └── nginx.png
└── index.html
2 directories, 2 files
[root@zabbix-mysql soft]#
2.负载均衡代理后端动静分离
环境
Slb zabbix-server
Web01 zabbix-agent
Web02 zabbix-mysql
upstream static {
server 106.13.160.69:80;
}
upstream java {
server 122.112.164.83:8080;
}
server {
listen 80;
server_name _;
location ~*.*\.(png|gif|jpg)$ {
proxy_pass http://static;
include proxy_params;
}
location ~\.jsp {
proxy_pass http://java;
include proxy_params;
}
location / {
proxy_pass http://static;
}
}
3.动态服务器配置
[root@zabbix-agent ~]# yum install -y tomcat
[root@zabbix-agent ~]# mkdir /usr/share/tomcat/webapps/ROOT
[root@zabbix-agent ~]# vim /usr/share/tomcat/webapps/ROOT/java_test.jsp
[root@zabbix-agent ~]# systemctl start tomcat
[root@zabbix-agent ~]# cat /usr/share/tomcat/webapps/ROOT/java_test.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<HTML>
<HEAD>
<TITLE>JSP Test Page</TITLE>
</HEAD>
<BODY>
<%
Random rand = new Random();
out.println("<h1>Random number:</h1>");
out.println(rand.nextInt(99)+100);
%>
</BODY>
</HTML>
访问slb测试
排查下:
1.error.log文件
2.是否存在端口冲突
3.lsof -i:prot 是否存在
4.安全组
5.pass 代理后端tomcatIP地址填写错误
4.静态服务器配置
server {
listen 80;
server_name _;
root /soft/code;
index index.html;
location ~* .*\.(png|jpg|gif)$ {
root /soft/code/images; 如果location没有路径,那么就回去局部作用域中找
}
}
[root@zabbix-mysql soft]# pwd. 根下的soft
/soft
[root@zabbix-mysql soft]# ll
总用量 4
drwxr-xr-x 3 root root 4096 11月 1 09:52 code
[root@zabbix-mysql soft]# tree .
.
└── code
├── images
│ └── nginx.png
└── index.html
2 directories, 2 files
[root@zabbix-mysql soft]#
5.访问SLB测试
6.访问日志
静态服务器查看
[root@zabbix-mysql ~]# tail /var/log/nginx/access.log
动态服务器怎么查看,默认日志路径在哪?我不知道你们知不知道,反正我不知道,怎么办?找find
[root@zabbix-agent logs]# find / -type f -name "*.txt"|xargs grep “139.9.186.7"
/var/log/tomcat/localhost_access_log.2019-11-01.txt:139.9.186.7 - - [01/Nov/2019:11:32:17 +0800] "GET /java_test.jsp HTTP/1.1" 200 141
/var/log/tomcat/localhost_access_log.2019-11-01.txt:139.9.186.7 - - [01/Nov/2019:11:40:41 +0800] "GET /java_test.jsp HTTP/1.1" 200 141
/var/log/tomcat/localhost_access_log.2019-11-01.txt:139.9.186.7 - - [01/Nov/2019:11:42:44 +0800] "GET /java_test.jsp HTTP/1.1" 200 141
负载均衡日志
静态服务器日志
访问下动态日志测试
7. 思考
这时我们要考虑一个问题了,如果我们在动静分离的情况下如何获取客户端IP呢?
静态服务器获取客户端ip
动态服务器获取客户端IP
1.备份文件
[root@zabbix-agent logs]# cp /etc/tomcat/server.xml{,.bak}
修改140行