Nginx rewrite not redirecting - nginx

I would like to rebuild a URL and redirect from https://test.com/info/schoolName/detail to https://test.com/school-info?name=schoolName with Nginx.
I have tried
location ~ ^/(school-info)(?:/(.*))?$ {
include /etc/nginx/servers/platform/shared/headers_proxy.conf;
proxy_set_header Host $backend_netlify_main;
proxy_ssl_server_name on;
proxy_pass https://$backend_netlify_main/$1/$2;
}
...
...
location ~* ^/(info|info/)$ {
include /etc/nginx/servers/platform/shared/headers_proxy.conf;
rewrite ^/info/(.?)/(.*)$ /school-info?school=$1 permanent;
proxy_pass $backend_cms;
}
however, if I visit https://test.com/info/byu/detail it's not doing a redirect at all.
EDIT: The /detail at the end is not important at all, so regardless of what is at the end of the URL the /schoolName/ is the most important part to be passed as a query parameter.

I think you need something like
location / { # "default" location
# do redirection for '/info/...' URIs
rewrite ^/info/([^/])* /school-info/$1 permanent;
# otherwise pass request to the default backend
proxy_pass $backend_cms;
}
location /school-info {
include /etc/nginx/servers/platform/shared/headers_proxy.conf;
proxy_set_header Host $backend_netlify_main;
proxy_ssl_server_name on;
proxy_pass https://$backend_netlify_main;
}
if you need to pass a request as /school-info/schoolName, or
location / { # "default" location
# do redirection for '/info/...' URIs
rewrite ^/info/([^/])* /school-info?name=$1 permanent;
# otherwise pass request to the default backend
proxy_pass $backend_cms;
}
location /school-info {
include /etc/nginx/servers/platform/shared/headers_proxy.conf;
proxy_set_header Host $backend_netlify_main;
proxy_ssl_server_name on;
proxy_pass https://$backend_netlify_main/school-info$is_args$args;
}
if you need to pass a request as /school-info?name=schoolName.

Related

Problem with nginx location and proxy_pass

I have a rule in my nginx.conf that does not work and I have no idea why. According to the documentation it should work. Part of the config looks like this.
The first rule on port 8100 works and redirects the call http://example.com/api/domains to https://localhost:8181/oan/resources/domains
# Working
server {
listen 8100 default_server;
server_name example.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
root /var/www/html/example;
location /api {
proxy_pass https://localhost:8181/oan/resources; break;
}
# For ReactJS to handle routes
location / {
if (!-e $request_filename) {
rewrite ^(.*)$ / break;
}
}
}
# Not working
server {
listen 8200;
server_name api.example.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
location / {
proxy_pass https://localhost:8181/oan/resources; break;
}
}
The last call to port 8200: http://api.example.com:8200/domains SHOULD redirect to: https://localhost:8181/oan/resources/domains but does NOT do that.
What is wrong with this config and how can I get the last rule on port 8200 do the correct stuff, always redirect to https://localhost:8181/oan/resources/$uri
When you use proxy_pass with an optional URI within a prefix location block, Nginx will transform the requested URI by performing a straight text substitution.
In your case, the prefix location value is / and the optional URI value is /oan/resources. So a requested URI of /foo will be transformed into /oan/resourcesfoo.
For correct operation, both values should end with / or neither end with /.
For example:
location / {
proxy_pass https://localhost:8181/oan/resources/;
}
See this document for details.

nginx : How to append a default file name and extension to a location

I use nginx as a proxy server to forward some request ( location /mnt/) to a dedicated upstream :
location /mnt/ {
expires 1y;
add_header Cache-Control public;
add_header X-Proxy-Cache $upstream_cache_status;
proxy_pass http://imaginary/; # match the name of upstream directive which is defined above
proxy_set_header Host $host;
proxy_set_header cf-ray '';
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_redirect off;
}
I've added before that location a rewrite rule to redirect silently (without changing shown url) /mnt/thumbnail/xxx?yyy to /mnt/thumbnail?yyy
location /mnt/thumbnail/ {
rewrite ^/mnt/thumbnail/(.*)$ /mnt/thumbnail last;
}
This is working as expected. But might not be the best way to do it.
I can't figure a way to set a permanent redirection to append /image.jpg to request made to /mnt/thumbnail?yyy like a kind of index page if no filename ends path.
All my attemps failed with rewrite or internal redirection cycle while processing "/mnt/thumbnail/image.jpg" or 404.
[EDIT]
I manage to have both redirect 301 and proxy forwarding. I had to replace the rewrite by a proxy_pass to forward and then I could add a rewrite to append /image.jpg if missing.
location = /mnt/thumbnail {
rewrite ^/mnt/thumbnail?(.*)$ /mnt/thumbnail/image.jpeg permanent;
}
location /mnt/thumbnail/ {
proxy_pass http://imaginary/thumbnail?$args;
}
As said in edit : I manage to have both redirect 301 and proxy forwarding. I had to replace the rewrite by a proxy_pass to forward and then I could add a rewrite to append /image.jpg if missing.
location = /mnt/thumbnail {
rewrite ^/mnt/thumbnail?(.*)$ /mnt/thumbnail/image.jpeg permanent;
}
location /mnt/thumbnail/ {
proxy_pass http://imaginary/thumbnail?$args;
}

