(1)静态资源站点配置
1.安装nginx 省略
2.配置nginx
#vim /usr/local/nginx/conf/nginx.conf
http {
include /usr/local/nginx/conf.d/*.conf;
}
#mkdir /usr/local/nginx/conf.d
#vim /usr/local/nginx/conf.d/www.test.com.conf
server {
listen 8003;
root /webroot/www;
location / {
index index.html;
}
location ~ .*.(jpg|bmp|png|gif)$ {
root /webroot/www/magic;
}
}
3.准备目录和资源
mkdir /webroot/www/magic -pv
wget -O /webroot/www/magic/nginx.png http://nginx.org/nginx.png
4.重新加载
nginx -t
nginx -s reload
ss -antlup | grep 8003
5.验证
192.9.191.31:8003/nginx.png
(2)动态资源站点
1.安装java和tomcat
yum install java -y
cd /tmp
wget http://mirrors.hust.edu.cn/apache/tomcat/tomcat-9/v9.0.8/bin/apache-tomcat-9.0.8.tar.gz
tar xf apache-tomcat-9.0.8.tar.gz -C /usr/local/
ln -sv /usr/local/apache-tomcat-9.0.8 /usr/local/tomcat
2.准备资源
#vi /usr/local/tomcat/webapps/ROOT/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>
3.启动服务
/usr/local/tomcat/bin/catalina.sh start
ss -anltup | grep 8080
192.9.191.31:8080/test.jsp
(3)nginx动静分离之根据扩展名调度
1.配置nginx,后缀名为.jpg转发到static组,后缀名.jsp转发到dynamic组
#vim /usr/local/nginx/conf/nginx.conf
http {
include /usr/local/nginx/conf.d/*.conf;
}
#mkdir /usr/local/nginx/conf.d
#vim /usr/local/nginx/conf.d/www.test.com.conf
upstream dynamic {
server 192.9.191.31:8080;
}
upstream static {
server 192.9.191.31:8003;
}
server {
listen 80;
server_name www.test.com;
location ~ .*.(jpg|bmp|png|gif)$ {
proxy_pass http://static;
}
location ~ .*.jsp$ {
proxy_pass http://dynamic;
}
}
2.重新加载
nginx -t
nginx -s reload
修改windows的hosts文件C:WindowsSystem32driversetchosts文件添加一条记录
192.9.191.30 www.test.com
3.测试
http://www.test.com/test.jsp
http://www.test.com/nginx.png
(4)nginx动静分离之根据浏览器和手机类型调度
#vim /usr/local/nginx/conf/nginx/nginx.conf
http {
upstream firefox {
server 192.9.191.31:8081;
}
upstream chrome {
server 192.9.191.31:8082;
}
upstream iphone {
server 192.9.191.31:8083;
}
upstream android {
server 192.9.191.31:8084;
}
upstream other {
server 192.9.191.31:8085;
}
server {
listen 80;
server_name 192.9.191.30;
location / {
if ($http_user_agent ~* "Firefox"){ //匹配firefox浏览器
proxy_pass http://firefox;
}
if ($http_user_agent ~* "Chrome"){ //匹配chrome谷歌浏览器
proxy_pass http://chrome;
}
if ($http_user_agent ~* "iphone"){ //匹配iphone手机
proxy_pass http://iphone;
}
if ($http_user_agent ~* "android"){ //匹配安卓手机
proxy_pass http://android;
}
proxy_pass http://other; //其它浏览器默认访问规则
}
}
}
(5)基于目录调度
1.方式一
upstream static {
server 192.9.191.31:8081;
}
upstream upload {
server 192.9.191.31:8082;
}
upstream default {
server 192.9.191.31:8083;
}
server {
listen 80;
server_name 192.9.191.30;
location / {
proxy_pass http://default;
}
location /static/ {
proxy_pass http://static;
}
location /upload/ {
proxy_pass http://upload;
}
}
2.方式二
upstream static {
server 192.9.191.31:8081;
}
upstream upload {
server 192.9.191.31:8082;
}
upstream default {
server 192.9.191.31:8083;
}
server {
listen 80;
server_name 192.9.191.30;
location / {
if ($request_uri ~* "^/static/(.*)$") {
proxy_pass http://static;
}
if ($request_uri ~* "^/upload/(.*)$") {
proxy_pass http://upload;
}
proxy_pass http://default;
}
}