Nginx an application redirects on root site - nginx

Navigate to localhost/app1 redirects me on localhost/login, expected - localhost/app1/login.
Is it possible to change the application redirect to the right place using nginx?
location /app1/ {
proxy_pass http://localhost:8080/;
}

Used Referer URL to make the right way redirection. The configs below:
nginx.conf - get application name from $http_referer
http {
map $http_referer $app {
~*//[a-z0-9_.-]+/([a-z0-9_.-]+) $1;
}...
}
root.conf - redirect wrong URLs to $app$uri
location / {
if ($http_referer != "") {
rewrite ^(.*)$ /$app$uri redirect;
}
app1.conf - important! The Referer header must be added!
location /app1/ {
proxy_pass http://localhost:8080/;
proxy_set_header Referer $http_referer;
...
}

Related

Nginx rewrite not redirecting

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.

How to proxy_pass only remaining URL after a match

Only need to proxy pass remaining url when match to location
location /blog { proxy_pass http://example.com }
i.e if somebody requests /blog/page1/temp.html they are getting proxy passed to example.com/blog/page1/temp.html
I want to change it to example.com/page1/temp.html
I want to change example.com/blog/page1/temp.html to example.com/page1/temp.html
Specify uri into proxy pass directive (/ after hostname in this case):
location /blog/ {
proxy_pass http://example.com/;
}
Or use rewrite like this:
location /blog {
rewrite /blog/([^/]+) $1;
proxy_pass http://example.com
}

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;
}

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

Convert 3 redirect rules into one

A proxy app proxies to other apps with some regex in the location blocks of the nginx.conf. For example to a signup app called proxy-signup-app.
This works, but it has 3 location blocks:
location ~* ^/signup/(.*)/$ {
proxy_pass https://proxy-signup-app.herokuapp.com/?proxy_regex=$1;
}
location ~* ^/signup/(.*)$ {
proxy_pass https://proxy-signup-app.herokuapp.com/?proxy_regex=$1;
}
location ~* ^/signup$ {
proxy_pass https://proxy-signup-app.herokuapp.com/?proxy_regex=$1;
}
It should proxy
/signup
/signup/
/signup/rainyday
/signup/rainyday/
Where the proxy app would receive on proxy_regex:
$1 = ''
$1 = ''
$1 = 'rainyday'
$1 = 'rainyday' (not /rainyday/, rainyday/ or /rainyday)
It should not redirect
/signups
How to merge these location blocks into one?
not sure if it's the best way, but it should work
location ~* ^/signup(/(.*?)/?|)$ {
proxy_pass https://proxy-signup-app.herokuapp.com/?proxy_regex=$2;
}

Resources