Question

After seeing this post http://www.ewanleith.com/blog/900/10-million-hits-a-day-with-wordpress-using-a-15-server I changed my server from apache2 to nginx. I am no computer geek just, savvy. I followed the steps. After that, the site was perfect, except for one thing: non-www to www thing. I searched all over the net on how to do this. I tried the modrewrite thing they said but just getting worst. For now, it is directed to www because I use wordpress and set it in general settings http://www.pageantly.com. Yet, I have static directories and it is in plain non-www. Please take a look on my default.conf in /etc/nginx/conf.d/ as well as the tutorial with link above:

server {
server_name pageantly.com www.pageantly.com;
root /var/www/;
listen 8080;
## This should be in your http block and if it is, it's not needed here.
index index.html index.htm index.php;

include conf.d/drop;

 location / {
            # This is cool because no php is touched for static content
try_files $uri $uri/ /index.php?q=$uri&$args;
    }

        location ~ \.php$ {
        fastcgi_buffers 8 256k;
        fastcgi_buffer_size 128k;
        fastcgi_intercept_errors on;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass unix:/dev/shm/php-fpm-www.sock;
    }

# BEGIN W3TC Page Cache cache
location ~ /wp-content/w3tc/pgcache.*html$ {
add_header Vary "Accept-Encoding, Cookie";
   } 
[...]
}
# END W3TC Page Cache core

}

Was it helpful?

Solution

Ideally, each domain (sub-domains included) should have a separate server block. Going by that, your configuration would look like:

# Following block redirects all traffic coming to pageantly.com to www.pageantly.com
server {
  server_name pageantly.com;
  listen 8080;

  # Send a 301 permanent redirect to any request which comes on this domain
  return 301 http://www.pageantly.com$request_uri;
}

# Following block handles requests for www.pageantly.com
server {
  server_name www.pageantly.com;
  listen 8080;
  root /var/www;
  [...] # all your default configuration for the website
}

Another unclean and inefficient way to achieve this would be to introduce an if statement which reads domain value and branches flow accordingly either to redirect traffic (in case of pageantly.com) or to process requests (in case of www.pageantly.com) but I would recommend you avoid going by that route.

Hope this helps.

OTHER TIPS

If you are using Route 53 on AWS; then you do NOT have to do any such thing. On Route53 itself we can create an alias and configure so that non-www is redirected to www.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top