NGNIX url rewriting doesn't use root statement - nginx

in the site configuration I have this simple statement:
location /test/mysite/ {
root /folder/;
rewrite ^/test/mysite/(.*)$ /$1 last;
}
I would like that ngnix remove the /test/mysite/ part and accesses the resource from /folder/.
For example, if I go to http://mywebsite/test/mysite/test.html
it would load the file ŧest.html from /folder/test.html

Related

NGINX Rewrite is encoding querystring as a path

I want to rewrite internally several locations /customers/foo?bar=2 to an existing location in my nginx configuration at /blah so that it's as if the request was to /blah/customers/foo?bar=2.
location /blah {
# View in fiddler
proxy_pass http://127.0.0.1:8888/;
# Lots of config here I don't want to repeat everywhere else
}
location /customers/ {
rewrite ^/customers/(.*) /blah/customers/$1$is_args$args;
}
location /other/ {
rewrite ^/other/(.*) /blah/other/$1$is_args$args;
}
# etc...
Nginx is rewriting the URL with the querystring encoded as a path /blah/customers/foo%34bar=2.
The same thing happens with rewrite ^ /blah$request_uri;. It encodes the ? as %3F effectively garbling the URL.
If I do a client redirect rewrite ^ /blah$request_uri permanent; the URL is correct and contains the ? but I want an internal redirect inside my NGINX config.
Don't use $is_args$args, because the rewrite directive will automatically append any existing query string.
For example:
rewrite ^/customers/(.*) /blah/customers/$1 last;
Although, I would prefer:
rewrite ^(.*)$ /blah$1 last;
Or even:
rewrite ^ /blah$uri last;

Nginx How do i route to reverse proxy by location

Currently i'm using nginx server and using nodejs server as reverse proxy.
I want to make the nginx server proxy_pass different server by location.
So, lets say my domain is subdomain.domain.com.
When loading subdomain.domain.com/, this should serve static files.
When loading subdomain.domain.com/example1/req1/req2, this should route to 127.0.0.1:3000/req1/req2.
When loading subdomain.domain.com/example2/req1/req2, this should route to 127.0.0.1:8080/req1/req2.
But my configuration routes subdomain.domain.com/example1/req1/req2 to 127.0.0.1:3000/example1/req1/req2 resulting error. (nodejs returns Cannot GET /example1)
How should I write this nginx conf file properly?
Try use rewrite directive in location block.
Something like:
location /example1/ {
rewrite ^/example1/(.*)$ /$1 break;
proxy_pass http://xxxxxx;
}
You can check documents related to rewrite module at http://nginx.org/en/docs/http/ngx_http_rewrite_module.html
You need to add below code snippet to your nginx.conf.
server subdomain.domain.com;
location / {
rewrite ^/example1/(.*)$ /$1 break;
proxy_pass http://127.0.0.1:3000;
}
}

Nginx, nuxt.js static generated mode and redirection of trailing slashs

i currently have a problem configuring my Nginx correctly for nuxt.js generated sites.
What i want to achieve is the following
/magazin -> /magazin/index.html
/magazin/ -> 301 /magazin
/magazin/artikel/titel-goes-here -> /magazin/artikel/titel-goes-here/index.html
/magazin/artikel/titel-goes-here/ -> 301 /magazin/artikel/titel-goes-here
currently this is the other way around.
If im correct i shouldn't use a proxy pass to a e.g. pm2 instance with express etc. as it destroys the sense of static site generation.
But how can i get this page structure to work, as i need the same url's as our legacy service for SEO reasons, which used Angular Universal SSR
my current config is:
location ^~ /magazin {
root /path/to/dist;
index index.html ;
}
if i add something like
rewrite ^(.+)/+$ $1 permanent;
i get an infinite 301 loop
Thanks for the help
You cannot use the built in index directive, as it works the other way around (as you have observed).
You can use try_files to test for the existence of the index.html file. Use a named location to process the redirection.
For example:
location ^~ /magazin {
root /path/to/dist;
try_files $uri $uri/index.html #rewrite;
}
location #rewrite {
rewrite ^(.+)/$ $1 permanent;
}
See this document for details.

nginx location rewrite not matching

In our nginx configuration we have defined the following rewrite to forward requests to another domain:
location /service {
rewrite ^/service/(.*)$ https://newdomain.com/service/$1 redirect;
}
It matches URLs like http://www.example.com/service/test123 but the following URL does not match:
https://www.example.com/service/imprint/acc/123456/ext_css/http://www3.example.com/formulare/css/service.css
Yes there is a second URL after /ext_css/ and we now that's not correct but at the moment there is no way to change that.
Is there a way to completely forward the whole path to the new server?
/service/imprint/acc/123456/ext_css/http://www3.example.com/formulare/css/service.css
Doing it outside of the location block did the trick.
Instead of
location /service {
rewrite ^/service/(.*)$ https://newdomain.com/service/$1 redirect;
}
we now have just
rewrite ^/service/(.*)$ https://newdomain.com/service/$1 redirect;
Now it matches everything written after /service/

nginx try_files with s3 bucket and proxy_pass for html pages without extension

I have an s3 bucket that contains directories and html files. I'm using nginx in front of the bucket and have configured a proxy_pass to the bucket, which works fine. I want clean URLs without the html extension, and for these URLs to resolve properly. For example, instead of foo.com/bar/my-file-name.html, this must be accessible at foo.com/bar/my-file-name
The primary question here is how to prevent requiring the .html at the end of every URL. As in, I want nginx to handle routing each requests and try looking for a file with the .html extension, and then return it.
My problem is that I can't figure out how to force nginx to try_files like I would on a non-S3 site. When I do try and add any configuration of try_files, it breaks the site by returning HTTP 500 errors on all paths.
For comparison, here is the configuration that works great on a non-S3 backed site (with the files on the server itself):
location / {
try_files $uri $uri/ #htmlext;
}
location #htmlext {
rewrite ^(.*)$ $1.html last;
}
My goal is to replicate this behavior with a proxy_pass to an S3 bucket and force it to try filenames with an .html extension other than index.html, which works out of the box.
Here is my current configuration that works great, minus any html files other than index.html:
location / {
proxy_pass my-bucket.s3.blahblah/;
}

Resources