Nginx 301 redirection options - nginx

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

Related

Nginx Mess (URL Redirects)

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

nginx - proxy_pass with return 301 redirect

I am building my nginx configuration with automated tools like nginx-proxy on docker. There they let me add a custom line inside the location directive.
Simply, I want www.example.com to be 301 redirected to example.com, or generally both http://www.example.com and https://www.example.com should be 301 redirected to https://example.com.
The automated configuration creation results as such:
server {
...
server_name www.example.com;
listen 443 ssl http2 ;
location / {
proxy_pass http://<upstream>;
return 301 $scheme://example.com;
}
}
I notice that there are proxy_pass syntax before return 301 ..., and since its creation is automated, I don't think I can easily modify that (i.e. to make return 301 appear before the proxy_pass syntax.
From nginx documentation:
proxy_pass
Sets the protocol and address of a proxied server and an optional URI to which a location should be mapped.
So, nginx-wise question, since it said "sets", will the 301 redirect be done correcly, even if the redirect came after the proxy_pass syntax ?
From the comment by RichardSmith, it is said that the return syntax will be evaluated first and hence proxy_pass is completely ignored.

Want to redirect the server url to server/somestring through nginx

want to redirect the server url to server url/somestring through nginx.conf.
Tried location /somestring{} inside server tag in nginx.conf, didn't work.
Please suggest how can i achieve that.
An example of nginx config that allows to redirect multiple domains.
You can use a similar approach to redirect paths as well.
map $http_host $redirect_destination {
hostnames;
default where-to-go-by-default;
my-domain where-to-redirect-my-domain;
}
server {
listen 80;
location / {
return 301 https://$redirect_destination/some-path;
}
}

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 subdomains with www

I have Nginx configured in a Amazon EC2 server.
Right now both www.myserver.com, .myserver.com works perfect.
What I need, is configure www..myserver.com. I need to redirect the user to .myserver.com. I mean, I need to rewrite the url or something.
How can I do that?
server {
listen 80;
server_name www.myserver.com;
return 301 http://myserver.com$request_uri;
}
You don't need "rewrites" in nginx. Leave them to apache, and read the docs:
http://nginx.org/en/docs/http/server_names.html
http://nginx.org/en/docs/http/request_processing.html
http://nginx.org/en/docs/http/converting_rewrite_rules.html
Upd:
That is not clear from your question (who knows, what do you mean by double dots). But if you have ever tried to read the docs, you would easily modify the example for your needs:
server {
listen 80;
server_name ~^www\.([^.]+\.myserver\.com)$;
return 301 http://$1$request_uri;
}

Resources