NGINX redirect from old to new domain - redirect

I need to redirect requests like http://domain.com/?part=word to http://another_domain/?part=word, 'word' may be different
My nginx configuration:
server {
listen 80;
server_name domain.com www.domain.com;
rewrite ^/?part=(.*)$ http://another_domain/?part=$1 permanent;
}
redirect does not work, what am I doing wrong?

instead of specifying what words you want to handle, you can just tell nginx to append all the args
server {
listen 80;
server_name old.example.com;
return 301 http://new.example.com$request_uri;
}
Update:
I've recently found out that $request_uri already contains the query string, and thus the extra part that I've had ($is_args$query_string) would not be necessary. I've updated the above part and removed the extra query string.

Related

Nginx 301 Redirect http (naked and www) to https www, plus wildcard subdomain to https

I'm trying to set up an Nginx proxy server for a multi-tenant Saas with lots of custom domain names. What I want to do is create a server block that can handle the following requests, all as 301 permanent:
http://custom-domain.com to https://www.custom-domain.com (custom-domain.com could be any user-set domain name)
http://www.custom-domain.com to https://www.custom-domain.com (again, any domain name)
http://.saas-domain.com to https://.saas-domain.com (saas-domain,com is a single domain name for my service)
I am currently handling this with a few If statements, but it looks hacky and I am hoping for some help with a more efficient way:
server {
listen 80 default_server;
location / {
# if 'www' redirect to https
if ($host ~* ^(www)) {
return 301 https://$host$request_uri;
}
# if '*.saas-domain.com' redirect to https://*.saas-domain.com
if ($host ~* ^(.*)\.saas-domain\.com) {
return 301 https://$host$request_uri;
}
# if not 'www' redirect to https and add 'www'
if ($host !~* ^(www)) {
return 301 https://www.$host$1 permanent;
}
}
}
Is this the best way to handle all of my scenarios? I think the complication is the wildcard custom domains. I'm concerned with the If statement's overhead. TIA!
Nginx recommend not to use "If" statements unless you have no other way to solve your issue. I would suggest to add separate blocks for your domain names as this will give you more flexibility.
Try the following to see if it helps.
# Capture requests that already have www and redirect to https
server {
listen 80;
server_name www.*;
return 301 https://$server_name$request_uri;
}
# Captures the saas-domain.com requests and redirects them
server {
listen 80 ;
server_name *.saas-domain.com;
return 301 https://$server_name$request_uri;
}
# Default capture everything else and redirect to https://www.
server {
listen 80 default_server;
server_name _;
return 301 https://www.$host$request_uri;
}
Test this first before implementing it in production.
Nginx Server names
Nginx if is evil
Nginx variables

How can I redirect non-www to www.domain.com and along with that also keep the m.domain.com / subdomain.domain.com same as it is in nginx?

I used this:
server {
server_name "~^(?!www.).*" ;
return 301 $scheme://www.$host$request_uri;
}
but this redirects everything. I need to write exceptions for subdomains along with this.
If the server block containing server_name "~^(?!www.).*" is matching m.domain.com, you obviously do not have another server block with a server_name m.domain.com.
Rather than use complex regular expressions to redirect website names to a default server block, you could use the default server block to perform the redirect instead.
For example:
server {
listen 80 default_server;
return 301 http://www.domain.com/$request_uri;
}
server {
listen 80;
server_name www.domain.com m.domain.com subdomain.domain.com;
...
}
See this document for more details.

Is it feasible to redirect all possible similar domain to one domain in Nginx

I have below configuration but it only redirects my specified urls to target.fi,I want to make it more general and for any possible domain that contain "target" it will be redirected to my domain.
server {
listen 80;
server_name abctarget.fi bctarget.fi targetbcd.fi bc.target.fi;
return 301 $scheme://target.fi;
}
How to make it like server_name *target*.fi;
server {
listen 80;
server_name ?;
return 301 $scheme://target.fi;
}
You could use a wildcard name. See this documentation.

Why is Every Request Matching my nginx Redirect?

I'm having some problems with my nginx settings.
I have one server that should be serving two domains. One domain should just be performing a redirect. One domain should be performing a redirect for www urls, and then serving up my uwsgi application.
For some reason, everything matches the first thing it finds. Which is odd.
Here's the .conf file
server {
listen 80;
server_name russellrollins.com;
return 301 $scheme://blog.russellrollins.com$request_uri;
}
server {
listen 80;
server_name www.russellrollins.com;
return 301 $scheme://blog.russellrollins.com$request_uri;
}
server {
listen 80;
server_name www.playbeercan.com;
return 301 $scheme://playbeercan.com$request_uri;
}
server {
listen 80;
server_name playbeercan;
charset utf-8;
client_max_body_size 75M;
location / { try_files $uri #playbeercan; }
location #playbeercan {
include uwsgi_params;
uwsgi_pass unix:/var/www/playbeercan/playbeercan_uwsgi.sock;
}
}
Instead of sending:
russellrollins.com -> blog.russellrollins.com
www.russellrollins.com -> blog.russellrollins.com
www.playbeercan.com -> playbeercan.com
playbeercan.com -> uwsgi app
It sends everything I try to blog.russellrollins.com. Everything seems to match the first server name.
I even tried removing all the redirects except the first one, and it's still matching everything.
The only setting in the nginx.conf file I've changed is the server_names_hash_bucket_size. Which seems unrelated.
You need to have a server_name directive to handle both blog.russellrollins.com and playbeercan.com, otherwise it would take the first server_name as default, or you could use default_server to set the default.

unable to redirect using nginx to another domain

Hey we changed the domain name from domain1.ourapp.com to domain2.ourapp.com
I would like to redirect requests to domain1.ourapp.com to domain2.ourapp.com using nginx conf. I want the browser url also to change.
In the nginx conf I have the following
server {
listen 80;
rewrite ^ https://$host$request_uri? permanent;
}
server {
server_name domain1.ourapp.com;
rewrite ^ $scheme://domain2.ourapp.com$request_uri permanent;
}
server {
listen 443 ssl;
server_name domain2.ourapp.com
# rest of the stuff
}
The trouble is that urls with domain1.ourapp.com return the right response but there is browser redirection happening. I would like some help in this regard.