nginx动静分离

本文介绍了简单配置nginx动静分离的方法。假设web1为静态服务器、web2为动态服务器,node2做代理,分别阐述了根据目录分开、通过请求分离、根据扩展名分离、根据客户端标识分离以及使用客户端的pc和移动分离等方式,并给出了相应配置代码和访问测试示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

简单配置nginx的动静分离

假设web1为静态服务器,web2为动态服务器,node2做代理

1.1 根据目录分开

web1只处理静态请求

复制代码
[root@web1 ~]# mkdir -p /var/www/www/image
[root@web1 ~]# yum -y install lrzsz
[root@web1 ~]# cd /var/www/www/image/
[root@web1 image]# rz
[root@web1 image]# ll
-rw-r–r--. 1 root root 156848 Mar 13 11:31 nhrzyx.png
[root@web2 ~]# vim /etc/httpd/conf/httpd.conf
DocumentRoot “/var/www/www”
[root@web2 ~]# systemctl restart httpd
复制代码
web2只处理动态请求

[root@web2 ~]# mkdir -p /var/www/www/dynamic
[root@web2 ~]# echo dynamic10 > /var/www/www/dynamic/index.html
[root@web2 ~]# vim /etc/httpd/conf/httpd.conf
DocumentRoot “/var/www/www”
[root@web2 ~]# systemctl restart httpd
访问测试

http://172.25.254.134/image/nhrzyx.png

http://172.25.254.135/dynamic/index.html

1.2 通过请求分离

配置代理

复制代码
[root@lb01 conf]# vim nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream stack_pools {
server 172.25.254.134:80 weight=5;
}
upstream dynamic_pools {
server 172.25.254.135:80 weight=5;
}
server {
listen 80;
server_name www.lbtest.com;
location / {
root html;
index index.html index.htm;
proxy_set_header Host $host;
proxy_pass http://dynamic_pools;
}
location /image/ {
proxy_set_header Host $host;
proxy_pass http://stack_pools;
}
location /dynamic/ {
proxy_set_header Host $host;
proxy_pass http://dynamic_pools;
}
}
}
[root@lb01 conf]# nginx -s reload
复制代码
配置hosts ,浏览器访问测试

172.25.254.131 www.lbtest.com

http://www.lbtest.com/image/nhrzyx.png

http://www.lbtest.com/dynamic/

1.3 根据扩展名分离

复制代码
[root@lb01 conf]# vim nginx.conf

worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream stack_pools {
server 172.25.254.134:80 weight=5;
}
upstream dynamic_pools {
server 172.25.254.135:80 weight=5;
}
server {
listen 80;
server_name www.lbtest.com;
location / {
root html;
index index.html index.htm;
proxy_set_header Host KaTeX parse error: Expected 'EOF', got '}' at position 60: …pools; }̲ locati… {
proxy_set_header Host $host;
proxy_pass http://stack_pools;
}
}
}
[root@lb01 conf]# nginx -s reload
复制代码
http://www.lbtest.com/image/nhrzyx.png

1.4 根据客户端标识进行分离

复制代码
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream stack_pools {
server 172.25.254.134:80 weight=5;
}
upstream dynamic_pools {
server 172.25.254.135:80 weight=5;
}
server {
listen 80;
server_name www.lbtest.com;
location / {
if ( h t t p u s e r a g e n t   ∗ " M S I E " ) p r o x y p a s s h t t p : / / d y n a m i c p o o l s ; i f ( http_user_agent ~* "MSIE") { proxy_pass http://dynamic_pools; } if ( httpuseragent "MSIE")proxypasshttp://dynamicpools;if(http_user_agent ~* “firefox”)
{
proxy_pass http://stack_pools;
}
}
proxy_set_header Host $host;
}
}
[root@web1 image]# echo stack_web>> /var/www/www/test.html
[root@web1 image]# systemctl restart httpd

[root@web2 ~]# echo dynamic_web>>/var/www/www/test.html
[root@web2 ~]# systemctl restart httpd
复制代码
分别使用IE和火狐浏览器访问

http://www.lbtest.com/test.html

1.5 使用客户端的pc和移动分离

复制代码
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream stack_pools {
server 172.25.254.134:80 weight=5;
}
upstream dynamic_pools {
server 172.25.254.135:80 weight=5;
}
server {
listen 80;
server_name www.lbtest.com;
location / {
if ( h t t p u s e r a g e n t   ∗ " i p h o n e " ) p r o x y p a s s h t t p : / / d y n a m i c p o o l s ; i f ( http_user_agent ~* "iphone") { proxy_pass http://dynamic_pools; } if ( httpuseragent "iphone")proxypasshttp://dynamicpools;if(http_user_agent ~* “android”)
{
proxy_pass http://stack_pools;
}
}
proxy_set_header Host $host;
}
}
复制代码
分别使用安卓和iphone访问测试

http://www.lbtest.com/test.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值