Question

Still trying to resolve www/non-www issue with my app

I’ve been trying for a while to figure out how to get www.ourapp.com to redirect to ourapp.com. I found this Stackoverflow post, which seems very clear, but so far I haven’t been able to make it work: http://stackoverflow.com/questions/7947030/nginx-no-www-to-www-and-www-to-no-www

It’s a Rails app, and I recently realized there’s an nginx.conf file in the config directory that’s executed as part of the deploy script; previously I had been sshing into the server directory and modifying the file in etc/nginx/sites-enabled, but didn’t realize it was getting written over every time I deployed.

However, even modifying the file there still it hasn’t having any effect. Nginx.conf currently looks like this (as does the version on the server now, obviously):

upstream unicorn {
  server unix:/tmp/unicorn.streamfeed.sock fail_timeout=0;
}

server {
  server_name www.streamfeed.com;
  return 301 http://streamfeed.com$request_uri;
}

server {
  listen 80 default deferred;
  server_name streamfeed.com;
  root /home/deployer/apps/streamfeed/current/public;

  location ^~ /assets/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }

  try_files $uri/index.html $uri @unicorn;
  location @unicorn {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://unicorn;
  }

  error_page 500 502 503 504 /500.html;
  client_max_body_size 4G;
  keepalive_timeout 10;
}

I’ve also tried the version suggested for https in that SO post (which some other people suggest for http as well), using rewrite ^(.*) http://domain.com$1 permanent, and that doesn’t do anything either - I can have two different sessions open on our site at streamfeed.com and www.streamfeed.com. I’ve been banging my head against the wall on this for a long time at this point…obviously i’m missing something, but I don’t know what it is - can anyone help?


Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

Your config looks proper, I’m not sure what’s wrong here. Per Module ngx_http_core_module, listen does not accept a default option, but it does accept default_server, so try replacing listen 80 default deferred; with listen 80 default_server deferred; and then restarting nginx.

Do you have any other server blocks or files in /etc/nginx/sites-enabled?