Permanent redirect with nginx

server {
    server_name domain.com;
    rewrite ^(.*)$ http://www.domain.com$1 permanent;
}

This entry was written by CharlyBr, posted on March 20, 2009 at 7:12 am, filed under http and tagged . Leave a comment or view the discussion at the permalink.

Nginx and worker_connections are more than open file resource limit warning

If you encounter this warning message under Linux:

2009/03/09 21:23:19 [warn] 26827#0: 4096 worker_connections are more than open file resource limit: 1024

A solution is to use the command ulimit in nginx start script, just before lunching nginx:

[...]
ulimit  -n 65536
[...]

This entry was written by CharlyBr, posted on March 9, 2009 at 10:30 pm, filed under Distro, http and tagged , , , . Leave a comment or view the discussion at the permalink.

Amazon is moving into CDN market

As read in Diamond Notes, Amazon is moving into CDN market with CloudFront.

Some Content Delivery Network providers

This entry was written by CharlyBr, posted on November 25, 2008 at 11:46 pm, filed under http and tagged . Leave a comment or view the discussion at the permalink.

Fix nginx increase server_names_hash_bucket_size error

When adding new virtual hosts in your nginx configuration file, you can experience this error message:

# nginx -t
2008/11/13 09:37:03 [emerg] 12299#0: could not build the server_names_hash, you should increase server_names_hash_bucket_size: 32
2008/11/13 09:37:03 [emerg] 12299#0: the configuration file /etc/nginx/nginx.conf test failed

server_names_hash_bucket_size controls the maximum length of a virtual host entry (ie the length of the domain name).

In other words, if your domain names are long, increase this parameter.

You need to add this flag in the http context:

http {
    server_names_hash_bucket_size 64;
    ...
}

After increasing the value, test your configuration file and reload nginx:

# nginx -t
2008/11/13 09:48:06 [info] 12315#0: the configuration file /etc/nginx/nginx.conf syntax is ok
2008/11/13 09:48:06 [info] 12315#0: the configuration file /etc/nginx/nginx.conf was tested successfully
# kill -HUP `cat /var/run/nginx.pid`

This entry was written by CharlyBr, posted on November 14, 2008 at 11:28 am, filed under http and tagged . Leave a comment or view the discussion at the permalink.

Maintenance mode (HTTP 503) with lighttpd and PHP

When you put your website in maintenance mode, it’s a good idea to return a HTTP 503 error code to the client.

This code indicates that “the server is currently unable to handle the request due to a temporary overloading or maintenance of the server”.

The 503 code is used to avoid crawlers or caching proxy use the maintenance page as the new valid content for the request. You certainly don’t want Google save this content in his search index as the content of your website :)

To achieve this we will use a rewrite rule in lighttpd to redirect all requests to a single PHP script which will return a 503 error code and print an informative message.

Lighttpd configuration

url.rewrite = ( "" => "/maintenance.php" )

This configuration will redirect any request to maintenance.php script.

If you need to serve an image in your maintenance page, you have to add another rule to the rewrite process like that :

url.rewrite = ( "upgrading.png" => "$0",
                "" => "/maintenance.php" )

Let some users see the website

It might be usefull to let admins or developers access the website during the maintenance.
For that, you can disable the maintenance rewrite rule for certain IP addresses :

 $HTTP["remoteip"] != “192.168.1.42″ {
               url.rewrite = ( "upgrading.png" => "$0",
                      "" => "/maintenance.php" )
}

PHP code example

<?php
header("HTTP/1.1 503 Service Unavailable");
?>
We're currently upgrading our servers...

Links

This entry was written by CharlyBr, posted on July 22, 2008 at 2:28 am, filed under http and tagged , . Leave a comment or view the discussion at the permalink.

Permanent redirect (301) with lighttpd

If you want to redirect visitors that access your website without the ‘www’, you can use mod_redirect with the following syntax:

