What is causing this 301 redirect?

30,100

Solution 1

The presence of an X-Powered-By: PHP header means that wordpress is issuing the 301. It's due to wordpress forcing www.mylesgray.com. When you use a nonstandard port, user agents will generally include the port in the Host: header. Try adding

fastcgi_param HTTP_HOST $host;

with the rest of your fastcgi_param directives (or alog with your "include fastcgi_params;") and it should fix this.

Solution 2

You should add a '/' at then end of your URLs. Furthermore if you run ab http://foo.com it will return you a "ab: invalid URL" error. If you do "ab -t 10 http://example.com/" everything will work fine. You should always use '/' in your URLs otherwize your webserver will try to redirect the page to the home page automatically for you which generates an undesirable extra load on the server and some extra bytes on the wire.

You web server told you what it did:

'/' is missing and something is incorrect with the port numer:

# curl -I http://www.mylesgray.com:8080
HTTP/1.1 301 Moved Permanently
[...]
======> Location: http://www.mylesgray.com/

'www' and '/' are missing:

# curl -I http://mylesgray.com
HTTP/1.1 301 Moved Permanently
[...]
=======> Location: http://www.mylesgray.com/
[...]

'/' and 'www' are missing:

# curl -I http://mylesgray.com:8080
HTTP/1.1 301 Moved Permanently
[...]
========> Location: http://www.mylesgray.com/

'hope that helps :)

Share:
30,100
Myles Gray
Author by

Myles Gray

#C.V. / Resumé# blah.cloud

Updated on February 06, 2020

Comments

  • Myles Gray
    Myles Gray about 4 years

    I have a problem with my server redirecting http://www.mylesgray.com:8080/ -> http://www.mylesgray.com/.

    Here are my Nginx default and fastcgi_params config files:

    https://gist.github.com/1745271

    https://gist.github.com/1745313

    This is rather a nusance as I am trying to run a benchmark of Nginx w/ caching vs Varnish w/ caching on top of Nginx to see if there is any performance benefit of one over the other.

    As such I have straight Nginx w/ caching listening on port 8080 and varnish on port 80 which forwards any non-cached requests to Nginx on localhost:8080, so obviously what I want to do is run an ab benchmark on http://www.mylesgray.com:8080/ and on http://www.mylesgray.com/ to see the difference.

    Here are the results of curl -I on various addresses.

    # curl -I http://www.mylesgray.com:8080
    
    HTTP/1.1 301 Moved Permanently
    Server: nginx/0.7.65
    Date: Sun, 05 Feb 2012 12:07:34 GMT
    Content-Type: text/html; charset=UTF-8
    Connection: keep-alive
    X-Powered-By: PHP/5.3.2-1ubuntu4.7ppa5~lucid1
    X-Pingback: http://www.mylesgray.com/xmlrpc.php
    Location: http://www.mylesgray.com/
    
    # curl -I http://mylesgray.com
    
    HTTP/1.1 301 Moved Permanently
    Server: nginx/0.7.65
    Content-Type: text/html; charset=UTF-8
    X-Powered-By: PHP/5.3.2-1ubuntu4.7ppa5~lucid1
    X-Pingback: http://www.mylesgray.com/xmlrpc.php
    Location: http://www.mylesgray.com/
    Content-Length: 0
    Date: Sun, 05 Feb 2012 12:15:51 GMT
    X-Varnish: 1419774165 1419774163
    Age: 15
    Via: 1.1 varnish
    Connection: keep-alive
    
    # curl -I http://mylesgray.com:8080
    
    HTTP/1.1 301 Moved Permanently
    Server: nginx/0.7.65
    Date: Sun, 05 Feb 2012 12:16:08 GMT
    Content-Type: text/html; charset=UTF-8
    Connection: keep-alive
    X-Powered-By: PHP/5.3.2-1ubuntu4.7ppa5~lucid1
    X-Pingback: http://www.mylesgray.com/xmlrpc.php
    Location: http://www.mylesgray.com/
    

    Then running curl -I http://www.mylesgray.com gives:

    # curl -I http://www.mylesgray.com
    
    HTTP/1.1 200 OK
    Server: nginx/0.7.65
    Content-Type: text/html; charset=UTF-8
    X-Powered-By: PHP/5.3.2-1ubuntu4.7ppa5~lucid1
    X-Pingback: http://www.mylesgray.com/xmlrpc.php
    Content-Length: 5132
    Date: Sun, 05 Feb 2012 12:07:29 GMT
    X-Varnish: 1419774133 1419774124
    Age: 30
    Via: 1.1 varnish
    Connection: keep-alive
    

    So as you can see 80 is served by Varnish and 8080 by Nginx but I cannot find anywhere anything that does a 301 redirect, not in nginx.conf or in the sites-enabled/default file and I don't believe it is caused by Wordpress itself but an very much open to correction.

    Please help, this is driving me nuts!

    Myles

  • Myles Gray
    Myles Gray about 12 years
    I have added this to my /etc/nginx/fastcgi_params and restarted all services but curl is still showing 301's for all except http://www.mylesgray.com.
  • Myles Gray
    Myles Gray about 12 years
    Here is my default site conf and my fastcgi_params file respectively: gist.github.com/1745271 and gist.github.com/1745313
  • kolbyjack
    kolbyjack about 12 years
    Which version of nginx are you running? Overriding request headers with fastcgi_param wasn't added until 0.8.40, it seems.
  • Myles Gray
    Myles Gray about 12 years
    Ah... that may be it - I am running whatever comes with 10.04, I will reinstall with 1.0.11 and see how that goes...
  • Myles Gray
    Myles Gray about 12 years
    You are the man! Excellent - works perfectly now!