HowtoForge Forums | HowtoForge - Linux Howtos and Tutorials

HowtoForge Forums | HowtoForge - Linux Howtos and Tutorials (
-   Installation/Configuration (
-   -   Nginx for static and $host (

GarGamel55 7th August 2011 21:33

Nginx for static and $host

I use Nginx Catch-All Host As Front End To Apache (for my static files) :


location ~* ^.+.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|swf|flv|html|htm|mp3)$ {
        root  /var/www/$host/web;
        access_log off;
        expires 30d;

I prefer that my domain name appear as www., but with the How TO, all domains starting with www will be redirected to http://domain.

In my panel, i set up my website with auto subdomain "www". So with this line "root /var/www/$host/web;" in /etc/nginx/sites-available/default it's returns a 404 error for the static files on my websites. Is normal because $host = and ispconfig is configure for

If i replace manualy $host by, it works. However, I 've several websites.

The solution would be to rename the $host variable into /etc/nginx/sites-available/default, but is that possible? Such as php $host = str_replace('.www', '', $host);

Thanks ! (again :()

falko 8th August 2011 11:28

You could simply create a vhost for and redirect it to the vhost:


server {
  rewrite ^$request_uri? permanent;

GarGamel55 25th August 2011 16:41

Thanks Falko,

But this rules doesn't work for me (redirect loop error)

However, I'm inspired by your idea and I just created a virtual host like this www.domain.tld in /etc/nginx/sites-enabled/

In this file, I wrote :


server {
        server_name www.domain.tld;

        location ~* \.(jpg|jpeg|gif|css|png|js|ico|swf|mp3)$ {
        #replace $host by domain without www.
        root  /var/www/domain.tld/web;
        expires      7d;
        access_log    off;

        location / {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        access_log off;

And all is ok. Nginx catch all statics and apache php

Voilą ! :D

All times are GMT +2. The time now is 21:35.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.