NGINX: subfolder to subdomain redirection doesn't work - redirect

yesterday i switched my sites to a new server. now i have a problem, because one domain i now run as a subdomain, was previously accessed as a subfolder and a few sites, which I have no access to, are still using that domain.com/subfolder/?some=parameters
so, long story short:
i need to update my nginx config file, to redirect my users from domain.com/subfolder/?some=parameters to subfolder.domain.com/?some=parameters
i searched for a solution for that problem yesterday, but couldn't fix the problem.
currently i have this in my conf file:
location /subfolder/ {
rewrite ^/subfolder(/.*)$ http://subfolder.domain.com$1 permanent;
}
which obviously redirects all requests from domain.com/subfolder/?some=parameters to subfolder.domain.com/?some=parameters, but requests to domain.com/subfolder/styles.css aren't redirected.

I think this is due to a conflicting location directive for specific static file extensions in your nginx config. I was able to get your rule to work by making it use a regular expression which influences the priority.
location ~ /subfolder/.* {
rewrite ^/subfolder(/.*)$ http://subfolder.domain.com$1 permanent;
}
See the details about the location directive, especially the rules and examples there about precedence.

Related

Ingress session-cookie-path setting regular express in Ingress Session Cookie; resulting in user logout

I have created an ingress controller configuration with following path definitions:
paths:
- path: (USA)/my-app/(.*)$
...............
- path: (UK)/my-app/(.*)$
The problem happening here is when I don't set the following annotation;
nginx.ingress.kubernetes.io/session-cookie-path
I get regular expression in INGRESSCOOKIE path as:
cookie-name: INGRESSCOOKIE --------cookie-path: /(USA)/my-app/(.*)$
This is coming from the given path i.e. /(USA)/my-app/(.*)$.
As a result this response cookie from Ingress doesn't go back to Ingress for any subsequent request for http://USA/my-app/?id=1. (as HTTP request path differs from path in INGRESSCOOKIE)
And because of this HTTP request at times hit a different upstream server and user logs out; as session id in request is generated by a different server managed through the same load balancer.
I then tried setting annotation as:
nginx.ingress.kubernetes.io/session-cookie-path= /$1/my-app
But $1 doesn't actually resolve here; probably we cant give expressions in session-cookie-path.
Is there anything I am not doing in a right way here? Or, I should try something else to achieve session affinity.
Thanks
I know this is pretty old but wanted to share my view anyway.
For your issue, you might want to try the following annotation: nginx.ingress.kubernetes.io/use-regex
Please remove "session-cookie-path" from annotations as Session Cookie Paths doesn't support regex
For more information, please visit below links:
https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/annotations/#use-regex
https://kubernetes.github.io/ingress-nginx/user-guide/ingress-path-matching/
https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/annotations/#cookie-affinity

Netlify redirect to www not working on gatsby site

I'm trying to redirect all urls without www to www, i have the following rules in my netlify.toml file
[[redirects]]
from = "/"
to = "/es"
status = 302
force = false
conditions = {Country = ["es"], Language = ["es","EU-es"]}
[[redirects]]
from = "https://example.com/*"
to = "https://www.example.com/:splat"
status = 301
force = true
The first rule works, but the second rule which is general rule to redirect all requests without www to www is not working. I'm using gatsby v1 with typescript.
Netlify does this redirect automatically if you set your site's primary custom domain to www.example.com and you have a valid SSL certificate for the hostname. In case example.com and www.example.com are secondary hostnames (called domain aliases: https://www.netlify.com/docs/custom-domains/#domain-aliases) then you would likely want/need this redirect.
Usually people put those kinds of redirects above the other ones like your 302/language redirect so that it happens first, but that is probably not the (only) problem with it if it is not working.
Note: That will ONLY catch requests to literally https://example.com(/*) and not requests to http://example.com in case you do not have your site forcing ssl (which should never be the case - Netlify tries to force automatically for all sites).
If you're finding that not to work and you've met the prerequisites:
- netlify site configured with those hostnames
- SSL certificate on the netlify site with support for those hostnames
...then it is possible we either didn't process it correctly and so it is not in place, or something else in the system is wrong and you should contact Netlify's helpdesk for a hand in investigating, since the redirects that were processed are not shown anywhere and trying to understand what was processed by trial and error is not a very awesome process :)

How to get a redirect chain working in Netlify?

I have a _redirects file in my netlify directory structure
/
- site
-- _redirects
_redirects
https://example.netlify.com/ https://www.example.com/:splat 301!
https://www.example.com/post/196 https://www.example.com/comic/post-name
Problem:
The first redirection occurs successfully, but the 2nd one returns:
Page Not found
I have followed the documentation in https://www.netlify.com/docs/redirects/ but cannot find a cause of this issue.
I note 2 potential causes mentioned in the documentation:
You can also add redirect rules to your netlify.toml file.
^^ I have not tried this, but since it reads "also" I assume using _redirects file should be sufficient.
For Jekyll, this requires adding an include parameter to
config.yml.)
^^ I am not using Jekyll as far as I know, but my project does contain a config.yml file.
You will not be able to chain redirects on Netlify from what the docs read.
The redirect engine processes the first matching rule it finds, so more specific rules should be listed before more general ones
https://example.netlify.com/post/196 https://www.example.com/comic/post-name
/post/196 /comic/post-name
https://example.netlify.com/* https://www.example.com/:splat 301!
You can try without the first line above to see if https://example.netlify.com/post/196 redirects to https://www.example.com//comic/post-name. If it does not redirect, then there is no chaining in Netlify redirects.
Solved by adding:
[[redirects]]
from = "https://www.example.me/post/196"
to = "https://www.example.me/comic/post-name"
status = 200
to netlify.toml
source of solution: https://www.netlify.com/docs/redirects/

Gitlab change redirect for nonexistent paths away from login page

Using omnibus gitlab 9.2.
Action: As a non-logged-in user, attempt a request for a public project that doesn't exist (at least not publicly).
Result: Receive a 302 redirect to /users/sign_in from nginx.
What I'd like to see: Receive a 302 redirect to /public (or wherever, for that matter)
What I've tried without success: Adding this to gitlab.rb:
nginx['custom_gitlab_server_config'] = "try_files $uri $uri/ /public;\n\nfastcgi_intercept_errors on;\n\n"
I couldn't find the explicit redirect in any nginx conf, so I guess it's in Rails. I'll peruse that code.
This is actually a custom HA configuration with the gitlab nodes behind haproxy fronts. I thought about possibly doing something on the fronts, but couldn't come up with anything.
Thanks!
Edit:
I see now that replacing the unmatched_route line in routes.rb with:
get '*unmatched_route', to: redirect('/public'), via: :all
does what I need, but I'd of course want to make that change persistent. Is that possible?

How to include redirects on external file?

I need to setup about 5-6k redirects on a domain (for a site migration), and I'm new to nginx. I have some test redirects working in the main .conf file for the domain. But I don't want to have 5k+ rewrites in the main .conf file so I have been told that I can include a external file in the .conf to keep it clean, so my main .conf like this
server {
listen.....etc
etc
rewrite ^oldurl newurl permanent;
rewrite ^oldurl newurl permanent;
include /etc/nginx/conf.d/redirects.conf;
location ....etc
etc
}
Then in the redirects.conf I just have
rewrite ^oldurl newurl permanent;
But when I try to restart nginx I get the error:
"rewrite" directive is not allowed here in /etc/nginx/conf.d/redirects.conf:1
Thanks
OK, issue was I was calling the extenal file redirects.conf, since every file ending in .conf is considered a site configuration file.
I changed it to sitename.redirects and now it works fine