Nginx keeps redirecting and adding tailing slashes - nginx

I am trying to redirect an app to some static html files.
I am using NGINX as a reverse proxy, and alias to use a specific path.
So I am using a simple Redirect within a react container:
redirectTo="/documentation/index-docs.html"
This is the Nginx config part used for that:
location /documentation {
alias /workspace/documentation;
rewrite ^(.+)/+$ $1 permanent;
The current behavior is that it keeps looping and adding tailing slashes.
http://localhost:3001/documentation/index-docs.html/////////////
Did anyone encounter the same issue before ?
Thanks.

Related

How can I rewrite parts of a Location header with nginx?

I'm trying to setup a reverse proxy nginx in a docker-compose setup I have. It works fairly well, but I have a problem with a 302 redirect.
The nginx server runs on https://localhost:1253. When I return the redirect within my internal server, nginx will use the internal name (serviceweb) of my docker container in the redirect uri. This is my redirect uri:
https://accounts.google.com/o/oauth2/v2/auth?response_type=code&<...>&redirect_uri=https%3A%2F%serviceweb%2Fsignin-google...
I'd like to change serviceweb to localhost:1253.
How can I do this with nginx?
I've tried this simple setup:
location / {
proxy_pass https://serviceweb;
proxy_redirect serviceweb localhost:1253;
}
But this does not quite work. The location header still contains the original value. I'm sure I somehow have a problem in the proxy_redirect rule , but I'm not sure how. There's a regex based way of doing it according to http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_redirect, and I think my current setup would work with that.
What am I doing wrong?

Nginx server not working with trailing slash

I have setup Ubuntu 20.4 with Nginx.
All working fine except following
When someone hit https://www.example.com/webservice.php all working fine as expected.
but If tried to hit https://www.example.com/webservice.php/ then it gives 404 error.
here, It just have extra slash at end.
I already tried to update Nginx default file as follow with rewrite rules
rewrite ^/(.*)/$ /$1 permanent;
However it redirect to webservice.php from webservice.php/
But it just result empty page while it rewriting it doesn't pass it request body there.
Please, any solution?
By adding the slash, you are saying it's a URI pointing at a directory and not the PHP file. There is no directory called webservice.php I presume. Only a file. Therefore, the 404 error. Don't do that.

How do I configure nginx for WordPress REST API in sub-folder?

I am trying to set up multiple Wordpress sites in sub-folders under our domain (ie not multi-site), but I have difficulty configuring the REST API endpoints. For example, this endpoint works fine:
https://example.com/site1/?rest_route=/wp/v2/posts
But this endpoint gives a 404:
https://example.com/site1/wp-json/wp/v2/posts
I have tried to rewrite the failing url to the working url with these rules in my nginx configuration:
location /site1/wp-json {
rewrite ^/site1/wp-json(.*)$ /site1/?rest_route=$1;
}
location /site1/ {
try_files $uri $uri/ /site1/index.php$is_args$args;
}
I can't see any special handling of wp-json in the WordPress docs or the nginx wiki. What am I missing here? The permalinks for the site is set to Numeric (https://example.com/site1/archives/123) if that might play a role.
Update
Gist of the redacted full config file and the config syntax lints okay:
nginx -c /etc/nginx/nginx.conf -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
I just hit this too, in WP 5.7. Without pretty permalinks, ie with the "Plain" option like ?p=123, my nginx WP installation uses requests like:
/index.php?rest_route=/wp/v2/users/&who=authors...
And these all work fine.
However if I enable pretty permalinks, eg "Post name", /sample-post/, it starts making requests like:
/wp-json/wp/v2/users/?who=authors...
And these all return a 404. For example, editing or publishing posts fails, and browser devtools shows a string of 404s in this format.
But now we know the pattern that works, a solution is clear - we just need to map the not-working format to the working format:
# Resolves WP Gutenberg 404 issue
location /wp-json {
rewrite ^/wp-json(.*)$ /index.php?rest_route=$1 last;
}
I believe that the rewrite directive should be written as shown below:
server {
location /site1/wp-json
{
rewrite ^(/site1/wp-json.*)$ /site1/?rest_route=$1 last;
}
}
I was able to resolve it like this:
location /wordpress/ {
rewrite ^/wordpress/wp-json/(.*?)$ /wordpress/index.php?rest_route=/$1 last;
}
An easy way if your website pages in the subfolder is already working, just add index.php to the url:
https://site1.com/site2/index.php/wp-json/
If your website pages still doesn't work in the subfolder, add this code to nginx/sites-available/website.conf file too:
location /site2 {
rewrite ^(/[^/]+)?(/wp-.*) /site2/$2 break;
rewrite ^/site2/(.*)$ /site2/index.php?q=$1 last;
}

Nginx: rewrite to a file without URL changing

I have tried many options, but did not find a suitable answer. I can do this in Apache, but I can’t figure out how to do a redirect in Nginx while maintaining the URL
.../s1 changes to .../image.jpg - how to fix it?
location /s {
rewrite "/s1" https://example.com/image.jpg last;
}
A redirect always changes the url, a rewrite keeps it internally.
In this case nginx is turning your rewrite into a redirect, because you specified a different server. You have 2 options:
If the url you want to rewrite to is local, you should simply remove the https://example.com part to make the url relative instead of absolute.
If you really want to mask a different server, then what you need to build is a reverse proxy. Nginx does have features for this.

How to rewrite path to url with semicolon in nginx?

I am using nginx as a reverse proxy, a Angular Universal application is serving in the background.
I am trying to rewrite an old URL pattern to a new one via nginx, namely
server {
rewrite ^/s/(.+)$ /search\;q=$1 permanent;`
...
}
such that something like /s/keywords gets redirected to /search;q=keywords
Unfortunately, the above nginx rule turns
/s/keywords into /search/;q=keywords (so a slash gets added after /search). Is there a chance to remove this slash so the result is /search;q=keywords?
(The application running behind nginx expects the url to be /search;q=keywords.)
You could add quotes to the replacement, for example:
rewrite ^/s/(.+)$ "/search;q=$1" permanent;

Resources