一、动静分离
1.单台机器动静分离
[root@web01 ~]# cat /etc/nginx/conf.d/linux.wp.com.conf
server {
listen 80;
server_name linux.wp.com;
location / {
root /code/wordpress;
index index.php;
}
location ~* \.(jpg|png|gif)$ {
root /code/wordpress;
}
location ~* \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME /code/wordpress/$fastcgi_script_name;
include fastcgi_params;
}
}
2.多台机器的动静分离
1)准备环境
主机 | 作用 | 服务 | IP | PORT |
---|---|---|---|---|
lb01 | 负载均衡 | nginx | 192.168.71.116 | 9090 |
web01 | 静态资源 | nginx | 192.168.71.112 | 8080 |
web02 | 静态资源 | nginx | 192.168.71.113 | 8080 |
web03 | 动态资源 | tomcat | 192.168.71.114 | 8080 |
2)配置web01的静态资源
1、准备包 [root@web01 /usr/share/nginx/html]# cd /usr/share/nginx/html [root@web01 /usr/share/nginx/html]# git clone https://gitee.com/egonlin/jsp_pro.git [root@web01 /usr/share/nginx/html]# ll jsp_pro/ drwxr-xr-x 2 root root 27 6月 3 17:10 backend_code drwxr-xr-x 4 root root 51 6月 3 17:10 frontend_code 2、修改配置文件 [root@web01 /usr/share/nginx/html]# cat /etc/nginx/conf.d/default.conf server { listen 8080; location / { root /usr/share/nginx/html/jsp_pro/frontend_code; index index.html; } } 3、 启动服务 [root@web01 /usr/share/nginx/html]# systemctl restart nginx 4、测试访问 [root@web01 /usr/share/nginx/html]# curl -I 127.0.0.1:8080/index.html HTTP/1.1 200 OK Server: nginx/1.26.1 Date: Mon, 03 Jun 2024 09:19:37 GMT Content-Type: text/html Content-Length: 1061 Last-Modified: Mon, 03 Jun 2024 09:10:29 GMT Connection: keep-alive ETag: "665d8885-425" Accept-Ranges: bytes
3)配置web02的动态资源
1、准备包
同上
2、修改配置文件
[root@web02 /usr/share/nginx/html]# cat /etc/nginx/conf.d/default.conf
server {
listen 8080;
#location /static {
location ~* \.(css|js|jpg|png|mp4|gif)$ {
root /usr/share/nginx/html/jsp_pro/frontend_code;
}
}
3、 启动服务
[root@web01 /usr/share/nginx/html]# systemctl restart nginx
4、测试访问
[root@web02 /usr/share/nginx/html]# curl -I 127.0.0.1:8080/static/css/a.css
HTTP/1.1 200 OK
Server: nginx/1.26.1
Date: Mon, 03 Jun 2024 09:17:02 GMT
Content-Type: text/css
Content-Length: 300
Last-Modified: Mon, 03 Jun 2024 09:10:29 GMT
Connection: keep-alive
ETag: "665d8885-12c"
Accept-Ranges: bytes
[root@web02 /usr/share/nginx/html]# curl -I 127.0.0.1:8080/static/img/1.jpg
HTTP/1.1 200 OK
Server: nginx/1.26.1
Date: Mon, 03 Jun 2024 09:17:13 GMT
Content-Type: image/jpeg
Content-Length: 950756
Last-Modified: Mon, 03 Jun 2024 09:10:29 GMT
Connection: keep-alive
ETag: "665d8885-e81e4"
Accept-Ranges: bytes
4)配置web03
1、安装tomcat yum install tomcat -y 2、准备包 [root@web03 ~]# git clone https://gitee.com/egonlin/jsp_pro.git [root@web03 ~]# cp jsp_pro/backend_code/java_test.jsp /var/lib/tomcat/webapps/ROOT/ 3、修改配置文件 无需修改 4、 启动服务 [root@web03 ~]# systemctl restart tomcat 5、测试访问 [root@web03 ~]# curl 127.0.0.1:8080/java_test.jsp 随机数:137
5)配置负载均衡
1.配置
[root@lb01 ~]# cat /etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
upstream frontend_server {
server 192.168.71.112:8080 max_fails=3 fail_timeout=5s;
}
upstream file_server {
server 192.168.71.113:8080 max_fails=3 fail_timeout=5s;
}
upstream backend_server {
server 192.168.71.114:8080 max_fails=3 fail_timeout=5s;
}
server {
listen 9090;
location / {
proxy_pass http://frontend_server;
}
location ~* \.(css|js|jpg|png|gif)$ {
proxy_pass http://file_server;
}
location ~* \.jsp$ {
proxy_pass http://backend_server;
}
}
}
2.重启
[root@lb01 ~]# systemctl restart nginx
最终测试
浏览器访问:
查看日志
[root@web01 /usr/share/nginx/html]# tail -f /var/log/nginx/access.log 192.168.71.116 - - [03/Jun/2024:17:31:07 +0800] "GET / HTTP/1.0" 200 1061 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36" "-" 192.168.71.116 - - [03/Jun/2024:17:31:07 +0800] "GET /favicon.ico HTTP/1.0" 404 555 "http://192.168.71.116:9090/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36" "-" [root@web02 /usr/share/nginx/html]# tail -f /var/log/nginx/access.log 192.168.71.116 - - [03/Jun/2024:17:31:07 +0800] "GET /static/css/a.css HTTP/1.0" 200 300 "http://192.168.71.116:9090/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36" "-" 192.168.71.116 - - [03/Jun/2024:17:31:07 +0800] "GET /static/js/jquery-3.7.1.js HTTP/1.0" 200 296028 "http://192.168.71.116:9090/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36" "-" 192.168.71.116 - - [03/Jun/2024:17:31:07 +0800] "GET /static/img/1.jpg HTTP/1.0" 200 950756 "http://192.168.71.116:9090/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36" "-" [root@web03 ~]# tail -f /var/log/tomcat/localhost_access_log.2024-06-03.txt 192.168.71.116 - - [03/Jun/2024:17:31:11 +0800] "GET /java_test.jsp HTTP/1.0" 200 12