Redirect to 'www' prefix domain using nginx
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
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!
server dns nginx ruby
add a comment |
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!
server dns nginx ruby
add a comment |
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!
server dns nginx ruby
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!
server dns nginx ruby
server dns nginx ruby
asked Apr 4 at 5:36
current_usercurrent_user
1126
1126
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
You basically need two server
configuration chunks:
- One for
www
; to be redirected to base domain only - One for the base domain
Here is an example to get you started:
server {
listen 80;
server_name www.example.com;
location / {
# Redirection happens here
return 301 http://example.com$request_uri;
}
}
server {
listen 80 default_server;
server_name example.com;
# Other conf directives go here
}
add a comment |
The easiest way is probably to make use of NGINX if
statements. Simply put the following in your virtual host configuration:
if ($host = "example.com") {
return 301 $scheme://www.$host$request_uri;
}
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 the return
statement there.
1
if
does some nasty things, and this will actually break HTTPS configurations too as it'll end up in an infinite redirection loop even for HTTPS requests. You should always use two individual server blocks; one for non-SSL and one for SSL. Or, one server block for one set of server names, and then test the scheme, but if is still odd.
– Thomas Ward♦
Apr 8 at 13:32
1
According to If Is Evil,return
andrewrite
can be safely used. I use this setup for a few of my sites, and it causes no errors and works as expected.
– NerdOfLinux
Apr 8 at 13:49
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1131082%2fredirect-to-www-prefix-domain-using-nginx%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You basically need two server
configuration chunks:
- One for
www
; to be redirected to base domain only - One for the base domain
Here is an example to get you started:
server {
listen 80;
server_name www.example.com;
location / {
# Redirection happens here
return 301 http://example.com$request_uri;
}
}
server {
listen 80 default_server;
server_name example.com;
# Other conf directives go here
}
add a comment |
You basically need two server
configuration chunks:
- One for
www
; to be redirected to base domain only - One for the base domain
Here is an example to get you started:
server {
listen 80;
server_name www.example.com;
location / {
# Redirection happens here
return 301 http://example.com$request_uri;
}
}
server {
listen 80 default_server;
server_name example.com;
# Other conf directives go here
}
add a comment |
You basically need two server
configuration chunks:
- One for
www
; to be redirected to base domain only - One for the base domain
Here is an example to get you started:
server {
listen 80;
server_name www.example.com;
location / {
# Redirection happens here
return 301 http://example.com$request_uri;
}
}
server {
listen 80 default_server;
server_name example.com;
# Other conf directives go here
}
You basically need two server
configuration chunks:
- One for
www
; to be redirected to base domain only - One for the base domain
Here is an example to get you started:
server {
listen 80;
server_name www.example.com;
location / {
# Redirection happens here
return 301 http://example.com$request_uri;
}
}
server {
listen 80 default_server;
server_name example.com;
# Other conf directives go here
}
answered Apr 4 at 5:53
heemaylheemayl
68.4k11145216
68.4k11145216
add a comment |
add a comment |
The easiest way is probably to make use of NGINX if
statements. Simply put the following in your virtual host configuration:
if ($host = "example.com") {
return 301 $scheme://www.$host$request_uri;
}
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 the return
statement there.
1
if
does some nasty things, and this will actually break HTTPS configurations too as it'll end up in an infinite redirection loop even for HTTPS requests. You should always use two individual server blocks; one for non-SSL and one for SSL. Or, one server block for one set of server names, and then test the scheme, but if is still odd.
– Thomas Ward♦
Apr 8 at 13:32
1
According to If Is Evil,return
andrewrite
can be safely used. I use this setup for a few of my sites, and it causes no errors and works as expected.
– NerdOfLinux
Apr 8 at 13:49
add a comment |
The easiest way is probably to make use of NGINX if
statements. Simply put the following in your virtual host configuration:
if ($host = "example.com") {
return 301 $scheme://www.$host$request_uri;
}
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 the return
statement there.
1
if
does some nasty things, and this will actually break HTTPS configurations too as it'll end up in an infinite redirection loop even for HTTPS requests. You should always use two individual server blocks; one for non-SSL and one for SSL. Or, one server block for one set of server names, and then test the scheme, but if is still odd.
– Thomas Ward♦
Apr 8 at 13:32
1
According to If Is Evil,return
andrewrite
can be safely used. I use this setup for a few of my sites, and it causes no errors and works as expected.
– NerdOfLinux
Apr 8 at 13:49
add a comment |
The easiest way is probably to make use of NGINX if
statements. Simply put the following in your virtual host configuration:
if ($host = "example.com") {
return 301 $scheme://www.$host$request_uri;
}
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 the return
statement there.
The easiest way is probably to make use of NGINX if
statements. Simply put the following in your virtual host configuration:
if ($host = "example.com") {
return 301 $scheme://www.$host$request_uri;
}
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 the return
statement there.
edited Apr 8 at 13:52
answered Apr 8 at 13:30
NerdOfLinuxNerdOfLinux
1,74311041
1,74311041
1
if
does some nasty things, and this will actually break HTTPS configurations too as it'll end up in an infinite redirection loop even for HTTPS requests. You should always use two individual server blocks; one for non-SSL and one for SSL. Or, one server block for one set of server names, and then test the scheme, but if is still odd.
– Thomas Ward♦
Apr 8 at 13:32
1
According to If Is Evil,return
andrewrite
can be safely used. I use this setup for a few of my sites, and it causes no errors and works as expected.
– NerdOfLinux
Apr 8 at 13:49
add a comment |
1
if
does some nasty things, and this will actually break HTTPS configurations too as it'll end up in an infinite redirection loop even for HTTPS requests. You should always use two individual server blocks; one for non-SSL and one for SSL. Or, one server block for one set of server names, and then test the scheme, but if is still odd.
– Thomas Ward♦
Apr 8 at 13:32
1
According to If Is Evil,return
andrewrite
can be safely used. I use this setup for a few of my sites, and it causes no errors and works as expected.
– NerdOfLinux
Apr 8 at 13:49
1
1
if
does some nasty things, and this will actually break HTTPS configurations too as it'll end up in an infinite redirection loop even for HTTPS requests. You should always use two individual server blocks; one for non-SSL and one for SSL. Or, one server block for one set of server names, and then test the scheme, but if is still odd.– Thomas Ward♦
Apr 8 at 13:32
if
does some nasty things, and this will actually break HTTPS configurations too as it'll end up in an infinite redirection loop even for HTTPS requests. You should always use two individual server blocks; one for non-SSL and one for SSL. Or, one server block for one set of server names, and then test the scheme, but if is still odd.– Thomas Ward♦
Apr 8 at 13:32
1
1
According to If Is Evil,
return
and rewrite
can be safely used. I use this setup for a few of my sites, and it causes no errors and works as expected.– NerdOfLinux
Apr 8 at 13:49
According to If Is Evil,
return
and rewrite
can be safely used. I use this setup for a few of my sites, and it causes no errors and works as expected.– NerdOfLinux
Apr 8 at 13:49
add a comment |
Thanks for contributing an answer to Ask Ubuntu!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1131082%2fredirect-to-www-prefix-domain-using-nginx%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown