Nginx redirect from one domain to another? - nginx

I'm on the process of migrating the same app but to a different domain.
For the old domain, I've the routes as:
http://app.example.com/app/users/sign_in?query=hello
I want it to be redirected to another domain omitting the app part as:
http://app.newexample.com/users/sign_in?query=hello
I tried with:
server {
...
location /app {
rewrite ^$scheme://app.sparkon.com/app(/.*)$ $1 last;
}
...
}
I doesn't work. How to achieve this?

I had this issue about a year ago and spent a long time looking for solutions. I found and use this:
server {
listen 80;
server_name example.com app.example.com;
rewrite ^/app(.*)$ http://app.newexample.com$1 permanent;
}
server {
listen 80;
server_name app.newexample.com;
# config for primary domain here
}
From this link. Credit to them.

Don't put scheme in the rewrite pattern:
server {
server_name app.example.com;
location /app/ {
rewrite ^/app/(.*)$ http://app.newexample.com/$1;
}
}
Brg.

I prefer this method as it doesn't need to use rewrite, one of the things that i read are good to avoid too much, cause it needs more processing by the nginx engine.
location ~ /app/(.*) {
return 301 $scheme://app.sparkon.com/$1;
}

Related

nginx if specific subdomain endpoint

I'd like to put robots.txt for the subdomain dev. to point to a specific robots.txt static file.
What routing rule do I need?
This is what i'm thinking:
if ($host = "dev.example.com") {
location ^~ /robots.txt {
allow all;
root /static/disallow/robots.txt;
}
}
Based on Nginx subdomain configuration I believe I may need to just make separate server blocks and use include's. If not a simple routing rule, is the includes method how this is typically done?
If you are wanting a specific robots.txt for each subdomain, you can do so with separate server blocks like this, which you allude (and link) to in your question:
server {
server_name subdomainA.domain.com;
include /common/config/path;
location = /robots.txt {
root /subdomainA/path;
}
}
server {
server_name subdomainB.domain.com;
include /common/config/path;
location = /robots.txt {
root /subdomainB/path;
}
}
Regarding your other approach, have you read If is Evil?

Nginx : detect mobile and forward to desktop

I have a problem i want to detect the domain and the device and forward the response to the desktop, www server section of nginx. With a rewrite this is done but the problem is the url, is cleared and substitute with my url, the configuration is the following :
server {
listen 80 default_server;
#this detects any domain
server_name ~^(www\.)?(?<domain>.+)$;
root /home/someone/main;
index index.html index.htm;
set $mobile_rewrite do_not_perform;
# this regex string is actually much longer to match more mobile devices
if ($http_user_agent ~* '(iPhone|iPod|iPad|Android|BlackBerry|webOS|Windows Phone)') {
set $mobile_rewrite perform;
}
location / {
if ($mobile_rewrite = perform) {
rewrite ^ http://m.someone.domain.com$request_uri? redirect;
break;
}
}
}
I tried a different approach with an alias to the path in so of the m.someone.domain.com code section but this doesn't work inside the if witch is inside the location.
You can try modifying your if block in following way.
location / {
if ($mobile_rewrite = perform) {
return 301 http://m.someone.domain.com$request_uri;
}
}
As you are using rewrite rule as redirect so above may work. Also rewrites are heavier.
location / {
if ($mobile_rewrite = perform) {
rewrite ^http://m.someone.domain.com$request_uri? / permanent;
}
}
Try this it might work for u

If Is Evil - Nginx

I'm using Nginx 1.6.2. I read that if () is evil and it's not good using it so I need a bit help, because I can't do what I want without using if(). I will post the rules I have with if and would ask if somebody could help me and tell me how to not use if () and use something else and get the same result.
# REDIRECT NON-WWW TO WWW.
if ($http_host != "www.site.eu") {
rewrite ^ http://www.site.eu$request_uri permanent;
}
# REMOVE INDEX FILES FROM URL FOR SEO PURPOSE.
if ($request_uri ~ "/index.php") {
rewrite ^ /$1 permanent;
}
# REMOVE ANY MULTIPLE SLASHES IN THE URL.
if ($request_uri ~* "\/\/") {
rewrite ^/(.*) $scheme://$host/$1 permanent;
}
First rule should be replaced with separate server blocks
server {
listen 80 default_server;
return 301 http://www.example.com$request_uri;
}
server {
listen 80;
server_name www.example.com;
# normal config
}
Other ifs usually are not necessary. Just don't generate links with index.php and you will not need to strip it.
In the official wiki introduction it says that there are some cases which are ok. Have a look at this quote:
The only 100% safe things which may be done inside if in location
context are:
return ...; rewrite ... last;
At the end of the introduction there is an example which also features a rewrite command. So your code looks ok, too.
EDIT: You should also have a look at how the if works.
You can replace this block
# REMOVE INDEX FILES FROM URL FOR SEO PURPOSE.
if ($request_uri ~ "/index.php") {
rewrite ^ /$1 permanent;
}
with this
location ~ ^/index.php/(.*[^/])$ { return 301 $scheme://$host/$1/$is_args$args; }
location ~ ^/index.php/(.*)/$ { return 301 $scheme://$host/$1/$is_args$args; }
I also don't think you need to worry about the last rule for double // because nginx by default automatically takes care of that before it even gets to the point of matching location blocks

Multiple domains - subdomains routed to main domain

I have multiple domains connected to the same DO droplet, with nginx
Let's assume:
firstdomain.com
seconddomain.com
I would like to set my nginx in a way that, every subdomain will be directed to it's main domain, and it will be reflected in address bar too:
subdomain.firstdomain.com ----> firstdomain.com
asldk.firstdomain.com --------> firstdomain.com
test.seconddomain.com --------> seconddomain.com
and so on.
What is the simplest way to achieve this?
server {
listen 80;
server_name ~^(?<subdomains>.+\.)?(?<domain>[^.]+\.[^.]+)$;
if ($subdomains != "") {
rewrite ^/(.*)$ http://$domain/$1;
}
}
I want to post this answer, for future visitors and for myself (I'm sure I will forget)
server {
listen 80 default_server;
server_name ~^(?<subdomains>.+\.)?(?<domain>[^.]+\.[^.]+)$;
if ($subdomains != "") {
rewrite ^/(.*)$ http://$domain/$1;
}
index index.html;
root /etc/nginx/conf.d/404;
}
It is almost identical with the solution I accepted, and it has a defined 404 page in conf.d to modify default nginx page.

nginx config - pass request from subdomain to path with argument, regex in location

I have a server view.example.com, and I have to take a request like this: view.example.com/1234 - domain + 4 numbers/letters.
What i want to do is proxy_pass the request to my local service at: 192.168.33.10/view/1234
How can I write the nginx config to:
care only about requests which match the regex (4 numbers/letters)
pass the request along to my service.
So far I have:
server {
server_name view.example.com;
listen 80;
location ~ '^/(?<hash>[a-zA-Z0-9\-_]{4})/?$' {
# rewrite - which I should probably use but not sure how
proxy_pass http://192.168.33.10/view/;
}
}
Arek
just checked this one:
server {
server_name view.example.com;
listen 80;
location ~ '^/(?<hash>[a-zA-Z0-9\-_]{4})/?$' {
rewrite ^ /view/$hash break;
proxy_pass http://192.168.33.10;
}
}
and it seems to do the trick - any better solutions ?
Arek

Resources