标签:end tab web服务 mime index 目录 timeout 安全 提前
通过Nginx实现动静分离,即通过Nginx反向代理配置规则实现让动态资源和静态资源及其他业务分别由不同的服务器解析,以解决网站性能、安全、用户体验等重要问题。
| 节点 | IP及端口 | 地址 | 显示结果 |
|---|---|---|---|
| web01 | 10.0.0.8:80 | http://www.xindaichina.cn/static/index.html | static |
| web02 | 10.0.0.7:80 | http://www.xindaichina.cn/index.html | default |
| web03 | 10.0.0.9:80 | http://www.xindaichina.cn/upload/index.html | upload |
upstream static_server_pools { server 10.0.0.8:80 weight=1; } upstream default_server_pools { server 10.0.0.7:80 weight=1; } upstream upload_server_pools { server 10.0.0.9:80 weight=1; }location方案
在server{}标签中添加
location /static/ { proxy_pass http://static_pools; include proxy.conf; } location / { proxy_pass http://default_pools; include proxy.conf; } location /upload/ { proxy_pass http://upload_pools; include proxy.conf; }if语句方案
if ($request_uri ~* "^/static/(.*)$"){ proxy_pass http://static_pools/$1;}if ($request_uri ~* "^/upload/(.*)$"){ proxy_pass http://upload_pools/$1;}location / { proxy_pass http://default_pools; include proxy.conf;}在对应的web服务器中配置对应的目录和index.html文件
web01
mkdir staticecho "static" >static/index.htmlweb02
echo "default" >index.html web03
mkdir uploadecho "upload" >upload/index.htmlcurl www.xindaichina.cn/static/index.htmlcurl www.xindaichina.cn/index.htmlcurl www.xindaichina.cn/upload/index.html location / { if ($http_user_agent ~* "android") { proxy_pass http://android_pools; #<==这是android服务器池,需要提前定义upstream。 } if ($http_user_agent ~* "iphone") { proxy_pass http://iphone_pools; #<==这是iphone服务器池,需要提前定义upstream。 } proxy_pass http://pc_pools; include extra/proxy.conf; }##nginx.conf配置worker_processes 1;events { worker_connections 1024;}http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; upstream android_pools { server 10.0.0.7:80 weight=1; } upstream iphone_pools { server 10.0.0.7:8080 weight=1; } upstream pc_pools { server 10.0.0.8:80 weight=1; } server { listen 80; server_name www.xindaichina.cn; location / { if ($http_user_agent ~* "android") { proxy_pass http://android_pools; } if ($http_user_agent ~* "iphone") { proxy_pass http://iphone_pools; } proxy_pass http://pc_pools; include proxy.conf; } }}标签:end tab web服务 mime index 目录 timeout 安全 提前
原文地址:http://www.cnblogs.com/wanglan/p/7850939.html