I brought a domain in godady and I have Installed Passenger + Nginx on a Linux/Unix(Ubuntu) server and deployed a Ruby app. Now my domain looks something like http://example.com when I try to request from a browser. But I want my domain to default redirect to www every time it is requested from a browser(like http://www.example.com.).
example.conf
server {
listen 80;
server_name www.example.com example.com;
# return 301 $scheme://www.example.com$request_uri;
# Tell Nginx and Passenger where your app's 'public' directory is
root /var/www/example/public;
# Turn on Passenger
passenger_enabled on;
passenger_spawn_method direct;
passenger_min_instances 1;
#passenger_pool_idle_time 0;
rails_env development;
passenger_ruby /usr/local/rvm/gems/ruby-2.3.3/wrappers/ruby;
passenger_sticky_sessions on;
}
uncommenting the line
return 301 $scheme://www.example.com$request_uri;
is throwing the error
www.example.com redirected you too many times.
Try clearing your cookies.
ERR_TOO_MANY_REDIRECTS
I am forced to manually type www.example.com in the browser. Instead, how can I redirect to www by default?
Any Help is highly appreciated. Thanks in advance!
You basically need two
server
configuration chunks:www
; to be redirected to base domain onlyHere is an example to get you started:
The easiest way is probably to make use of NGINX
if
statements. Simply put the following in your virtual host configuration:and be sure to replace "example.com" with your domain name.
While this is the simplest way I've found, it's not the best. The recommended route is to create a separate
server
block for non-www, and put thereturn
statement there.