根据URL的目录地址实现动静态分离
■通过Nginx实现动静分离,即通过Nginx反向代理配置规则实现让动态资源和静态资源及其他业务分享由不同的服务器解析,以解决网站性能、安全、用户体验等重要问题
___________________________________________________________
需求梳理:
当用户请求www.szk.com/upload/xx地址时,实现由upload上传服务器池处理请求
当用户请求www.szk.com/static/xx地址时,实现由static服务器池处理请求
除此之外,对于其他访问请求,全部由默认的动态服务器池处理请求
upstream static_pools {
server 192.168.1.202:80 weight=1;
}
upstream upload_pools {
server 192.168.1.203:80 weight=1;
}
upstream default_pools {
server 192.168.1.204:80 weight=1;
}
server {
listen 80;
server_name www.szk.com;
location / {
proxy_pass http://default_pools;
proxy_set_header X-Forwarded-For $remote_addr;
}
location /static/ {
proxy_pass http://static_pools;
proxy_set_header X-Forwarded-For $remote_addr;
}
location /upload/ {
proxy_pass http://upload_pools;
proxy_set_header X-Forwarded-For $remote_addr;
}
-----------------------------------
[root@lab-2-C6 ~]#
cd /data/www/
mkdir static
echo static_pools > static/index.html
curl http://www.szk.com/static/index.html
------------------------------------测试
curl http://www.szk.com/static
static_pools
■通过Nginx实现动静分离,即通过Nginx反向代理配置规则实现让动态资源和静态资源及其他业务分享由不同的服务器解析,以解决网站性能、安全、用户体验等重要问题
___________________________________________________________
需求梳理:
当用户请求www.szk.com/upload/xx地址时,实现由upload上传服务器池处理请求
当用户请求www.szk.com/static/xx地址时,实现由static服务器池处理请求
除此之外,对于其他访问请求,全部由默认的动态服务器池处理请求
upstream static_pools {
server 192.168.1.202:80 weight=1;
}
upstream upload_pools {
server 192.168.1.203:80 weight=1;
}
upstream default_pools {
server 192.168.1.204:80 weight=1;
}
server {
listen 80;
server_name www.szk.com;
location / {
proxy_pass http://default_pools;
proxy_set_header X-Forwarded-For $remote_addr;
}
location /static/ {
proxy_pass http://static_pools;
proxy_set_header X-Forwarded-For $remote_addr;
}
location /upload/ {
proxy_pass http://upload_pools;
proxy_set_header X-Forwarded-For $remote_addr;
}
-----------------------------------
[root@lab-2-C6 ~]#
cd /data/www/
mkdir static
echo static_pools > static/index.html
curl http://www.szk.com/static/index.html
------------------------------------测试
curl http://www.szk.com/static
static_pools
编辑回复