Reverse-proxying web applications with nginx

While you can use Apache as a reverse proxy, it tries to be too smart. Try nginx instead, it's much simpler to set up.

After struggling with Apache for quite a while, since I simply could not make it pass through the Digest Authentication of my target host (that I proxied to), I switched to nginx. Here is what I did.

  1. Have nginx

    sudo apt-get install nginx
    
  2. Define your nginx config, e.g. at /etc/nginx/conf.d/reverse-proxy.conf:

    server {
      listen 127.0.0.1;
      
      location /...
    

Ruby: How to update all values of a Hash

There are many solutions, but a very concise one is this:

hash.merge!(hash) do |key, old_value, new_value|
  # Return something
end

The block of merge! will be called whenever a key in the argument hash already exists in the base hash. Since hash is updated with itself, each key will conflict and thus allow you to modify the value of each key to whatever you like (by returning old_value you'd get the behavior of Rails' reverse_merge!, by re...

Latency Numbers Every Programmer Should Know

A list of common computer I/O actions and how long they take.

Visual comparison chart: http://i.imgur.com/k0t1e.png

PostgreSQL: Expanded display and other command line features

One useful postgres command I stumbled upon recently was \x. It gives you an expanded display which allows you to actually read the results of your select * from queries. The link below describes a few more useful techniques and commands.

AngularJS: How to force Content-Type on GET and DELETE requests

While you usually do not need a Content-Type on GET request (which have a blank body), an external API may still force you to send one.
Angular's $http service will strip that header when the request data (body) is blank. [1] This is possibly a misconception of RFC2616.

Here is how to send GET requests with a Content-Type header in Angular.

Example

Consider this request:

$http({ me...

Shrine - A file upload toolkit

Now that CarrierWave is no longer maintained, Shrine might be worth a look.

Thunderbird: How to compose an HTML e-mail (when plain-text messages are your default)

Nobody needs HTML e-mails. However, you occasionally might have to write an HTML message for some weird reason. Here is how:

  1. Hold the Shift-Key while clicking on "Write", "Reply", "Reply All", or "Forward".

That's it. :)

Designing a landing page that sells

A great walkthrough through several design iteration of the same landing page.

Remove Rubygems deprecation warnings

Rubygems can produce lots of deprecation warnings, but sometimes, you cannot fix them. To have a tidy terminal with output that matters, add this to the top of your Gemfile and enjoy silence:

Deprecate.skip = true if defined?(Deprecate.skip)
Gem::Deprecate.skip = true if defined?(Gem::Deprecate.skip)

# all gems go here ...

(Inspiration)

Count number of existing objects in Ruby

Sometimes you want to know exactly how many objects exist within your running Ruby process. Here is how:

stats = {}
ObjectSpace.each_object  {|o| stats[o.class].nil? ? stats[o.class] = 0 : stats[o.class] += 1 }; stats

=> {String=>192038, Array=>67690, Time=>2667, Gem::Specification=>2665, Regexp=>491, Gem::Requirement=>16323, Gem::StubSpecification=>2665, ...}

Maybe you want to sort it like this:

stats.sort_by {|k,v| v }

Configure RSpec to raise an error when stubbing a non-existing method

You can configure RSpec 3.3+ to raise an error when attempting to stub or mock a non-existing method. We strongly recommend to do this as non-verified stubs are a footgun.

You can enable this behavior by adding the following to your spec_helper.rb:

RSpec.configure do |config|
  config.mock_with :rspec do |mocks|
    mocks.verify_partial_doubles = true
  end
end

This will also replace stub_existing from our rspec_candy.

Sending TCP keepalives in Ruby

When you make a simple TCP connection to a remote server (like telnet), your client won't normally notice when the connection is unexpectly severed on the remote side. E.g. if someone would disconnect a network cable from the server you're connected to, no client would notice. It would simply look like nothing is being sent.

You can detect remote connection loss by configuring your client socket to send TCP keepalive signals after some period of inactivity. If those signals are not acknowledged by the other side, your client will terminat...

Check SSL certificates

Installing SSL certificates usually implies additionally using intermediate certificates. If one of them is missing, some SSL client implementations might fail with failures such as

curl

~ curl -v https://host-to-check
curl: (60) SSL certificate problem, verify that the CA cert is OK. Details:
error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed

OpenSSL

~ openssl s_client -connect host-to-check:443
...
verify error:num=20:unable to get local issuer certificate
...
v...

grosser/parallel

Utility methods to distribute each or map over multiple threads or processes.

How to repair a corrupt PDF

If you have issues with PDFs, fix them like this: pdftk <corrupted_file>.pdf output <fixed_file>.pdf

Background

I had an issue where an included PDF would not show up in a document created with xelatex. This is the relevant line of LaTeX code:

  \AddToShipoutPicture*{ \includegraphics[width=21cm]{/home/dominik/code/ihero/public/system/stationery/original/stationery.pdf} }

The included PDF is a stationery for invoices which users can upload themselves. It did work until someone updated their stationery with a nearly-iden...

Fix "subprocess installed post-removal script returned error exit status ..." when installing/removing/updating a package with apt

If you get an error like:

subprocess installed post-removal script returned error exit status 78

when installing/removing/updating a package with apt you should check the postinst, postrm, prerm, ... script in /var/lib/dpkg/info/.

For example in my case I had a problem when removing varnish:

Removing varnish (4.0.3-2~trusty) ...
dpkg: error processing package varnish (--remove):
 subprocess installed post-removal script returned error exit status 78
Errors were encountered while processing:
 varnish

So I checked ...

Show or hide a jQuery element given a condition

If you have jQuery code like this:

if (condition) {
  $element.show();
} else {
  $element.hide();
}

... you can shorten this to:

$element.toggle(condition);

Test downstream bandwidth of Internet connection

You want to test your 1GE or 10GE internet uplink? We needed to ensure we have full 10GE to the backbone for a customer project.

Using netcat

To test whether we can achieve the bandwidth internally, you can use netcat and dd like this:

On your first server: nc -v -l 55333 > /dev/null
On your second server: dd if=/dev/zero bs=1024K count=5000 | nc -v $remote_ip 55333

You should see some output like this:

user@xxx:~ % dd if=/dev/zero bs=1024K count=5000 | nc -v removed 55333
Connection to 91.250.95.249 55333 port [...

How to remove properties of ActiveRecord scopes

When dealing with AR scopes, you can remove conditions, order, etc by using the unscope method.

It is available on Rails 4+.


Examples

Consider an exemplary User class as follows. For the examples below, we will use a scope that applies all its constraints.

class User < ActiveRecord::Base
  scope :active, -> { where(locked: false) }
  scope :admins, -> { where(role: 'admin') }
  scope :ordered, -> { order(:name) }
end

users = User.active.admins.ordered

^
SELECT "users".* FROM "users" WHERE "use...

Detect the current jQuery version

You can say this in Javascript:

$.fn.jquery
=> "1.11.1"

Find your Thunderbird passwords

You cannot find your account passwords in the Account Settings – that'd be too easy. Here is where you find them:

Preferences > Security > Passwords > Saved Passwords… > Show Passwords

A case for Redactor

Redactor is yet another WYSIWYG editor. It definitely has its weak points, but I want to point out that it has clear strengths, too.

Pro

  • Simple and beautiful interface.
  • Outstandingly organized source code. Have never seen a JS library that was this structured.
  • Clear, comprehensive and searchable API documentation. Filled with code examples.
  • Easily customizable: specify toolbar buttons, pass various callbacks, etc.
  • Features a collection of great [plugins](ht...

How to disable Rails raising errors on pending migrations in development

Rails 4 introduced raising an error on pending migrations. This is most annoying when you are crafting a migration but need to play with your application to figure out how to do it.

To disable this behavior, just set the corresponding config option to false:

# in config/environments/development.rb

# Raise an error on page load if there are pending migrations.
config.active_record.migration_error = false # was :page_load