Nginx : Block Referrer Spam

In this article, we will show you how to block referrer spam in one of our Nginx web server. 1. Find the Patterns Check the Nginx access.log file, and identify the “referrer spam” patterns. $ sudo tail -f /var/log/nginx/access.log Some patterns : 200 http://???.ru/engine/redirect.php?url= 200 http://???.com/tp/out.php?link=alternatevideo&url=http%3A// 200 http://???.edu/online/redirect.asp?url=http://mywebsite/site/ We areContinue Reading


Nginx + Apache Tomcat configuration example

This tutorial shows you how to configure Nginx as a reverse proxy to redirect the traffics from port 80 to Apache Tomcat on port 8080. Here is the environment in my Linode server : Debian 7.5 Nginx 1.2.1 Tomcat 7.0.28 P.S Both Nginx and Tomcat are installed via apt-get install.Continue Reading


Java : Return a random item from a List

Normally, we are using the following ways to generate a random number in Java. 1. ThreadLocalRandom (JDK 1.7) //Generate number between 0-9 int index = ThreadLocalRandom.current().nextInt(10); 2. Random() //Generate number between 0-9 Random random = new Random(); int index = random.nextInt(10); 3. Math.random() //Generate number between 0-9 int index =Continue Reading


Nginx : Block User Agent

In Nginx, you can block certain user agents (normally it is crawler) like this : /etc/nginx/sites-enabled/default server { listen 80; server_name; root /etc/tomcat7/webapps/mysite;   if ($http_user_agent ~* (ahrefs|wget|crawler|majestic) ) { return 403; }   location / { <!– xxx –> } } In above example, for “user agent” thatContinue Reading


Count IP address in Nginx access logs

Recently, many referer spam hit on my server, below is the command I used to find and count the IP Address from a Nginx access log file. $ sudo awk ‘{print $1}’ /var/log/nginx/access.log | sort | uniq -c | sort -nr Full example. $ sudo awk ‘{print $1}’ /var/log/nginx/access.log |Continue Reading


jsoup : Send search query to Google

This example shows you how to use jsoup to send a search query to Google. Document doc = Jsoup .connect(""); .userAgent("Mozilla/5.0") .timeout(5000).get(); Unusual traffic from your computer network Don’t use this example to spam Google, you will get above message from Google, read this Google answer. 1. jsoup example ExampleContinue Reading


Java – Read a file from resources folder

In this tutorial, we will show you how to read a file from a resources folder, in both Java and Unit Test environment. In simple, put files in a resources folder, and read the file with following code snippets : ClassLoader classLoader = getClass().getClassLoader(); File file = new File(classLoader.getResource("file/test.xml").getFile()); 1.Continue Reading


How to change Tomcat manager default path ?

By default, Tomcat’s manager webapp is able to access via yourapp:8080/manager. It is a good practice to change the default /manager to something else, to avoid potential brute force attack. Environment Tomcat 7 (apt-get install) Debian 7.5 1. Solution This article show you how to change / rename the defaultContinue Reading


How to change Tomcat to use JDK 7

The current environment is using Tomcat 7 + JDK 1.6. Installed a JDK 1.7, but how to tell Tomcat to use the latest JDK? Environment Tomcat 7 (apt-get install) Debian 7.5 JDK 6 and JDK 7 1. Solution By default, Tomcat will pick up the JDK which configured in theContinue Reading


Find out Tomcat is using which JDK or JAVA_HOME

In this article, we will show you how to find out which JRE / JDK or JAVA_HOME is used by the Tomcat container. Environment : Debian 7.5 Tomcat 7, installed via apt-get JDK 6 and JDK 7 Note To understand how Tomcat pick up which JDK to run, refer toContinue Reading

log4j logo

Log4j Tutorial

Apache log4j, a classic logging tool in Java. Here are a series of Log4j 1.2 tutorials.


Eclipse – red-x icon didn’t display on project explorer

In Eclipse IDE, If a project contains errors, a small “red-x” icon will be displayed in the files that are causing the error. This useful feature is supported in Java related perspectives only, for example, “Package Explorer”. 1. Problem Personally, I prefer to use the “Project Explorer” perspective (pure folderContinue Reading


MongoDB : write Aggregation result into a new collection

This article shows you 2 ways to export a MongoDB “aggregation” result into another new collection. 1. $out Example This $out operator is New in version 2.6. 1.1 Review a simple grouping example, it writes the result to a new variable “result”. > var result = db.hc_hosting.aggregate( { $group :Continue Reading


Ubuntu : Status 14: The disk contains an unclean file system

Dual boot Windows and Ubuntu, but Ubuntu is unable to access the Windows partition /dev/sdb3, shows the following error message : Error mounting /dev/sdb3 … status 14: The disk contains an unclean file system (0, 0). Metadata kept in Windows cache, refused to mount. Failed to mount ‘/dev/sdb3′: Operation notContinue Reading


Linux : How to gzip a folder

On Linux, gzip is unable to compress a folder, it used to compress a single file only. To compress a folder, you should use tar + gzip, which is tar -z. Note $ tar –help -z, -j, -J, –lzma Compress archive with gzip/bzip2/xz/lzma For example, tar -zcvf outputFileName folderToCompress 1.Continue Reading

Page 1 of 9712345...102030...Last »