Rewrite a url and rerun the server block - nginx

I'm trying to rewrite a url like the following but without sending a 301 to the client (change the url and then restart processing of the server block with the new url).
rewrite ^/a/b/$ /a/c/d permanent;
I would normally just reorder the lines but it's a really convoluted config and also really large.

permanent always means redirect, replace it with last
check the last flag in the reference page

Related

NGINX: redirect request_uri that contains http

I have rewritten all external links sent to standard output on the fly as an internal link in PHP from https://externalsite.com to https://myexamplesite.com/https://externalsite.com, yet I want Nginx to handle the request for this URL and not PHP.
I have tried it via the line below, but it is not working
rewrite ^/http(.+)$ http$1 redirect;
Please advise on the best way to redirect https://myexamplesite.com/https://externalsite.com, to https://externalsite.com on request
I was able to have it work with this.
rewrite ^/https?\W+(.*) https://$1 redirect;
I am not sure why the former did not work but seems NGINX is passing it back internally.
NGINX seems not to honor an external redirect without the "https://" statically written before and jointly with the second option of the directive despite being captured in the parenthesis.

nginx redirect to hashbang URL without URL encoding

I have a rewrite rule that looks like the following
rewrite ^/([a-zA-Z0-9_]+)$ /mysite/#!/$1/login;
The idea is that a shortcode like
/foo
gets redirected to
/mysite/#!/foo/login
However nginx is redirecting to:
/mysite/%23!/foo/login
How do I prevent the URL encoding from happening in the rewrite?
I can reproduce this issue by using a reverse proxy.
Nginx is actually doing the right thing, as # is a reserved character for URIs and identifies the start of the fragment identifier.
The fragment identifier is for the browser's use only and is not usually received by the server in the requested URL. I am not sure how your Tomcat server is receiving requests containing a naked # in the first place.

Is it possible to rewrite to a URL with a fragment in it in nginx?

E.g.
rewrite ^/page /#page;
I'm not sure if this would even be possible, due to the nature of fragments (never sent to the server etc). But since I'm rewriting TO one, rather than FROM one, I think it should work?
In which case how do I encode/escape that hash sign so that it doesn't start a comment..?
Thanks!
As you know, the fragment is used by the browser and is not sent to the server.
But you can use nginx to rewrite a request into a new URI containing a fragment, but it only makes sense if the new URI is sent to the browser, i.e by using an HTTP 3xx response.
The rewrite directive will generate an HTTP 3xx response when the redirect (302) or permanent (301) flag is provided (or the replacement string starts with a scheme - see this document for more).
For example:
rewrite ^/page /#page redirect;
Adding quotation marks, could help. I tried in Docker Nginx latest image (1.17.4)
rewrite ^/page "/#page" redirect;

nginx rewrite Issue, why does it just replaces one matched string?

I want to replace my url via Nginx's rewrite directive. For instance, the client side requests http://127.0.0.1/user/user_id/, and I want to let Nginx rewrite the url to http://127.0.0.1/person/person_id/.
My Nginx configuration is like this:
rewrite (.*)user(.*) $1person$2;
But I fount the Nginx changes the url to .../user/person_id/
Could someone tell me how to change the user to person via rewrite directive?
Assuming that the first instances of user and personare constant and that there is always a slash after the second item, you can try:
rewrite ^/user/user_([^/]+)/(.*)$ /person/person_$1/$2 ;
well this single case you are talking about can be solved simple:
rewrite ^/user/user_id/(.*)$ /person/person_id/$1 ;

nginx multi-stage 404 handling

We just moved to a new site, and want to redirect old links where necessary - however, some still work. For instance,
/holidays/sku.html
still works, while
/holidays/christmas/
no longer works. I'd like to be able to allow the site to attempt to serve a page, and when a 404 is reached, THEN try to pass it through a series of regex redirects, that may look like:
location ~* /holidays/(.*)+$ { set $args ""; rewrite ^ /holidays.html?r=1 redirect; }
I'm using a ~* location directive instead of doing a direct rewrite because we're moving from a Windows-based ASPX site to Magento with php-fpm behind nginx, so we suddenly have to worry about case sensitivity.
Without using nested location directives (which are actively discouraged by nginx documentation) with an #handler of some sort, what's the best way to allow nginx to attempt to serve the page first, THEN pass it across redirects if it fails?
Thanks!
http://wiki.nginx.org/NginxHttpCoreModule#try_files

Resources