Alex Brandt

An avid Linux user for many years (since 2004). Have moved through Fedora, Slackware, and finally settled on Gentoo as a distribution of choice. Hacking Gentoo and other software as well as playing bass pass the time and add a flair of fun.

May 012011
 

Introduction

I was recently requested to document common apache rewrite pitfalls and examples and crafted the following document as a response.  It is intended as a two page document (Rewrite Cheat Sheet) where the first page is a reference guide of commonly used rewrite variables and flags and the second page is a short list of examples, gotchas, and troubleshooting advice.

Rewrite Cheat Sheet

Common Variables

HTTP Headers

  • HTTP_USER_AGENT
  • HTTP_REFERER
  • HTTP_COOKIE
  • HTTP_FORWARDED
  • HTTP_HOST
  • HTTP_PROXY_CONNECTION
  • HTTP_ACCEPT

connection & request

  • REMOTE_ADDR
  • REMOTE_HOST
  • REMOTE_PORT
  • REMOTE_USER
  • REMOTE_IDENT
  • REQUEST_METHOD
  • SCRIPT_FILENAME
  • PATH_INFO
  • QUERY_STRING
  • AUTH_TYPE

server internals

  • DOCUMENT_ROOT
  • SERVER_ADMIN
  • SERVER_NAME
  • SERVER_ADDR
  • SERVER_PORT
  • SERVER_PROTOCOL
  • SERVER_SOFTWARE

date and time

  • TIME_YEAR
  • TIME_MON
  • TIME_DAY
  • TIME_HOUR
  • TIME_MIN
  • TIME_SEC
  • TIME_WDAY
  • TIME

specials

  • API_VERSION
  • THE_REQUEST
  • REQUEST_URI
  • REQUEST_FILENAME
  • IS_SUBREQ
  • HTTPS

Variable Descriptions

  • REQUEST_FILENAME
    • The full local filesystem path to the file or script matching the request, if this has already been determined by the server at the time REQUEST_FILENAME is referenced. Otherwise, such as when used in virtual host context, the same value as REQUEST_URI.
  • HTTPS
    • Will contain the text “on” if the connection is using SSL/TLS, or “off” otherwise. (This variable can be safely used regardless of whether or not mod_ssl is loaded).

Flag Descriptions

  • nocase|NC‘ (no case) :: This makes the test case-insensitive – differences between ‘A-Z’ and ‘a-z’ are ignored, both in the expanded TestString and the CondPattern.
  • ornext|OR‘ (or next condition) (RewriteCond Only) :: Use this to combine rule conditions with a local OR instead of the implicit AND.
  • ‘last|L’ :: Stop the rewriting process immediately and don’t apply any more rules.
  • ‘proxy|P’ :: Force the substitution URL to be internally sent as a proxy request.
  • ‘qsappend|QSA :: Appends any query string created in the rewrite target to any query string that was in the original request URL.
  • redirect|R[=code]‘ :: Forces an external redirect, optionally with the specified HTTP status code.
  • forbidden|F :: Returns a 403 FORBIDDEN response to the client browser.

Good Examples

  • Adding www to all requests
    • RewriteCond %{HTTP_HOST} !^www [NC]
    • RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R,L,QSA]
  • Forcing all requests to HTTPS
    • RewriteCond %{HTTPS} off
    • RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R,L,QSA]
  • Redirect a specific subweb to another domain
    • RewriteRule ^/?subweb/(.*) http://other.example.com/$1 [R,L,QSA]
  • Block specific IPs from access
    • RewriteCond %{REMOTE_ADDR} ^127\.0\.0
    • RewriteRule ^ – [F,L]
  • Creating a filesystem alias with modrewrite
    • RewriteRule ^/?alias/(.*) /var/www/vhosts/$1/httpdocs/$1 [L,R]
  • A condition to stop CMS software from over-riding fullstatus (added before the offending rewriterule)
    • RewriteCond ${REQUEST_URI} !server-status [NC]

Bad Examples

  • Recursive rewrite
    • RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R,L,QSA]

Gotchas

  • Some rewrites may conflict with existing rewrites provided by many CMS packages (wordpress, drupal, joomla, etc). Check for any existing rewrites in a .htaccess file.
  • Rewriterule and rewritecond can only be used in the following contexts: server config, virtual host, directory, .htaccess

