Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

We have a customer with a site running on Apache. Recently the site has been seeing increased load and as a stop gap we want to shift all the static content on the site to a cookieless domains, e.g.

The application is not well understood. So to give the developers time to amend the code to point their links to the static content server ( I thought about proxying the site through nginx and rewriting the outgoing responses such that links to /images/... are rewritten as

So for example, in the response from Apache to nginx there is a blob of Headers + HTML. In the HTML returned from Apache we have <img> tags that look like:

<img src="/images/someimage.png" />

I want to transform this to:

<img src="" />

So that the browser upon receiving the HTML page then requests the images directly from the static content server.

Is this possible with nginx (or HAProxy)?

I have had a cursory glance through the docs but nothing jumped out at me except rewriting inbound urls.

share|improve this question

There is a - "This module can search and replace text in the nginx response."

copy past from docs:


sub_filter string replacement


location / {
  sub_filter      </head>
  '</head><script language="javascript" src="$script"></script>';
  sub_filter_once on;
share|improve this answer
Is there anything like this for haproxy? – bradvido May 10 at 19:34
@bradvido I've not found such feature in haproxy. – Oleg Neumyvakin May 11 at 5:30

It is best to use the proxy feature and fetch the content from the appropriate place, as opposed to rewriting URLs and sending redirects back to the browser.

A good example of proxying content looks like:

#  This configuration file handles our main site - it attempts to
# serve content directly when it is static, and otherwise pass to
# an instance of Apache running upon
server {
    listen :80;

        access_log  /var/log/nginx/d-a.proxied.log;

        # Serve directly:  /images/ + /css/ + /js/
    location ^~ /(images|css|js) {
        root   /home/www/;
        access_log  /var/log/nginx/ ;

    # Serve directly: *.js, *.css, *.rdf,, *.xml, *.ico, & etc
    location ~* \.(js|css|rdf|xml|ico|txt|gif|jpg|png|jpeg)$ {
        root   /home/www/;
        access_log  /var/log/nginx/ ;

        # Proxy all remaining content to Apache
        location / {

            proxy_redirect     off;

            proxy_set_header   Host             $host;
            proxy_set_header   X-Real-IP        $remote_addr;
            proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;

            client_max_body_size       10m;
            client_body_buffer_size    128k;

            proxy_connect_timeout      90;
            proxy_send_timeout         90;
            proxy_read_timeout         90;

            proxy_buffer_size          4k;
            proxy_buffers              4 32k;
            proxy_busy_buffers_size    64k;
            proxy_temp_file_write_size 64k;

In this configuration, instead of redirecting requests to and expecting the browser to make another request, nginx simply serves the file from the relevant local path. If the request is dynamic then the proxy kicks in and fetches the response from an Apache server (local or remote) without the end user ever knowing.

I hope that helps

share|improve this answer
Thanks for the time spent answering this. I'll set up a test rig and see how this works. The important thing here is to move all the static content off of the Apache server. So I guess I could run nginx on the CDN server and have proxy_pass set to the Apache server, e.g. proxy_pass ? i.e. we move the site's public IP address to the nginx/CDN server. – Kev Nov 30 '11 at 23:25
Yep that's correct. And no probs - I'm knee deep in nginx too right now and loving it. – Tak Nov 30 '11 at 23:30
Haven't forgotten your answer :) Still not had a chance to try this yet. – Kev Dec 7 '11 at 14:46

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.