Nginx redirecting domain to port 8000 with a 301

301-redirectnginxphp-fpm

I have been looking everywhere to figure out why my nginx server is causing a 301 redirect on one domain, and not the other.
I have two sites configured on this server in an apache style (sites-available). Let's say domain1.com and domain2.com. I am running PHP-FPM as well.
Here are the configurations

Domain1.com

server {
        listen   80; ## listen for ipv4; this line is default and implied
        server_name domain1.com www.domain1.com; 

        root /var/www/domain1.com;
        index index.php index.html index.htm;


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

        #error_page 404 /404.html;

        # redirect server error pages to the static page /50x.html
        #   
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
                root /usr/share/nginx/www;
        }   

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #   
        location ~ \.php$ {
                try_files $uri =404;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;

        }   

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one 
        #   
        location ~ /\.ht {
                deny all;
        }   
}

Domain2.com

server {
        listen   80; ## listen for ipv4; this line is default and implied
        server_name domain2.com www.domain2.com; 

        root /home/mike/www;
        index index.php index.html index.htm;


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

        #error_page 404 /404.html;

        # redirect server error pages to the static page /50x.html
        #   
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
                root /usr/share/nginx/www;
        }   

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #   
        location ~ \.php$ {
                try_files $uri =404;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;

        }   

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one 
        #   
        location ~ /\.ht {
                deny all;
        }   
}

You'll notice these are basically the exact same, save for the server_name and root. Otherwise they are identical. Now here is the situation. domain1.com works fine, no issues. The site for domain2 was tested ONCE by using listen 8000 when the DNS for domain2.com was not changed to the server. When it was tested as IP:8000, it worked fine, so I changed it to the domain, changed the port to 80, and had the DNS changed. The server has since been completely power cycled and nginx and php5-fpm has been restarted probably 100 times.

If I visit domain2.com in the browser, it automatically redirects to domain2.com:8000. If I use web-sniffer.net and look at the HTTP header, it returns a 301 redirect. I never setup a redirect, and there are no 301's setup anywhere on this server. What really bugs me is if I visit www.domain2.com, when I have removed it from nginx configuration file under server_name, I see the default nginx page, which means it is working fine. Once I add the www.domain2.com to the server_name directive in the config, the starts the 301 redirect again.

I have added port_in_redirect off under the http section in my nginx.conf as well, which did not seem to do anything.

Does anyone have any idea what is happening here?

Edit: curl-v http://domain2.com

* About to connect() to domain2.com port 80 (#0)
*   Trying 162.243.XXX.XXX... connected
> GET / HTTP/1.1
> User-Agent: curl/7.22.0 (x86_64-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3
> Host: domain2.com
> Accept: */*
> 
< HTTP/1.1 301 Moved Permanently
< Server: nginx/1.1.19
< Date: Mon, 13 Jan 2014 17:37:07 GMT
< Content-Type: text/html; charset=UTF-8
< Transfer-Encoding: chunked
< Connection: keep-alive
< X-Powered-By: PHP/5.3.10-1ubuntu3.9
< X-Pingback: http://162.243.XXX.XXX:8000/xmlrpc.php
< Location: http://domain2.com:8000/
< 
* Connection #0 to host domain2.com left intact
* Closing connection #0

Best Answer

It is a little hard to guess without seeing the actual output from the server, I'd suggest using curl -v http://example2.com/ to see what is actually returned, but I'd guess your browser has cached the redirect. I've seen that before.

Does using a different browser show the same redirect?

Update: As per your diagnostic output it seems clear this was caused by Wordpress issuing the redirection, rather than nginx.