Can Nginx insert a missing "/" or missing "/bla/" in a URL?

Looking through the error.log file of Nginx, we can see requests coming as one of two incorrect patterns:
http://www.example.com/app-contextmoduleA/controller1 -> should be http://www.example.com/app-context/moduleA/controller1
http://www.example.com/app-contextcontroller2 -> should be http://www.example.com/app-context/moduleB/controller2
The current Nginx configuration looks like this:
server {
listen 8080;
location /app-context/ {
proxy_redirect off;
proxy_set_header Host $host;
proxy_pass http://localhost:8888/app-context/;
}
}
The challenge is inserting the missing / after app-context (for the first wrong URL) or missing /moduleB/ (for the second wrong URL). It doesn't look like try_files would support that, and I have not found a way to do it with rewrite.
Is there a way for Nginx to rewrite the URLs for both use cases? In particular, I would prefer not to have to know all the name of the modules or controllers in advance. There are many, so "hard-coding" them in the rewrite rule would be onerous.
These should handle your example case:
location /app-context {
rewrite ^(/app-contextmoduleA)/(.*)$ /app-context/moduleA/$2 permanent;
rewrite ^(/app-contextcontroller2) /app-context/moduleB/controller2 permanent;
...
}
Check the ngx_http_rewrite_module for more info.
Looking through quite a few online resources and through trial and error, I was able to come to a good-enough solution:
location /app-context {
location ~ (moduleA|moduleB) {
# inserts a forward slash after app-context if not there,
# e.g. /app-contextmoduleA/foo/bar to /app-context/moduleA/foo/bar
rewrite ^(/app-context(?!/))(.*) $1/$2 break;
try_files $uri #proxy;
}
# inserts /defaultModule/ after app-context
# e.g. /app-context/controller1 to /app-context/defaultModule/controller1
rewrite ^(/app-context(?!/defaultModule/))(.*) $1/defaultModule/$2 break;
try_files $uri #proxy;
}
location #proxy {
proxy_redirect off;
proxy_set_header Host $host;
proxy_pass http://localhost:8888;
}

Nginx dynamic proxy configuration

Below is a static config of what I'm trying to do.
server {
listen 80;
server_name browser.shows.this.server.com;
location / {
proxy_set_header Host backend.server.com;
proxy_redirect http://backend.server.com/ http://browser.shows.this.server.com/;
}
}
How can I make backend.server.com dynamic for each request? I'd like to pass the domain somehow in the request. Maybe in a header?
You should use proxy_pass instead of proxy redirect. Hope this helps
alternatively can write a config like this
resolver your-server-ip;
set $upstream_endpoint http://your-url;
location / {
rewrite ^/(.*) /$1 break;
proxy_pass $upstream_endpoint;
}
http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass

Nginx redirect with rewrite

Assuming I want to redirect URIs like
http://server1:8081/test/admin/option?options
http://server1:8081/test/admin/option/suboption?options
http://server1:8081/test/admin/option/suboption/subsuboption?options
to
http://server2:8080/tomcat/admin/option?options
http://server2:8080/tomcat/admin/option/suboption?options
http://server2:8080/tomcat/admin/option/suboption/subsuboption?options
what nginx rules I have to use? I've tried the following but it doesn't work
location =/test/admin {
proxy_pass http://server2:8080/tomcat/admin;
proxy_redirect off;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
Try something along these lines ..
location /test/admin {
rewrite ^/test/admin(.*)$ /tomcat/admin$1;
}
location /tomcat/admin {
internal;
proxy_pass http://server2:8080;
[…]
}
That is, rewrite the requests to "tomcat/admin" which you can optionally make open to internal requests only.
In that location block, you can then proxy passthe request.

Resources