環境信息如下:
測試目的:
訪問jpg文件的時候,會請求到192.168.1.106上面
訪問其他文件會請求到192.168.1.103上面
nginx的安裝詳見前面的文章
nginx的配置如下
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; upstream static_pools { server 192.168.1.106:8080; } upstream dynamic_pools { server 192.168.1.103:8080; } server { listen 80; location / { root html; index index.html index.htm; proxy_pass http://dynamic_pools; } location ~ .*.(gif|jpg|jpeg|png|bmp|swf|css|js)$ { //如果請求包含這些后綴,請求被轉發到靜態池,否則轉發到動態池 proxy_pass http://static_pools; } } } ~
然后重新加載配置
./nginx -s reload
測試訪問結果
訪問jpg文件
因為jpg文件只在tomcat1上面有,所以可以看到靜態請求被轉發給了106的機器
訪問其他文件
因為dunamic的目錄只在tomcat2上面有,因此可以看到測試是成功的
有時候有可能工程沒有做成動靜分離,但是通過nginx的配置可以做靜態的請求轉發給一些服務器,動態的請求轉發給另外一些服務器。