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

December 24, 2014 932 views

I've been trying for a while to figure out how to get to redirect to I found this Stackoverflow post, which seems very clear, but so far I haven't been able to make it work:

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 {
  return 301$request_uri;

server {
  listen 80 default deferred;
  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 ^(.*)$1 permanent, and that doesn't do anything either - I can have two different sessions open on our site at and 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?

1 Answer

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?

  • There are no other files or server blocks in the sites-enabled directory. There is the default file in the sites-available directory, but I've made the same changes there.

    I tried making your suggested change and it's still not redirecting...the command to restart nginx is sudo service nginx restart, right?

    Thanks for your help...

Have another answer? Share your knowledge.