Here's my NGINX config (no apache, just php-fpm):

user  nginx;
worker_processes  1;
error_log /usr/local/nginx/logs/error.log notice;
pid       /var/run/nginx.pid;

events  {
        worker_connections      384;

http    {
        include          mime.types;
        default_type  application/octet-stream;
        access_log          off;
    server_tokens       off;
        sendfile                 on;
        tcp_nopush           on;
    tcp_nodelay             off;
    client_max_body_size     8M;
    client_body_timeout      30;
    client_header_timeout    15;
    keepalive_timeout     15 65;
    send_timeout             30;

gzip on;
    gzip_static on;
    gzip_disable "msie6";
    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 9;
    gzip_buffers 32 4k;
    gzip_http_version 1.0;
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

    upstream php-fpm-sock {
    server unix:/var/run/php-fpm.sock;

server  {
        listen          80;
        server_name     example.com;
        index           index.php index.html;
        root            /usr/local/nginx/html;
    error_page      404 index.php;

        if ($request_method !~ ^(GET|HEAD|POST)$ ) {
        return 444;

        location ~* \.(?:jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js)$ {
        expires 1y;
        log_not_found off;

        location / {
    try_files $uri $uri/ /index.php?q=$uri;

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

        location ~ \.php$ {
         fastcgi_index  index.php;
                try_files $uri =404;
         fastcgi_pass   php-fpm-sock;
             fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name;
             include        fastcgi_params;
         fastcgi_connect_timeout    15;
         fastcgi_send_timeout   30;
         fastcgi_read_timeout   15;
         fastcgi_buffer_size    8k;
         fastcgi_buffers         32 8k;

I thought maybe it was godaddy doing some sort of forwarding but I moved DNS from Godaddy to AWS Route 53 and STILL if I type example.com it forwarded to 301's to www.example.com.

My Route 53 dns:

mywebsite.com   3600    A   107.22.210.xxx

*.mywebsite.com 3600    CNAME   ec2-107-22-210-xxx.compute-1.amazonaws.com

What causing this redirect?


  • 1
    Please provide the actual domain you're working with, so people can test it themselves. Obfuscation is frustrating.
    – womble
    Mar 18 '12 at 1:50

nginx isn't redirecting - especially not sending the 301 response code that you're seeing. The code that's running in PHP is almost certainly the culprit.

Please provide information about what's running in PHP code - nginx is not the problem.

  • THANK YOU! Ok I guess my focus was on nginx too much. Its a setting in the PHP script's config. I changed to to mysite.com and it removes the www and reverted and it adds the www.mysite.com. I was looking into using nginx to do the 301 but seems the script is sending 301 anyway. Plus, when I compiled nginx I did not enable rewrite so that's why I thought maybe it was some sort of bug lol
    – Hayden
    Mar 18 '12 at 9:09

I have noticed that in most web browsers if they can't make a connection using domain.com, then they will automatically (and near instantaneously) jump to www.domain.com. Make sure your server is actually answering the request for domain.com.

Do the following to test, and please leave a comment with results (this assumes the use of a Linux workstation, maybe someone else can post a Windows/Mac version):

Make sure the domain resolves when used by itself:

nslookup domain.com

Make sure the web server is actually listening:

telnet domain.com 80

See where the web server sends us when requesting the index file:

wget domain.com

Good luck!

  • According to redbot.org mysite.com response headers are: HTTP/1.1 301 Moved Permanently Server: nginx Date: Sun, 18 Mar 2012 05:25:06 GMT Content-Type: text/html;charset=UTF-8 Transfer-Encoding: chunked Connection: keep-alive Keep-Alive: timeout=65 Set-Cookie: session_id=xxxxxxxxxxxxxxx; path=/; domain=www.mysite.com; httponly Location: mysite.com
    – Hayden
    Mar 18 '12 at 5:27
  • nslookup mysite.com Server: 172.16.0.xxx Address: 172.16.0.xxx#53 Non-authoritative answer: Name: mysite.com Address: 107.22.210.xxx
    – Hayden
    Mar 18 '12 at 5:30
  • WGET = Resolving mysite.com... 107.22.210.xxx Connecting to mysite.com|107.22.210.xxx|:80... connected. HTTP request sent, awaiting response... 301 Moved Permanently Cookie coming from mysite.com attempted to set domain to mysite.com Location: mysite.com [following] --2012-03-18 05:30:55-- mysite.com Resolving www.mysite.com... 10.194.5.xxx Connecting to www.mysite.com|10.194.5.xxx|:80... connected. HTTP request sent, awaiting response... 200 OK Length: unspecified [text/html] Saving to: “index.html”[ <=>] 97,543 --.-K/sin 0.008s (11.9 MB/s) - “index.html” saved [97543
    – Hayden
    Mar 18 '12 at 5:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.