Nginx redirect with parts of URL - nginx

I have a redirect problem with nginx and I really don't know how to do it.
The problem is that I need to redirect all the traffic to a website INCLUDING THE PATH
BASE_URL: https://example.com/auth/
For example if the request is: https://example.com/auth /some/path/
I need it to redirect to: https://newdomain.com /some/path/
In other words, I need to concatenate the part of / some / path / to the new URL.
Here is the part of my code that I have tried
location /auth/* {
proxy_pass https://newdomain.com/$request_uri;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
}
Thanks.

the issue seems to be the URL handling. You need a small regexp and get the result.
try:
location ~ ^/auth/(.*)$ {
proxy_pass https://newdomain.com/$1;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
}
You can find examples there https://www.liaohuqiu.net/posts/nginx-proxy-pass/

Related

How to remove first part of route in nginx?

I have nginx configured on server like below
localhost ~ elastic{
proxy_pass http://localhost:5000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
With this I go on http://nginx_host/elastic/something I reach http://localhost:5000/elastic/something.
But I want to change it such that by going on http://nginx_host/elastic/something I reach http://localhost:5000/something.
I have many other services running on this server using same configuration as above, therefore I cannot use a generic method to remove the first part of the path, it needs to be specifically to remove elastic from start. How can I do this?
You can modify the config like this:
localhost /elastic/ { # note the trailing slash here
proxy_pass http://localhost:5000/; # note the trailing slash here
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
The trailing slashes will ensure that /elastic/ are removed from the url while proxying the request. So, http://nginx_host/elastic/something will now go to http://localhost:5000/something.

NGINX as reverse proxy to serve paths directly via url

I am looking to configure nginx to serve following requirement
We are serving individual websites at www.company.com/mywebsites and want to use nginx as reverse proxy to serve the same website on custom URL e.g www.mywebsites.com
I have configured dns of mywebsites.com to point to nginx server and following code block serves the home page of www.company.com
server {
server_name www.mywebsites.com;
location / {
proxy_pass http://127.0.0.1:9002;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
however i am struggling to get the part of redirecting www.mywebsites.com to the www.company.com/mywebsites path. I have tried added proxy_redirect but that didn't help and also trying to add path in the proxy_pass doesn't work either as it then tries to fetch all of the components using www.company.com/mywebsites/required/componenets.css which results in 404 as the correct path is www.company.com/required/componenets.css
You might need to do this change:
server {
server_name www.mywebsites.com;
location / {
rewrite /mywebsites/(.*) /$1 break;
proxy_pass http://127.0.0.1:9002;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_redirect off;
}
}
However, I truly recommend that this issue to be fixed from the site code itself (I don't know what the site is developed with), as this might have unknown side effects on some URLs.

NGINX internal redirect from uri path to JSF context root

I'm configuring a cloud server which use NGINX as reverse proxy to serve different applications on different URI (all the applications are on the same wildfly standalone instance).
To be more specific i've a JSF application with a contextroot, let's say, /jsfcontext and i've set up a NGINX location like /mypublicuri.
What happens is that when I navigate to https://myserver.com/mypublicuri/index.xhtml i receive the following error:
/mypublicuri/index.xhtml Not Found in ExternalContext as a Resource.
I'm pretty sure it's related to a missing internal redirect route or some kind of "hack" that i need to specify in order to make everything work but i'm a newbie in NGINX and I don't know how to properly set everything up.
Thanks for the help
Cheers
Read NGINX documentation but my lack of english knowledge makes difficoult to understand what should I have to do
My actual NGINX config
server {
server_name myserver.com www.myserver.com;
access_log /usr/share/logs/access.log;
error_log /usr/share/logs/error.log;
location / {
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_intercept_errors on;
location /anotherworkingapp {
add_header Allow "GET, POST, HEAD, PUT, DELETE" always;
proxy_pass http://127.0.0.1:8080$request_uri;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
}
location /mypublicuri {
proxy_pass http://127.0.0.1:8080/jsfcontext$request_uri;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
}
}
}

Nginx Reverse Proxy & Rewrite with Parameters

I'm trying to deploy a Bokeh Server application that requires an input parameter ("Part") in the form:
(1) http://127.0.0.1:5100/myapp?Part=1234-567
I want to use rewrite so that when a user hits the below URL
(2) http://<my_hostname>/myappRenamed/Part=1234-567
Nginx reverse proxies to URL (1)
Right now, my config file is similar to below.
location /myappRenamed/ {
proxy_pass http://127.0.0.1:5100;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_http_version 1.1;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host:$server_port;
proxy_buffering off;
}
This works when I hit
http://<my_hostname>/myappRenamed/myapp?Part=1234-567
Except I want the URL to be
http://<my_hostname>/myappRenamed/Part=1234-567
You should rewrite the URL first, making it URL friendly using nginx rewrite capabilities and the proxy it:
location /myappRenamed/ {
rewrite ^/myapp(.*) /$1 break;
proxy_pass http://127.0.0.1:5100;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_http_version 1.1;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host:$server_port;
proxy_buffering off;
}
I do not have your ENV so I can't really test the rule but you got the concept.

Nginx location proxy pass homepage one place, all other routes, another place

I need your help, I have not been able to find the exact way to do this.
I would like to send my homepage ONLY to one proxypass location
location / {
proxy_pass http://website;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
And then send every other route (I won't know what they will all be, it will be dynamic) to another proxy pass.
location /* {
proxy_pass http://interaction;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
I know the * is wrong (since it doesn't work), but it was just to represent /anything or /whatever or /idontknowyet
Thanks for the help!
You are not clear as to which URIs constitute your homepage (other than /).
Assuming that no resource files are required, and no redirection occurs, an exact match location block can be used to match the homepage, using the default location block to match everything else:
location = / {
proxy_pass http://website;
...
}
location / {
proxy_pass http://interaction;
...
}
See this document for details.
But there are probably other URIs (such as css and js files) that make up the homepage. You will need to add location blocks for those too, or perhaps use a regular expression location block that matches all of the URIs required by the homepage.

Resources