2

im trying to mock sites from my own FS. i want to intercept any http request and redirect it to my server that will return the needed file. i have wrote a simple default sever:

server { listen <<SERVICE_PORT>> default_server; server_name _; location / { proxy_set_header X-ORIGINAL-URI $request_uri; proxy_set_header X-ORIGINAL-REFERRER $http_referer; proxy_set_header X-ORIGINAL-HOST $http_host; proxy_pass <<BROWSE_RESOURCE_URL>>/browsing/resource/; proxy_redirect off; } } 

when a url as "http://exapmle.com" enters it works fine. but when any path is added as "http://exapmle.com/bar" it dose not pass the to <<BROWSE_RESOURCE_URL>>/browsing/resource/. currently i recive 404 but not from my server.

offcurce i dont need the orignal uri to be concated to my proxy_pass at all.

why dosent it work for me?

1 Answer 1

1

From Nginx's documentation:

A request URI is passed to the server as follows:

If the proxy_pass directive is specified with a URI, then when a request is passed to the server, the part of a normalized request URI matching the location is replaced by a URI specified in the directive

So, for your given configuration, when you request http://example.com/bar:

  • The normalized request URI will be /bar
  • The URI specified in the proxy_pass directive will be /browsing/resource/
  • The final URI that will be passed to the backend server is /browsing/resource/bar.

You have not configured the backend server to understand /browsing/resource/bar. So it only understands /browsing/resource/. That's why your backend server returned a 404 not found.

Because you don't want Nginx to combine the request URI with the URI specified in the proxy_pass directive, you can use another feature of the proxy_pass directive as mentioned in the Nginx's documentation:

When the URI is changed inside a proxied location using the rewrite directive, and this same configuration will be used to process a request (break):

...

In this case, the URI specified in the directive is ignored and the full changed request URI is passed to the server.

So you will instruct Nginx to rewrite all request URIs to the same URI /browsing/resource/ as follows:

location / { proxy_set_header X-ORIGINAL-URI $request_uri; proxy_set_header X-ORIGINAL-REFERRER $http_referer; proxy_set_header X-ORIGINAL-HOST $http_host; rewrite ^ /browsing/resource/ break; proxy_pass <<BROWSE_RESOURCE_URL>>; proxy_redirect off; } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.