Nginx Mess (URL Redirects) - nginx

I'm looking to redirect old urls to the new urls / new links. It's proving to be a mess to do just that. I'm using nginx 1.19.6 and ubuntu 18.04
I want to setup my redirects so https://ikhnetworks.com/feeds/Live/* would redirect to https://ikhnetworks.com/feeds/HD1/
https://ikhnetworks.com/Stations/* -> redirect to the respective call letters - for example
https://ikhnetworks.com/Stations/Radio/WDJO/ > https://ikhnetworks.com/WDJO/

For the first part, I can provide you the solution as:
server{
listen 443;
server_name ikhnetworks.com;
...
...
root /var/www/html;
location /feeds/Live {
return 301 https://ikhnetworks.com/feeds/HD1/;
}
}

Related

Nginx URL forwarder to another domain

How can I redirect someone from one domain to another whilst keeping the current URL path?
For example:
example.com/ABC -> example2.com/file.html?x=ABC
You can create a rewrite rule to forward from the old domain to the new one.
server {
listen 80;
server_name example.com;
return 301 http://new.example.com$request_uri;
}
Documentation: http://nginx.org/en/docs/http/converting_rewrite_rules.html

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 301 redirection options

I have some 100 or so URLs that need to be permanently redirected from static HTML to dynamic pages served by Wordpress. All examples for Nginx 301 redirection I found suggest that each redirect should be defined as its own server block.
However, I have found out that multiple redirects within one server block work as well such as in this simplified configuration:
server {
listen 80;
server_name www.example.com;
root /var/www/;
location = /subdir/red1.html { return 301 /subdir/?p=1; }
location = /subdir/red2.html { return 301 /subdir/?p=2; }
location = /subdir/red3.html { return 301 /subdir/?p=3; }
}
Curl -I confirms the 301 code. The redirects take place. I prefer this configuration to the one with one server block per redirect because human readability is better. But does this configuration lack something that I'd otherwise have if each redirect was in its own server block?
Well, execrpts you found are probably dealing with http to https redirections where it makes sense not to have anything more in the server block because the vhost is only there to redirect to an other domain. That's completely wrong to pick random examples and take them as a rules of thumb instead of refering to the official documentation.
100 redirects is not a huge count, you could even use permanent rewrites in one unique server block and group them with regexs.
server {
server_name www.example.com;
root /var/www/;
location /subdir/ {
rewrite ^/subdir/red([1-3])\.html /subdir/?p=$1 permanent;
}
}

Resources