nginx动静分离

    xiaoxiao2024-10-29  76

    简单配置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

    最新回复(0)