S
S
Sergey2018-02-06 17:58:39
Nginx
Sergey, 2018-02-06 17:58:39

How to write correct rewrite?

There are several "wrong web pages" on the internal network that open correctly when "direct access" to the sites on which they are located. To access them from the outside, I use nginx with a location, inside which I do rewrite.
location example


location /esp1/ {
rewrite ^/esp1/(.*)$ /$1 break;
proxy_pass http://esp1;
}

An example of a page (I can't fix it, because it is "hardwired" into a certain device)

_div class="c"_
_a href="configmain"_Main_/a _
_a href="configall"_Hardware_/a_
_a href="configsrv"_Servers_/a_
_hr_
_a href="/debug"_Debug_/a_
_a href=" /configrst"_Restart_/a_
_/div_

Accessing the page ....ru/esp1/
When following the configmain link to the page ...ru/esp1/configmain
When following the configrst link , apparently because of the "/" the error is thrown to the root.
Logs
configmain

2018/02/06 16:59:27 [notice] 20908#20908: *1 "^/esp1(.*)$" matches "/esp1/configmain", client: 172.16.0.1, server: mediaserver, request: "GET /esp1/configmain HTTP/1.1", host: "192.168.1.16", referrer: "http://..................ru/esp1/"
2018/02/06 16:59:27 [notice] 20908#20908: *1 rewritten data: "/configmain", args: "", client: 172.16.0.1, server: mediaserver, request: "GET /esp1/configmain HTTP/1.1", host: "192.168.1.16", referrer: "http://..................ru/esp1/"

configrst

2018/02/06 16:59:35 [notice] 20908#20908: *1 "^/esp1(.*)$" matches "/esp1/", client: 172.16.0.1, server: mediaserver, request: "GET /esp1/ HTTP/1.1", host: "192.168.1.16"
2018/02/06 16:59:35 [notice] 20908#20908: *1 rewritten data: "/", args: "", client: 172.16.0.1, server: mediaserver, request: "GET /esp1/ HTTP/1.1", host: "192.168.1.16"
2018/02/06 16:59:37 [error] 20908#20908: *1 open() "/home/www/configrst" failed (2: No such file or directory), client: 172.16.0.1, server: mediaserver, request: "GET /configrst HTTP/1.1", host: "192.168.1.16", referrer: "http://..................ru/esp1/"


I repeat that I can't fix the page, and I have a lot of similar "servers" (href="/...").
Moreover, the only way to access them is location. NGINX is accessible from the outside at a single address: port, and there are many sites inside the network.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
ivankomolin, 2018-02-08
@ivankomolin

I read it several times and still did not understand your chaotic story.
But what you want is called proxying.
Let's look at the details with examples:
There is a test.com resource and it has an image at test.com/images/img.jpg
1. There is a test2.com resource where we would like to display this image at test2.com/images /img.jpg
In the test2.com server settings, write the following location:

location /images/ {
  proxy_pass http://test.com;
}

2. There is a test2.com resource where we would like to show this picture at test2.com/pictures/img.jpg
In the test2.com server settings, write the following location:
location /pictures/ {
  rewrite /pictures/(.+) /images/$1 break;
  proxy_pass http://test.com;
}

I specifically showed 2 examples to understand why you need rewrite, and why proxy_pass

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question