unable to redirect using nginx to another domain - redirect

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.

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.

Ispconfig nginx redirecting to https

I've deployed a CentOS server with ISPConfig and Nginx.
Also I was able to configure Nginx, manually (by editing /etc/nginx/sites-available/mysite.com.vhost), to redirect http requests to https:
server {
listen 80;
server_name mysite.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
..
}
As I edited the file manually, every time I change a setting with ISPConfig, my vhost file gets overwritten and I lose my redirection's trick.
Do you know a way to config the redirection above using ISPConfig panel instead editing the nginx file manually ?
Thanks in avance.
I've configured the redirection as the accepted answer suggested.
Also I've included an external config so I put all our manual configurations.
In our version of ISPConfig, I did this:
if ($scheme != "https") {
rewrite ^ https://$http_host$request_uri? permanent;
}
include /etc/nginx/sites-available/my-own-config.conf;
That way, ISPConfig won't break our configs.
On the more recent versions of ISPConfig, you can simply select the website to use SSL (and that means HTTPS, and optionally, SPDY or HTTP/2), with an additional checkbox to redirect all HTTP requests permanently to HTTPS, and ISPConfig will automatically generate the vhosts files correctly.
For the sake of completude, this is what ISPConfig adds:
server {
listen *:80;
listen *:443 ssl;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_certificate /var/www/clients/clientX/webY/ssl/your.web.site.crt;
ssl_certificate_key /var/www/clients/clientX/webY/ssl/your.web.site.key;
server_name your.web.site www.your.web.site;
root /var/www/your.web.site/web/;
if ($http_host = "www.your.web.site") {
rewrite ^ $scheme://your.web.site$request_uri? permanent;
}
if ($scheme != "https") {
rewrite ^ https://$http_host$request_uri? permanent;
}
index index.html index.htm index.php index.cgi index.pl index.xhtml;

Nginx: Redirect both http://example.com and http://*.example.com to https://example.com

I only have an SSL certificate for example.com and want to redirect both http://example.com and http://*.example.com to https://example.com using nginx. I'm aware of it being impossible to redirect subdomains via SSL without a certificate including all the subdomains, but at least, I should be able to redirect users who are typing www.example.com (port 80) to the SSL homepage.
My current nginx config starts as follows:
server {
# This should catch all non-HTTPS requests to example.com and *.example.com
listen 80;
server_name example.com *.example.com;
access_log off;
return 301 https://example.com$request_uri;
}
server {
listen 443 ssl;
# Actual server config starts here...
Requesting http://example.com will be redirected properly to https://example.com, whereas http://www.example.com leads to https://www.example.com (and of course, the browser is showing a certificate error). I think it has something to do with the processing order of the server_name values, but I haven't found any information about how to enforce a certain order.
Try to add another server {}
server {
listen 80;
server_name www.example.com
access_log off;
return 301 https://example.com$request_uri;
}
Try:
server_name example.com www.example.com *.example.com;
Taken directly from the Nginx docs.

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;
}
}