Nginx redirect domain.com/blog/posts to sub.domain.com/blog/posts - redirect

Yet another nginx redirect question.
I've been trying to redirect domain.com/blog/post-1, /blog/post-2 to sub.domain.com/blog/post-1, etc.
Any pointers?

If you don't want to serve anything on domain.com without subdomains, add this block:
server {
server_name domain.com;
return 301 $scheme://sub.domain.com$request_uri;
}
If you want to use it somehow, add this to your domain.com server block:
location /blog {
rewrite ^ http://sub.domain.com$request_uri? permanent;
}
Of course, in any case you want sub.domain.com server block which is catching this request.

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

Nginx is redirecting to www even though I didn't tell it to

I have a node app that is running on port 8989 and it is being port-proxied to 80.
server {
listen 80;
server_name example.com www.example.com;
access_log /var/log/nginx/example.access.log;
location / {
proxy_pass http://127.0.0.1:8989/;
}
}
That works beautifully. But for some reason, the web address automatically goes to www when I type http://example.com into the browser bar. I didn't tell it to do that! haha
I checked the domain settings in my registrar to make sure I didn't stupidly set a www redirect over there. Nothing.
Finally, I looked at the console logs of requests to http://example.com and the response is a 302 moved temporarily. Not sure how that happened, or why.
Where else can I look?
Try rewriting the server name for permanent
server {
server_name www.domain.com;
rewrite ^(.*) http://domain.com$1 permanent;
}
server {
server_name domain.com;
#The rest of your configuration goes here#
}
I would suggest that your 8989 service is issuing the 302 redirect, which is then being relayed by nginx. You should be looking at your 8989 service configuration to determine why it thinks it lives at www.example.com.

Redirect nginx config server_name to custom 404 error page

I'm new to nginx configs and have spent a lot of time googling so far. I'm trying to create a very basic nginx config file to be used in a "redirect" server.
Users will be required to point naked domains (example.com) by A-record to my redirect server IP address, and the 'www' record by CNAME to another server.
The purpose of the redirect server is to then perform a 301 redirect any/wildcard naked domains back to to the 'www' version of the domain so it can be properly handled by my other server.
But I also want to catch any misconfigured 'www' domains that are pointing to my server IP by A-record, and simply direct them to a custom error page on the redirect server with further instructions on how to set up their account correctly for my service.
Here's what I have. It works, but since I am new to writing configs I was wondering if there is a better way to handle the redirect to the custom error page in the first server block. TIA!
#redirect to error page if begins with 'www.'
server {
listen 80;
server_name ~^www.; #only matches if starts with 'www.'. Is this good enough?
rewrite ^(.*)$ /404.html; #is this the correct way to direct to a custom error page?
error_page 404 /404.html;
location = /404.html {
root /usr/share/nginx/html;
}
}
#no match, so redirect to www.example.com
server {
listen 80 default_server;
rewrite ^(.*)$ $scheme://www.$host$1 permanent;
}
Prefix/suffix server name matching is faster and easier than regexp.
Also, there is no reason to use rewrite. You want to return 404, so do it and nginx will do all the rest. BTW, with rewrite you will return 200 OK with content of /404.html instead of 404 Not Found.
So here it is:
server {
listen 80;
server_name www.*;
root /usr/share/nginx/html;
error_page 404 /404.html;
location / {
return 404;
}
location = /404.html {
internal;
}
}

Redirect undefined subdomains to main domain in nginx?

I want to redirect undefined subdomains to my main domain.
So I have a site domain.com and i have 2 subdomains: sub1.domain.com and sub2.domain.com.
And what I want is that when someone tries to go to sub4.domain.com that it mismatches in nginx and redirects it to domian.com. Can this be achieved?
This will redirect any subdomain request.
For all existing subdomains you have to add server entry.
# this will catch all subdomains and redirect to main domain.
server {
server_name *.domain.com;
return 301 http://domain.com$request_uri;
}
# this block will be used for sub1.domain.com;
server {
server_name sub1.domain.com;
...
}
# this is for sub2.domain.com;
server {
server_name sub2.domain.com;
...
}
# and so on...
nginx has no idea of which subdomains exists and which are not. So you have to explicitly name all of them.

NGINX redirect from old to new domain

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.