Lighttpd both http and https

 

We can configure both http & https on lighttpd webserver.

Use following configuration to serve both http & https requests:

server.document-root = "/var/www"
server.upload-dirs = ( "/var/cache/lighttpd/uploads" )
accesslog.filename = "/var/log/lighttpd/access.log"
server.errorlog = "/var/log/lighttpd/error.log"
server.pid-file = "/var/run/lighttpd.pid"
server.username = "www-data"
server.groupname = "www-data"
server.port = 80

#Configuration for https
$SERVER["socket"] == ":443" {
  ssl.engine = "enable"
  ssl.pemfile = "/etc/lighttpd/certs/www.example.com.pem"
}

In above configuration http://www.example.com.pem file should contain both the private key and the certificate.

After updating lighttpd.conf file restart lighttpd:

$ service lighttpd restart

After restarting both http & https should work for your host.

Linghttpd – unknown config-key: accesslog.filename

Recently I faced wearied scenario where I am unable to view access logs of lighttpd.

Even though after adding accesslog.filename = “/var/log/lighttpd/access.log” line in lighttpd.conf file logs are not getting updated in lighttpd.conf file

After debugging for a while I seen following error in /var/log/lighttpd/error.log:

WARNING: unknown config-key: accesslog.filename (ignored)

To fix this issue I adding “mod_accesslog” to servers.modules in lighttpd.conf file & restarted lighttpd. After that issue got resolved.

Following is my server.modules after adding mod_accesslog:

server.modules = (
  "mod_access",
  "mod_alias",
  "mod_compress",
  "mod_redirect",
  "mod_accesslog",
  "mod_rewrite",
)

lighttpd – allow/block ip range

 

To allow some or multiple ip rages use below code:

$HTTP["remoteip"] =~ "192\.168\.1\.*|10\.2\.20\.*" {
    server.document-root = "/var/www"
}

Above code will allow only 192.18.1.* and 10.2.20.* ip rages and blocks all other ip’s.

To block some or multiple ip ragnes use below code

$HTTP["remoteip"] =~ "192\.168\.1\.*|10\.2\.20\.*" {
    url.access-deny = ( "" )
}

Above code will block 192.168.1.* and 10.2.20.* ip rages and allow all other ip’s.

Lighttpd – block user agent

 

In lighttpd we can block or deny access to a specific user agent.

Syntax to block user agent:

$HTTP["useragent"] =~ "User agent name" {url.access-deny = ( "" )}

where “User agent name” is any user agent.

Example:

$HTTP["useragent"] =~ "Xenu Link Sleuth" {url.access-deny = ( "" )}

Above example will block all requests with user agent name “Xenu Link Sleuth”.

Linux reuse command history with shortcuts

Using command line history effectively will boost your productivity.
Here are some tips:

Repeat last executed command:

$ !!

or

$ !-1

Execute command with a key word:

$ !?wc?

It will run most recently executed command with with key word wc.

Execute last executed command that starts with a key word:

$ !wc

It will run most recently executed command that starts with wc

Execute first command in history file:

$ !1

Execute most recent nth command from history:

$ !-n

where n is number.

Execute line number 10 in the history:

$ !n

where n is number, it will execute command in nth line from command line.

-Sany

Lighttpd redirect all requests to new domain/host

To redirect all requests to new domain add following condition in your lighttpd.conf and restart lighttpd.

Here in the example I am trying to redirect all requests coming to http://www.dummy.com to http://www.example.com

$HTTP["host"] =~ "www\.dummy\.com$" {

url.redirect = ( "^/(.*)" => "http://www.example.com/" )

}

After adding above rule restart lighttpd.

$ service lighttpd restart

Now open the browser and type http://www.dummy.com and it should automatically redirected to http://www.example.com.

In above example according to your requirement change the domain/host names and test.

-Sany

Lighttpd redirect all requests to www

 

To redirect all request from example.com to http://www.example.com add following configuration in lighttpd.conf file.

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

After adding restart lighttpd service.

$service lighttpd restart

Now open example.com in your browser and test, it should be redirected to http://www.example.com

-Sany

Lighttpd add expire and cache-control headers

For static content like image we can add expire headers in lighttpd.

There are two ways to add expire headers.

  1. with expire.url
  2. with setenv.add-response-header

Add any of following configuration in lighttpd.conf and restart after adding.

With expire.url:

$HTTP["url"] =~ "\.(jpg|gif|png|css|js|txt|ico)$" {
     expire.url = ( "" => "access plus 2 days" )
}

In some versions of lighttpd above rule wont works, in that case use setenv.add-response-header to set expire interval.

With setenv.add-response-header:

$HTTP["url"] =~ "\.(jpg|gif|png|css|js|txt|ico)$" {
    setenv.add-response-header = ( "Cache-Control" => "max-age=290304000, public" )
}

-Sany