Nginx Global Server Settings

I want to user access deny each file under .git directory
there are many virtual hosts conf in my /etc/nginx/conf.d/

server {
    server_name  aaa.xxx.com

    #deny access to .git
    location ~ /\.git {
      deny all;
    }
}



server {
    server_name  bbb.xxx.com

    #deny access to .git
    location ~ /\.git {
      deny all;
    }
}



server {
    server_name  ccc.xxx.com

    #deny access to .git
    location ~ /\.git {
      deny all;
    }
}

this is the simple way to work.but not the better way to work.
we can add some configs and include them.

create a global server settings file,put it into
/etc/nginx/conf.d/global/server_deny.conf

#/etc/nginx/conf.d/global/server_deny.conf
#deny access to .git
location ~ /\.git {
  deny all;
}


change virtual host conf


server {
    server_name  aaa.xxx.com
    include /etc/nginx/conf.d/global/*.conf;
}



server {
    server_name  bbb.xxx.com
    include /etc/nginx/conf.d/global/*.conf;
}



server {
    server_name  ccc.xxx.com
    include /etc/nginx/conf.d/global/*.conf;
}



$ service nginx restart


done and enjoy it~!

留言