phpmypython
4 years ago
1180
7
Forge

Redirect Loop for non-www domains to www domain.

Posted 4 years ago by phpmypython

Im working on trying to forward all non-www request to www request for a site i setup in forge.

This is my nginx configuration

server {
     listen 80;
     server_name domain.com;
     return 301 $scheme://www.domain.com$request_uri;
}

server {
    listen 80;
    server_name www.domain.com;
    root /home/forge/www.domain.com/public;

    # FORGE SSL (DO NOT REMOVE!)
    # ssl_certificate;
    # ssl_certificate_key;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

    index index.html index.htm index.php;

    charset utf-8;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    access_log off;
    error_log  /var/log/nginx/www.domain.com-error.log error;

    error_page 404 /index.php;

    location ~ \.php$ {
        proxy_intercept_errors on;
        error_page 502 = @fallback;

        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root;
        include fastcgi_params;
    }

    location @fallback  {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }
}

I know that forge will auto generate a file with www prepended to what ever site you make so i removed that one and now there's only one conf file that should be firing when i visit the domain. If i do a wget on the non-www domain then i can see that it's hitting the 301 but then when it begins a new connection with the www domain it 301's back to the non-www domain thus causing the redirect. Im not sure where ive messed up here and i would really appreciate some help

Please sign in or create an account to participate in this conversation.