Nginx动静分离简单来说就是把动态跟静态请求分开,不能理解成只是单纯的把动态页面和静态页面物理分离。严格意义上说应该是动态请求跟静态请求分开,可以理解成使用Nginx 处理静态页面, Tomcat、 Resin处理动态页面。
动静分离从目前实现角度来讲大致分为两种,一种是纯粹的把静态文件独立成单独的域名,放在独立的服务器上,也是目前主流推崇的方案;另外一种方法就是动态跟静态文件混合在一起发布,通过 nginx 来分开。这样也是本次要讲解的,通过 location 指定不同的后缀名实现不同的请求转发。
通过 expires 参数设置,可以使浏览器缓存过期时间,减少与服务器之前的请求和流量。具体 Expires 定义:是给一个资源设定一个过期时间,也就是说无需去服务端验证,直接通过浏览器自身确认是否过期即可,所以不会产生额外的流量。
此种方法非常适合不经常变动的资源。(如果经常更新的文件,不建议使用 Expires 来缓存),可以设置 3d,表示在这 3 天之内访问这个 URL,发送一个请求,比对服务器该文件最后更新时间没有变化,则不会从服务器抓取,返回状态码 304,如果有修改,则直接从服务器重新下载,返回状态码 200。
准备工作:Centos7 yum安装tomcat
:Centos7 tomcat自定义jsp网页并修改默认端口
[root@ecs-4082 ~]# cat /etc/redhat-release
CentOS Linux release 7.3.1611 (Core)
[root@ecs-4082 ~]# ip add
1: lo: mtu 65536 qdisc noqueue state UNKNOWN qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether fa:16:3e:8f:9b:37 brd ff:ff:ff:ff:ff:ff
inet 192.168.2.10/24 brd 192.168.2.255 scope global dynamic eth0
valid_lft 56141sec preferred_lft 56141sec
inet6 fe80::f816:3eff:fe8f:9b37/64 scope link
valid_lft forever preferred_lft forever
1、配置yum,就可以使用yum安装nginx
[root@ecs-4082 ~]# cd /etc/yum.repos.d/
[root@ecs-4082 yum.repos.d]# vim aliyun.repo
[aliyun]
name=aliyun epel
baseurl=http://mirrors.aliyun.com/epel/7Server/x86_64/
gpgcheck=0
[root@ecs-4082 yum.repos.d]# ll
-rw-r--r-- 1 root root 88 Feb 4 14:52 aliyun.repo
-rw-r--r-- 1 root root 1924 Apr 20 2017 CentOS-ctyun.repo
2、yum安装nginx
[root@ecs-4082 ~]# yum install -y nginx
3、修改配置文件
[root@ecs-4082 ~]# cd /etc/nginx/
[root@ecs-4082 nginx]# vim nginx.conf
upstream tomcat {
server 192.168.2.9:80;
}
server {
listen 80 default_server;
server_name _;
root /usr/share/nginx/html; #静态页面
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location ~ \.(jsp|do)$ {
proxy_pass http://tomcat; #动态页面
}
4、完整配置文件
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
upstream tomcat {
server 192.168.2.9:80;
}
server {
listen 80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location ~ \.(jsp|do)$ {
proxy_pass http://tomcat;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2 default_server;
# listen [::]:443 ssl http2 default_server;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# location / {
# }
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
}
1、修改nginx静态主页面
[root@ecs-4082 ~]# whereis nginx
nginx: /usr/sbin/nginx /usr/lib64/nginx /etc/nginx /usr/share/nginx /usr/share/man/man8/nginx.8.gz /usr/share/man/man3/nginx.3pm.gz
[root@ecs-4082 ~]# cd /usr/share/nginx/
[root@ecs-4082 nginx]# ll
total 8
drwxr-xr-x 2 root root 4096 Feb 4 14:56 html
drwxr-xr-x 2 root root 4096 Feb 4 14:56 modules
[root@ecs-4082 nginx]# cd html/
[root@ecs-4082 html]# ll
total 20
-rw-r--r-- 1 root root 3650 Oct 18 16:08 404.html
-rw-r--r-- 1 root root 3693 Oct 18 16:08 50x.html
-rw-r--r-- 1 root root 3700 Oct 18 16:08 index.html
-rw-r--r-- 1 root root 368 Oct 18 16:08 nginx-logo.png
-rw-r--r-- 1 root root 2811 Oct 18 16:08 poweredby.png
[root@ecs-4082 html]# vim index.html
Welcome to nginx动静分离-静态页面 on Fedora!
2、修改tomcat动态测试页面,Centos7 tomcat自定义jsp网页并修改默认端口,这里要改变图片地址为绝对路径
IP: 192.168.2.9
3、如果图片路径设置不对,页面显示的时候图片就打不开,在nginx的/var/log/nginx/error.log日志里看到如下错误提示。
1、开启或者重启nginx服务
[root@ecs-4082 ~]# systemctl start nginx
2、开启或重启tomcat服务
[root@ecs-19cb bin]# ./startup.sh
Using CATALINA_BASE: /root/apache-tomcat-9.0.4
Using CATALINA_HOME: /root/apache-tomcat-9.0.4
Using CATALINA_TMPDIR: /root/apache-tomcat-9.0.4/temp
Using JRE_HOME: /usr
Using CLASSPATH: /root/apache-tomcat-9.0.4/bin/bootstrap.jar:/root/apache-tomcat-9.0.4/bin/tomcat-juli.jar
Tomcat started.
1、访问静态测试:
2、访问动态页面测试
第一种是图片地址没有设置为绝对路径
第二种是图片地址设置为绝对路径
简单nginx+tomcat动静分离测试完成