Common Troubleshooting

  • Enable the rewrite logs with RewriteLog and RewriteLevel
    • RewriteLog <file path>
    • RewriteLogLevel 3 # ranges from 0 to 9
  • Check your regular expressions against a PCRE checker (they are very bountiful on the Internet [http://tinyurl.com/3hop7xu]).
  • Utilize curl to test redirects (R), `curl -I example.com`

Android Battery Life

 Android  Comments Off
Mar 062011
 

Recently, I’ve stumbled across an application in the android market, juice defender, that does exactly what it advertises. The application extends battery life through automated micromanaging of peripherals and software. This combined with another application, juice plotter, allows one to actually experiment with different battery preserving techniques (or different ROMs for the rooted phones).

More specifically, the application behaves much like any other power management software in other Linux installations. It controls the frequency scaling of the processor (if the phone is rooted), it controls networking interfaces and screens to help conserve power. All of this functionality can easily be configured and modified for your desired power settings, but the defaults will provide a significant boost to battery life. This is one more useful utility in a long list that allows us to get the most out of our phones.

Android Automation

 Android  Comments Off
Feb 272011
 

Recently I’ve found the wonderful Android application, profile valet.  This little application handles location and time based profiles for various settings on your Android device.  It can control things like sound, wifi and bluetooth based on time schedules or location (GPS backed).  Using a utility like this can help prevent the common, “I forgot to unsilence my phone,” resulting in missed calls, texts or emails.  It can also be used to control silencing your phone for certain events.  Simply setup a profile for various movie theaters to silence your phone automatically when you enter that area.

By automating the little things in life we can open up our minds to less stress and turn our attention to more fun or important matters.  I know offloading thought has been pushed by the “Getting Things Done” paradigm but it can be expanded with the smartphone.  It can be expanded to allow our devices to be the tool they were designed to be.

Feb 202011
 

Introduction

As I mentioned last time, you want to be comfortable with the existing documentation on CFLAGS before going crazy trying to play with. It also helps to have a good understanding of what you’re doing to the code when you modify these “sacred” parameters.

Alright, now that the CYA is out of the way let’s take this one step further. Last time we talked about figuring out which instruction sets your processor understood and how to figure out what `-m` flags would get those instruction sets into the binaries on your system. This time we’ll be talking about making sure those same flags are in your use flags (just to be sure they’re picked up by the system).

Finding Flag Names

So how do we find the flags that do what we want? Well, as always BASH is our friend and can be used to find this answer in a mostly automated fashion:

. /etc/make.conf && gcc -Q -c -v ${CFLAGS} --help=target | grep enabled

This displays the currently enabled flags based on your CFLAGS parameter and allows us to find which flags have use flags with the following one liner:

gawk '/-m.*/ { print $1 }' | cut -d 'm' --complement -f 1 | xargs -I{} equery h "{}"

Conclusion

Using a little scripting we can extract the necessary information to quickly determine if there are any use flags we should be adding for particular compiler flags that our system might support.  With this last level of optimization beyond the previous time’s we should be ready to move on to -O3 (for the daring) and watch our machine’s nose bleed.

Feb 152011
 

Introduction

It seems that disk snapshots have become a hot topic and a confusing topic.  I intend to simply outline what snapshots look like in terms of the lower layers of abstraction and nothing more.  Snapshots are built into things like LVM, SAN, etc but I will not be covering those technologies.  Instead, what I intend to cover is the abstractions ranging from the disk device to the snapshot.

The Disk

A hard disk is typically chopped up into pieces called partitions (and this is the unit I’ll use as my foundation).  These logical separations of the blocks on a disk allow us to make sure that disks are not over-allocated for one particular purpose or lose access to a system if one partition fills up, etc.  Partitioning is only the beginning.   We are able to take the idea of partitions and extend the idea of a contiguous region of blocks to the strange concept of snapshots as well.

Writing to Disk

Using our simplified view of a disk (a contiguous region of blocks) we can see that the simplest way to write data (ignoring filesystems) is to simply take our data one block at a time and place it on the disk.  This is great until we need to remove blocks or update blocks (which is probably why we only use this type of writing for tapes).  We’ll have to add some logic to this view of disks to handle the complexities of files but for all intensive purposes, disks are simply groups of blocks available for writing and groups of blocks that are already used.  If we want a consistent view of the data at any point it’s simply a matter of making sure nothing is writing to the disk (partition) we’re interested in.  This is where snapshots can help us out.

Snapshots

A snapshot creates another region of blocks we can write to.  Let me start that again … when you create a snapshot of some disk (snapshots are not standalone by any means) the following happens:

  • It reserves space for a changelog (or writes that would happen to the disk) in the difference disk
  • It creates a new way to access the original disk via the snapshot name (which refers to the base disk)
  • It begins sending all writes to the reserved space for the changelog (depicted in the figure below)

Thus, we have the following situation on the disk (again very simplified):

Deleting Snapshots

Since a snapshot isn’t a true partition what happens when one of these gets removed?  The obvious is that the snapshot name gets removed so it’s no longer accessible, but that’s not going to help rectify the two regions of data we now have on the disk.  This is handled by making the difference disk a literal list of differences.  I find it best to think of it (even though it may not actually be implemented in this fashion) as a queue of block changes or a transaction log for writes to the disk.  With this visualization of the process it makes sense as to what happens when the snapshot is removed:

  • The reference to the base disk is removed
  • The difference disk is replayed onto the base disk (applying all recorded changes)

That’s all there is to it.  Everything continues as if the snapshot exists until it has been fully replayed and then disk access resumes as normal without the snapshot.

Conclusion

Snapshots provide a convenient way to access a frozen image of a disk which is perfect for backups or point in time restores of data.  This is typically used via LVM, virtualbox, SANs, etc and has far-reaching impacts that allow system administration to be easier.

© 2011 Alunduil's Hosting Suffusion theme by Sayontan Sinha