Nginx for static and $host

Discussion in 'Installation/Configuration' started by GarGamel55, Aug 7, 2011.

  1. GarGamel55

    GarGamel55 Member


    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 :()
  2. falko

    falko Super Moderator ISPConfig Developer

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

    server {
       rewrite ^$request_uri? permanent;
  3. GarGamel55

    GarGamel55 Member

    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/[B]domain.tld[/B]/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
    Last edited: Aug 25, 2011

Share This Page