Nginx for static and $host

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

  1. GarGamel55

    GarGamel55 New Member

    Hello,

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

    Code:
    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 = www.mydomain.com and ispconfig is configure for mydomain.com

    If i replace manualy $host by mydomain.com, 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

    You could simply create a vhost for example.com and redirect it to the www.example.com vhost:

    Code:
    server {
       server_name example.com;
       rewrite ^ http://www.example.com$request_uri? permanent;
    }
     
  3. GarGamel55

    GarGamel55 New 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 :

    Code:
    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_pass http://127.0.0.1:82;
            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