0

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

}

Angelo Suerte
  • 23
  • 1
  • 1
  • 6

2 Answers2

1

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.

rhetonik
  • 1,818
  • 1
  • 15
  • 21
  • I have seen this also on other guides. But still I tried to tweak my default.conf using that but unfortunately it gave me Error 503 and after restarting nginx in command line: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) nginx: [emerg] still could not bind()... And also I am not really particular with the If's, only in PHP (smile). – Angelo Suerte Dec 27 '12 at 15:54
  • `...0.0.0.0:80 failed (98: Address already in use)` indicates that your main nginx configuration is trying to bind nginx with port 80 which is being used by some other process. You might want to check the process PID which uses port 80 using `sudo fuser -n tcp 80`. Further, you could check the process using `ps aux | grep `. If you do not intend to bind nginx with port 80, look for a server block which has `listen 80;` in it and change that port to something else. – rhetonik Dec 28 '12 at 04:09
  • After running fuser -n tcp 80 it gave me "80/tcp: 14537". What does it mean? Where can I edit that server block which listens to 80 'cause I can't find it in default.conf file? – Angelo Suerte Dec 28 '12 at 13:41
  • Can you try executing `ps aux | grep 14537` and paste it's output here? – rhetonik Dec 28 '12 at 13:51
  • nobody 14537 0.0 0.6 269892 3788 ? Sl Dec26 1:03 /usr/sbin/varnishd -P /var/run/varnishd.pid -a :80 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,64m root 21793 0.0 0.1 8104 924 pts/0 S+ 13:59 0:00 grep --color=auto 14537 – Angelo Suerte Dec 28 '12 at 13:59
  • Looks like Varnish is running with port 80 on your system. If you are sure you don't want to use Varnish, you may shut it down. Else, You might want to search for `listen 80` occurrences in your nginx configuration. – rhetonik Dec 28 '12 at 14:09
  • Also, I have updated my answer with a `listen 8080` for server block which handles redirects. Maybe that one was defaulting to port 80 earlier. – rhetonik Dec 28 '12 at 14:13
  • I stopped the varnish for the mean time. First it gives error or blank page, so I changed the listen port from 8080 to 80. After that it worked fine. Static files redirected to www already. By the way, can I change varnish port to 8080 to make it work? – Angelo Suerte Dec 28 '12 at 14:26
  • I haven't used varnish so far. You could go through their documentation at https://www.varnish-cache.org/docs though. – rhetonik Dec 28 '12 at 14:39
  • However, I feel you should move your redirect rules from nginx to Varnish or a higher level than that. Typically, systems using Varnish would want Varnish to listen on default HTTP port 80 and let it be the front-facing application for handling HTTP requests. Varnish could then proxy these requests to other backends (nginx on port 8080 in your case). – rhetonik Dec 28 '12 at 14:41
  • By the way @rhetonik, I found out that you can still use the 301 redirect in nginx like what you gave before without shutting down the varnish. But, before restarting nginx, shutdown the varnish first. After restarting the the nginx, when you open the pageantly.com or www.pageantly.com, it gives error page. However, after restarting the the varnish, everything is back to normal with 301 redirects in place. – Angelo Suerte Dec 28 '12 at 15:42
1

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.

Deepak Singhal
  • 10,568
  • 11
  • 59
  • 98
  • I use route 53. *.pageantly.com is pointed to my elastic IP as well as non-www (pageantly.com). So what you mean is redirect "pageantly.com" to alias "www.pageantly.com" instead of using its elastic IP? – Angelo Suerte Dec 28 '12 at 13:26
  • I tried to change the A value of "pageantly.com" from elastic IP to "www.pageantly.com" using alias but it says "The record set could not be saved because: - Alias Target contains an invalid value." So the record cannot be added. – Angelo Suerte Dec 28 '12 at 13:37
  • Here is my configuration I use. example.com [ blank in the first text box]. Type as A. Alias as Yes. Alias Target as www.example.com. Routing Policy as Simple. – Deepak Singhal Dec 28 '12 at 14:20
  • Yes, this is what I did. And I tried it again, still not able to add. – Angelo Suerte Dec 28 '12 at 14:38
  • I am really sorry dude.. Not sure why this is creating problems for you ! Other possible solutions are .htaccess file; or creating a filter in your web.xml to achieve same re-direction. – Deepak Singhal Dec 28 '12 at 17:38
  • It's okay. Finally found out using nginx redirect. Thanks by the way. – Angelo Suerte Dec 29 '12 at 01:36