$HTTP[”host”] =~ “^lescampeurs\.org” {
    url.redirect = (
        ”^/(.*)$” => “http://www.lescampeurs.org/$1″
    )
}

Links:

This entry was written by CharlyBr, posted on June 30, 2008 at 11:47 pm, filed under http and tagged . Leave a comment or view the discussion at the permalink.

Rotate Apache logs with Cronolog

Cronolog is log rotation program which gives you a lot of options to template the log destination files. The common use is to split logs by year / month / day.

Here is how to configure Apache to send log entries to cronolog :

CustomLog "|/usr/sbin/cronolog /home/log/apache2/%Y-%m-%d_domain.com_access.log" combined

This will create a log file named 2008-06-02_domain.com_access.log for today.

Cronolog reads log entries from standard input and writes them to the output file specified by your template.

More examples

  • Rotate by month :
CustomLog "|/usr/sbin/cronolog /home/log/apache2/%Y-%m_domain.com_access.log" combined
  • Rotate by week number :
CustomLog "|/usr/sbin/cronolog /home/log/apache2/%Y-%W_domain.com_access.log" combined
  • Rotate hourly
CustomLog "|/usr/sbin/cronolog /home/log/apache2/%H_domain.com_access.log" combined

Links

This entry was written by CharlyBr, posted on June 4, 2008 at 7:33 am, filed under Logs, http and tagged , . Leave a comment or view the discussion at the permalink.

Deny access to .svn directories with Apache2 / lighttpd

If you’re using SVN to control your web application, your certainly need to deny access to .svn sub-directories.

With Apache2

    <DirectoryMatch "^/.*/\.svn/">
            Order allow,deny
            Deny from all
    </DirectoryMatch>

With lighttpd

    $HTTP["url"] =~ "/\.svn/" {
        url.access-deny = ( "" )
    }

This entry was written by CharlyBr, posted on May 28, 2008 at 7:17 am, filed under http and tagged , , . Leave a comment or view the discussion at the permalink.

WordPress replaces pound by nginx

After two years of using Pound, WordPress decided to switch to Nginx as software load balancers for WordPress.com. Read the full story on Barry’s Blog.

This entry was written by CharlyBr, posted on May 8, 2008 at 7:12 am, filed under http and tagged . Leave a comment or view the discussion at the permalink.

Use mod_redirect with lighttpd

Enable mod_redirect

server.modules += ( "mod_redirect" )

Force your domain with www.

$HTTP["host"] =~ "^domain\.com$" {
    url.redirect = ( "^/(.*)" => "http://www.domain.com/$1" )
}

Force your domain without www.

$HTTP["host"] =~ "^www\.domain\.com$" {
    url.redirect = ( "^/(.*)" => "http://domain.com/$1" )
}

Redirect HTTP requests to HTTPS

$SERVER["socket"] == ":80" {
    $HTTP["host"] =~ "(.*)" {
        url.redirect = ( "^/(.*)" => "https://%1/$1" )
    }
}

Redirect an old domain to your new domain

$HTTP["host"] =~ "^www1\.domain\.com" {
    url.redirect = ( "^/(.*)" => "http://www.domain.com/$1" )
}

Redirect a url that can be matched by a mod_rewrite rule

Lets say that you want to redirect http://www.domain.com/party to http://blog.domain.com/party while you have a mod_rewrite rule on www.domain.com that redirect all URLs to a controller (“^(.*)$” => “/index.php$1″).

Your redirect will not work because mod_rewrite always execute before redirect rules. To bypass this, you need to use the $0 as the rule target to pass URLs through unmangled.

e.g.

url.redirect = ( "^/party(.*)" => "http://blog.domain.com/party$1" )
url.rewrite-once = ( "^/party" => "$0" )

tested with lighttpd 1.4.x

This entry was written by CharlyBr, posted on May 6, 2008 at 12:56 pm, filed under http and tagged . Leave a comment or view the discussion at the permalink.