本文内容承接《Nginx配置try_files实践一

1. 环境:

OS:Ubuntu 15.10

nginx:nginx/1.9.3 (Ubuntu)

假设有三台虚拟机db1(IP:192.168.68.21)/db2(IP:192.168.68.22)/db3(IP:192.168.68.23),通过try_files等配置,使三台机器的/data/www/upload合集组成网络资源,并且支持HTTPS请求SSL证书未认证(注:未验证合法证书的场景)

设计思路如下:

若请求到db2:

  • 检索db2是否存在目标资源,若存在则返回,否则请求通过db1-proxy重定向到db1
  • 检索db1是否存在目标资源,若存在则返回,否则返回404
  • 把404重定向到db3
  • 检索db3是否存在目标资源,若存在则返回,否则返回404
  • 请求结束

若请求到db1/db3同理。

2. 配置三台机器nginx默认配置

略过具体过程,注意事项

  • 配置日志格式
  • 生成SSL证书并上传(/etc/nginx/server.crt, /etc/nginx/server.key)

3. 配置db1

  • /etc/nginx/conf.d/db1.test.com.conf
     server{
          listen 80;
          server_name db1.test.com;
          listen 443 ssl;
          ssl on;
          ssl_certificate /etc/nginx/server.crt;
          ssl_certificate_key /etc/nginx/server.key;
          error_page 404 /404.html;

        access_log  /var/log/nginx/db1_access.log  main;
        error_log   /var/log/nginx/db1_error.log;

        location  /upload
        {
         root /data/www;
         try_files $uri @db2;
        }

        location @db2{
         proxy_pass http://192.168.68.22:8000/proxy$uri;
         proxy_intercept_errors on;
         recursive_error_pages on;
         error_page 404 = @db3;
        }

        location @db3{
         proxy_pass http://192.168.68.23:8000/proxy$uri;
        }
     }
View Code
  • /etc/nginx/conf.d/db1-proxy.test.com.conf
     server{
          listen 8000;
          server_name db1-proxy.test.com;
          error_page 404 /404.html;

        access_log  /var/log/nginx/db1_access.log  main;
        error_log   /var/log/nginx/db1_error.log;

        location  /proxy/upload
        {
         alias /data/www/upload;
        }

     }
View Code
  • 重启nginx

4. 配置db2

  • /etc/nginx/conf.d/db2.test.com.conf
     server{
          listen 80;
          server_name db2.test.com;
          listen 443 ssl;
          ssl on;
          ssl_certificate /etc/nginx/server.crt;
          ssl_certificate_key /etc/nginx/server.key;
          error_page 404 /404.html;

        access_log  /var/log/nginx/db2_access.log  main;
        error_log   /var/log/nginx/db2_error.log;

        location  /upload
        {
         root /data/www;
         try_files $uri @db1;
        }

        location @db1{
         proxy_pass http://192.168.68.21:8000/proxy$uri;
         proxy_intercept_errors on;
         recursive_error_pages on;
         error_page 404 = @db3;
        }

        location @db3{
         proxy_pass http://192.168.68.23:8000/proxy$uri;
        }
     }
View Code
  • /etc/nginx/conf.d/db2-proxy.test.com.conf
     server{
          listen 8000;
          server_name db2-proxy.test.com;
          error_page 404 /404.html;

        access_log  /var/log/nginx/db2_access.log  main;
        error_log   /var/log/nginx/db2_error.log;

        location  /proxy/upload
        {
         alias /data/www/upload;
        }
     }
View Code
  • 重启nginx

5. 配置db3

  • /etc/nginx/conf.d/db3.test.com.conf
     server{
          listen 80;
          server_name db3.test.com;
          listen 443 ssl;
          ssl on;
          ssl_certificate /etc/nginx/server.crt;
          ssl_certificate_key /etc/nginx/server.key;
          error_page 404 /404.html;

        access_log  /var/log/nginx/db3_access.log  main;
        error_log   /var/log/nginx/db3_error.log;

        location  /upload
        {
         root /data/www;
         try_files $uri @db1;
        }

        location @db1{
         proxy_pass http://192.168.68.21:8000/proxy$uri;
         proxy_intercept_errors on;
         recursive_error_pages on;
         error_page 404 = @db2;
        }

        location @db2{
         proxy_pass http://192.168.68.22:8000/proxy$uri;
        }
     }
View Code
  • /etc/nginx/conf.d/db3-proxy.test.com.conf
     server{
          listen 8000;
          server_name db3-proxy.test.com;
          error_page 404 /404.html;

        access_log  /var/log/nginx/db3_access.log  main;
        error_log   /var/log/nginx/db3_error.log;

        location  /proxy/upload
        {
         alias /data/www/upload;
        }

     }
View Code
  •  重启nginx

6. 创建测试文件

server name location url
db1 /data/www/upload/db1.html https://db1.test.com/upload/db1.html
/data/www/upload/db1/test.html https://db1.test.com/upload/db1/test.html
db2 /data/www/upload/db2.html https://db2.test.com/upload/db2.html
/data/www/upload/db2/test.html https://db2.test.com/upload/db2/test.html
db3 /data/www/upload/db3.html https://db3.test.com/upload/db3.html
/data/www/upload/db3/test.html https://db3.test.com/upload/db3/test.html

7. 配置本地host

192.168.68.21 db1.test.com
192.168.68.21 db1-proxy.test.com
192.168.68.22 db2.test.com
192.168.68.22 db2-proxy.test.com
192.168.68.23 db3.test.com
192.168.68.23 db3-proxy.test.com
View Code

8. 访问结果

url http status
https://db3.test.com/upload/db1.html 200
http://db3.test.com/upload/db1.html 200
https://db3.test.com/upload/db1/test.html 200
http://db3.test.com/upload/db1/test.html 200
https://db3.test.com/upload/db2.html 200
http://db3.test.com/upload/db2.html 200
https://db3.test.com/upload/db2/test.html 200
http://db3.test.com/upload/db2/test.html 200
https://db3.test.com/upload/db3.html 200
http://db3.test.com/upload/db3.html 200
https://db3.test.com/upload/db3/test.html 200
http://db3.test.com/upload/db3/test.html 200
https://db3.test.com/upload/db3/test1.html 404
http://db3.test.com/upload/db3/test1.html 404
https://db3.test.com/upload/dbfdsafas 404
http://db3.test.com/upload/dbfdsafas 404
 posted on 2017-06-23 15:53  hiv  阅读(2836)  评论(0编辑  收藏  举报