首页 > 代码库 > Nginx配置try_files实践

Nginx配置try_files实践

1. 环境:

OS:Ubuntu 15.10

nginx:nginx/1.9.3 (Ubuntu)

两台虚拟机db1(IP:10.0.1.62)/db2(IP:10.0.1.63)

2. 配置两台机器的nginx.conf

user www-data;
worker_processes auto;
pid /run/nginx.pid;

events {
        worker_connections 768;
        # multi_accept on;
}

http {

    log_format  main  [$content_type]--[$remote_addr] - $remote_user [$time_local] "[$request]" 
    [$status] $body_bytes_sent [$request_body] "$http_referer" 
    "$http_user_agent" $http_x_forwarded_for;

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        # server_tokens off;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ##
        # SSL Settings
        ##

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
        ssl_prefer_server_ciphers on;

        ##
        # Logging Settings
        ##

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        ##
        # Gzip Settings
        ##

        gzip on;
        gzip_disable "msie6";

        # gzip_vary on;
        # gzip_proxied any;
        # gzip_comp_level 6;
        # gzip_buffers 16 8k;
        # gzip_http_version 1.1;
        # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

        ##
        # Virtual Host Configs
        ##

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
}

3. 为db1添加/etc/nginx/conf.d/db1.conf

 server{
      listen 80;
      server_name 10.0.1.62;
      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  /proxy/upload
    {
     alias /data/www/upload;
    }

    location @db2{
     proxy_pass http://10.0.1.63/proxy$uri;
    }
 }

4. 为db2添加/etc/nginx/conf.d/db2.conf

 server{
      listen 80;
      server_name 10.0.1.63;
      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  /proxy/upload
    {
     alias /data/www/upload;
    }

    location @db1{
     proxy_pass http://10.0.1.62/proxy$uri;
    }
 }

5. 重新加载nginx配置

`service nginx reload`

6. 在db1创建测试文件

技术分享

7. 在db2创建测试文件

技术分享

8. 访问结果

http://10.0.1.62/upload/db1.html 技术分享

 

http://10.0.1.62/upload/db2.html  技术分享

 

http://10.0.1.63/upload/db1.html?a=1  技术分享

 

http://10.0.1.63/upload/db2.html?bfdsfads^*()^&  技术分享

 

http://10.0.1.63/upload/db3.html  技术分享

 

 

http://10.0.1.62/upload/db1/test.html  技术分享

 

http://10.0.1.62/upload/db2/test.html  技术分享

 

http://10.0.1.63/upload/db1/test.html  技术分享

 

http://10.0.1.63/upload/db2/test.html  技术分享

 

http://10.0.1.63/upload/db3/test.html  技术分享

 

Nginx配置try_files实践