Welcome! Log In Create A New Profile

Advanced

Rewrite and redirect a URL

Posted by 7curepMN 
Rewrite and redirect a URL
October 18, 2016 09:52AM
Sorry for the newbie question, but I am struggling trying to rewrite and redirect a URL.

The external URL is www.mydomain:443/app1. The internal URL is 192.168.1.2:8080. Nginx is running on 192.168.1.1. I have tried several things looking at the docs and the forum. I believe I need 1 command to rewite the URL and then a second to redirect. I tried the code below, but it is not working.

location /app1{
rewrite ^/app1(.*) https://192.168.1.1$1 permanent;
proxy_pass https://192.168.1.2:8080;
Re: Rewrite and redirect a URL
October 18, 2016 02:53PM
location /app1/ {
rewrite /app1/([^/]+) /$1 break;
proxy_pass https://192.168.1.2:8080/;
}

---
nginx for Windows http://nginx-win.ecsds.eu/
Re: Rewrite and redirect a URL
October 18, 2016 03:28PM
This is getting closer. URL now comes across as https://www.mydomain.net/api/4/, but the page is blank. The internal URL that display correctly is https://192.168.1.2:8080/api/4. Does Nginx pass https://www.mydomain.net on internally as https://192.168.1.2:8080?
Re: Rewrite and redirect a URL
October 18, 2016 04:00PM
Try it without the trailing / in proxypass.

---
nginx for Windows http://nginx-win.ecsds.eu/
Re: Rewrite and redirect a URL
October 18, 2016 04:13PM
Removing the trailing / on the proxy_pas line caused the URL to change from https://www.mydomian.net/api/4/ to https://osuhickeys.no-ip.org/app1. I get a blank page then instead of a 404 Not Found like before.
Re: Rewrite and redirect a URL
October 18, 2016 04:29PM
Check the logfiles where the file is being looked for, the rewrite changes the client request towards where the backend should have the files requested, that needs to match.

---
nginx for Windows http://nginx-win.ecsds.eu/
Re: Rewrite and redirect a URL
October 18, 2016 04:35PM
Here are the last 2 entries which I think relate to the test with and without the slash.

172.98.67.120 - - [18/Oct/2016:15:11:23 -0500] "GET /app1/ HTTP/1.1" 307 0 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36"

172.98.67.120 - - [18/Oct/2016:15:11:23 -0500] "GET /api/4/ HTTP/1.1" 404 571 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36"
Re: Rewrite and redirect a URL
October 18, 2016 05:29PM
"GET /api/4/ HTTP/1.1" 404

Means files need to be in /api/4/
Looks like your backend is not looking there or is not configured to serve files from there.

---
nginx for Windows http://nginx-win.ecsds.eu/
Re: Rewrite and redirect a URL
October 18, 2016 09:10PM
It is an appliance and I do not have root access. Does that mean since I cannot set a URL base for it on the appliance end, I am at a dead end trying to get it to work with nginx?
Re: Rewrite and redirect a URL
October 19, 2016 03:16AM
What does this do:

location /app1 {
rewrite /app1/([^/]+) /$1 break;
proxy_pass https://192.168.1.2:8080/;
}

---
nginx for Windows http://nginx-win.ecsds.eu/
Re: Rewrite and redirect a URL
October 19, 2016 08:56AM
I think that is the same thing we tried before. It gets to the appliance, but the page is blank and the browser URL is redirected to https://192.168.1.2:8080/api/4/.

I changed the proxy pass line to https://192.168.1.2:8080/api/4/. I now can get to the web page externally, but login fails. I am guessing the pages at or after login or not in the /api/4/ folder or subfolders.
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 265
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready