NGINX : redirecting non-www to www - nginx

Could be the stupid one and could be most duplicate question ever asked.
But yes, I can't get this done.
I've one domain which is : example.co.in
I want to redirect to www.example.co.in
Server is : NGINX
My vHost config is:
server {
listen 80;
server_name example.co.in www.example.co.in;
return 301 http://www.example.co.in$request_uri;
}
server {
listen 80;
server_name example.co.in www.example.co.in;
access_log /var/www/example.co.in/public_html/logs/access.log;
error_log /var/www/example.co.in/public_html/logs/error.log;
....
}
I've a A record entry for www in my dns server.
Edit:
I'm getting ERR_TOO_MANY_REDIRECTS error
Any help would be help full.

You have to server blocks with the same port and server_names. It hits the first one and tells the requester to redirect to www which the requester makes another request to the same server block which starts the process over again.
If you want it to redirect to www then take www out of the first server block then reload or restart nginx.

Related

Nginx redirect not applied

I am configuring an nginx server at the moment and I have two domain types:
www.example.com
example.com
On the server I added a new server block with the following code to redirect every incoming requests to the domain without www.
server {
server_name www.example.com;
return 301 $scheme://example.com$request_uri;
}
The problem is that I only got HTTP 200 instead of 301.
The changed file is: /etc/nginx/sites-enabled/default
What am I missing? I did exactly how the documentation says in Digital Ocean.

Redirect root to another folder in nginx server

I am very new to nginx. I was using Apache previously and was using htaccess to redirect root to another folder. Now migrated to nginx. Here is four things I want to achieve
Redirect http to https e.g. http://www.example.com -> https://www.example.com
Then redirect root to another folder but URL must be rewritten as example.com not example.com/blog
All files in php should show as html in url e.g. example.com/contact.php -> example.com/contact.html
example.com/page.php?content=file -> example.com/file
I found this code to redirect but don't know where to insert this code nginx.conf or any other file?
server{
location = / {
return 301 https://www.example.com/blog;
}
}
Also please suggest me if these changes are made in nginx.conf file or /etc/nginx/sites-available/example.com file.
To redirect HTTP to HTTPS traffic you can create another server block to match the incoming HTTP and domain then rewrite to your HTTPS server block.
Which file do you put this in? Both /etc/nginx/nginx.conf and /etc/nginx/sites-available/example.com should get read (unless you changed config) so it shouldn't matter, but I personally put these configs in /etc/nginx/sites-available/example.com because I consider it part of the same domain.
file: /etc/nginx/sites-available/example.com
server {
listen 80;
server_name www.example.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name www.example.com;
...
# your location blocks and redirects here
}

How to Redirect a non-www to www with Mutiple Sites? (Nginx and Varnish)

First I want to clarify that I searched a lot about this issue, but I haven't found a solution.
I have 2 Wordpress sites configured under the same Nginx (port 8080) and Varnish (port 80).
This is my actual setting:
map $http_host $blogid {
default 0;
www.site1.com 1;
www.site2.com 2;
}
server {
listen 8080;
server_name www.site1.com site1.com;
root /var/www/site1.com;
...
}
server {
listen 8080;
server_name www.site2.com site2.com;
root /var/www/site2.com;
...
}
What I want to do, is to configure a redirect for site2 from a non-www to a www. Example: http://site2.com -> http://www.site2.com
I added another 'server' configuration with the redirect, and let just 'www.site2.com' in the other 'server_name'.
map $http_host $blogid {
default 0;
www.site1.com 1;
www.site2.com 2;
}
server {
server_name www.site1.com site1.com;
root /var/www/site1.com;
...
}
server {
listen 8080;
server_name site2.com;
return 301 http://www.site2.com$request_uri;
}
server {
server_name www.site2.com;
root /var/www/site2.com;
...
}
After changed with the configuration above and restarting Nginx, what happened is when accessing "http://site2.com" (without www) it is loading the content from "http://site1.com" (url continues site2.com without www). Acessing "http://www.site2.com" shows the right content.
What I'm doing wrong?
I think that redirect is not working because I tried to redirect to Google.com, but it didn't redirect. It keeps loading "site1.com" content inside "site2.com".
return 301 http://www.google.com
I tried this code below, but with the same result:
rewrite ^(.*) http://www.site2.com$1 permanent;
My complete Nginx configuration:
http://codepad.org/TfPHS0jH
Your site isn't being served by Nginx but by Varnish.
When you navigate to any of http://www.siteX.com or http://siteX.com, you are going to http://www.siteX.com:80 or http://siteX.com:80 and according to your explanation, Varnish listens on Port 80, not Nginx.
There is no real reason to ever have to use the two together (despite what you may have read on some websites) and you should get rid or one or the other and focus on the one you decide to keep.
After long time, this was my solution:
http://www.softprayog.in/troubleshooting/how-to-redirect-non-www-urls-to-www-in-varnish

nginx rewrite for subsubdomains to subdomains

We have an application where we use subdomains for each of our customer's installations. so we have customer1.ourapp.com, customer2.ourapp.com, customer3.ourapp.com and so on.
Because of security we want to redirect all http to https, since we have a wildcard SSL certificate.
Also, some customers are not that tech savvy and add www to their domain name, so then you get things like: http://www.customer1.ourapp.com or https://www.customer1.ourapp.com. In those cases the SSL certificate isn't valid because of the subsubdomain.
I'm trying to write the vhost config for nginx to make the correct redirect in these two cases. I got the http to https redirect to work with:
server {
listen 80;
server_name *.ourapp.com;
#Rewrite all nonssl requests to ssl.
return 301 https://$host$request_uri$is_args$args;
}
correct url's use:
server {
listen 443;
server_name *.ourapp.com;
#Rest of config
}
Made an attempt for the subsub domains, but it's not matching:
server {
server_name "~^(.*)\.(.*)\.ourapp\.com$";
return 301 https://$2.ourapp.com$request_uri;
}
Any idea how to get this working?
Wildcarded server takes precedence over regexp'ed one and matches 'www...' too.
You can use one definition for both cases:
server_name ~ ^(?:.*\.)?(.*)\.ourapp\.com$;
return 301 https://$1.ourapp.com$request_uri;

NGINX - redirect from https to http without ssl certificate

I'm setting up a new nginx box however I can't figure out why my nginx isn't starting when I use this:
server {
listen 443;
server_name mysite.com; // https://mysite.com
rewrite ^(.*) https://mynewsite.com.com$1 permanent; // new site
}
Anyone help me out?
This is impossible to do. Redirecting from https to http itself is based on a successful https connection, which requires a certificate.
if you have an SSL either purchased one or self signed SSL, you can then redirect the https to http. yes, you can redirect https to http without SSL if someone try adding the s letter in your url so that your url can't serve anything over HTTPS, but only HTTP
server {
listen 443;
server_name yourdomain.com;
return 301 http://www.yourdomain.com;
}
and you need to have another server block to complete the www servingon your url.
server {
listen 443;
server_name www.yourdomain.com;
// the rest of your configs //
}
I hope that helps.

Resources