主机 |
IP |
角色 |
备注 |
nginx01 |
172.24.10.21 |
Nginx Proxy主机 |
接受请求,并代理至后端css存储点 |
nginx02 |
172.24.10.22 |
Nginx 静态服务器 |
处理静态请求 |
nginx03 |
172.24.10.23 |
Nginx 动态服务器 |
处理动态请求 |
[root@nginx02 ~]# mkdir /usr/share/nginx/staticrs/ [root@nginx02 ~]# echo ‘<h1>Static_Web</h1>‘ > /usr/share/nginx/staticrs/index.html [root@nginx02 ~]# ll /usr/share/nginx/staticrs/ #上传示例图片静态资源 total 16K -rw-r--r-- 1 root root 20 Jun 20 14:32 index.html -rw-r--r-- 1 root root 11K Jun 20 14:35 nginx.jpg [root@nginx02 ~]# mv /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.bak
[root@nginx02 ~]# vi /etc/nginx/conf.d/staticrs.conf server { listen 80; server_name staticrs.linuxds.com; access_log /var/log/nginx/staticrs.access.log main; error_log /var/log/nginx/staticrs.error.log warn; location / { root /usr/share/nginx/staticrs; index index.html; } }
1 [root@nginx02 ~]# nginx -t -c /etc/nginx/nginx.conf #检查配置文件 2 [root@nginx02 ~]# nginx -s reload #重载配置文件
1 [root@nginx03 ~]# yum install -y tomcat 2 [root@nginx03 ~]# mkdir -p /usr/share/tomcat/webapps/ROOT
[root@nginx03 ~]# vi /usr/share/tomcat/webapps/ROOT/javatest.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>随机数:<h1>"); out.println(rand.nextInt(99)+100); %> </BODY> </HTML>
1 [root@nginx03 ~]# systemctl start tomcat.service #启动tomcat
1 [root@nginx01 ~]# mkdir -p /usr/share/nginx/dss 2 [root@nginx01 ~]# ll /usr/share/nginx/dss/ 3 total 4.0K 4 -rw-r--r-- 1 root root 1.9K Jun 20 18:10 test.css #模拟css
[root@nginx01 ~]# vi /etc/nginx/conf.d/dss.conf #配置Dynamic-Static Separation upstream static_server { server 172.24.10.22; } upstream tomcat_server { server 172.24.10.23:8080; } server { listen 80; server_name dss.linuxds.com; access_log /var/log/nginx/dss.access.log main; error_log /var/log/nginx/dss.error.log warn; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; # location / { # root html; # index index.html; # } location / { proxy_pass http://static_server; } location ~ .*\.(css)$ { root /usr/share/nginx/dss; } location ~ .*\.(htm|html|gif|jpg|jpeg|png|gif|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma) { proxy_pass http://static_server; expires 5d; } location ~ .*\.jsp$ { proxy_pass http://tomcat_server; expires 1h; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
1 [root@nginx01 ~]# nginx -t -c /etc/nginx/nginx.conf #检查配置文件 2 [root@nginx01 ~]# nginx -s reload #重载配置文件
[root@nginx02 ~]# mkdir /usr/share/nginx/staticrs/ [root@nginx02 ~]# echo ‘<h1>Static_Web</h1>‘ > /usr/share/nginx/staticrs/index.html [root@nginx02 ~]# ll /usr/share/nginx/staticrs/ #上传示例图片静态资源 total 16K -rw-r--r-- 1 root root 20 Jun 20 14:32 index.html -rw-r--r-- 1 root root 11K Jun 20 14:35 nginx.jpg [root@nginx02 ~]# mv /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.bak
[root@nginx02 ~]# vi /etc/nginx/conf.d/staticrs.conf server { listen 80; server_name staticrs.linuxds.com; access_log /var/log/nginx/staticrs.access.log main; error_log /var/log/nginx/staticrs.error.log warn; location /static { alias /usr/share/nginx/staticrs; index index.html; } }
1 [root@nginx02 ~]# nginx -t -c /etc/nginx/nginx.conf #检查配置文件 2 [root@nginx02 ~]# nginx -s reload #重载配置文件
1 [root@nginx03 ~]# yum install -y tomcat 2 [root@nginx03 ~]# mkdir -p /usr/share/tomcat/webapps/ROOT/dynamic
[root@nginx03 ~]# vi /usr/share/tomcat/webapps/ROOT/dynamic/javatest.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>随机数:<h1>"); out.println(rand.nextInt(99)+100); %> </BODY> </HTML>
1 [root@nginx03 ~]# systemctl start tomcat.service #启动tomcat
手动访问后端动态站点及资源:http://dynamic.linuxds.com:8080/dynamic/javatest.jsp 3.4 配置前端动静分离
[root@nginx01 ~]# vi /etc/nginx/conf.d/dss.conf #配置Dynamic-Static Separation upstream static_server { server 172.24.10.22; } upstream tomcat_server { server 172.24.10.23:8080; } server { listen 80; server_name dss.linuxds.com; access_log /var/log/nginx/dss.access.log main; error_log /var/log/nginx/dss.error.log warn; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; # location / { # root html; # index index.html; # } location / { proxy_pass http://static_server; } location ~ .*\.(css)$ { root /usr/share/nginx/dss; } location /static/ { proxy_pass http://static_server; expires 5d; } location /dynamic/ { proxy_pass http://tomcat_server; expires 1h; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
1 [root@nginx01 ~]# nginx -t -c /etc/nginx/nginx.conf #检查配置文件 2 [root@nginx01 ~]# nginx -s reload #重载配置文件
原文:https://www.cnblogs.com/liujunjun/p/14128894.html