I have some files getting data from another domain not on my server. For instance pulling a pdf document from www.example.com/mypdf.pdf I have no control over this function, but instead of getting the pdf file from example.com, I want it to get the document from mydomain.com/mypdf.pdf instead.
2 Answers
You may use Module ngx_http_rewrite_module and especially the return directive.
Here’s a very simple example that redirects clients to a new domain name:
server {
listen 80;
listen 443 ssl;
server_name www.old-name.com;
return 301 $scheme://www.new-name.com$request_uri;
}
The
listen
directives mean the server{}
block applies to both HTTP and HTTPS.
The
server_name
directive matches request URLs that have domain name www.old‑name.com
.
The return
directive tells NGINX to stop processing the request and
immediately send code 301
(Moved Permanently) and the specified rewritten
URL to the client.
The new URL uses two NGINX variables to capture from the original request URL:
$scheme
is the protocol (http or https)$request_uri
is the full URI including arguments.
For more information see Creating NGINX Rewrite Rules.
-
this also works for outgoing links to external sites not on the server? thought listen only worked for incomming requests. Commented Mar 24, 2022 at 7:13
-
Your question relates to incoming requests. If the nginx server distributes requests to other servers, then this relates to those as well.– harrymcCommented Mar 24, 2022 at 8:48
-
Thanks for trying to help Harrymc, much appriciated as I still trying to learn all this server configuration stuff. Tried your code and no effect. I think the problem is that since example.com isn't located on this server, the listen function isn't picking up when the php script tries to get data from example.com. In windows I could do it by changing the host file to www.example.com 127.0. 0.1. I am looking for something similar here, but if possible, something that only affects mydomain.com on the server. not sure it's even possible. Commented Mar 24, 2022 at 9:27
-
I don't understand - you can't modify a request that doesn't pass through your server.– harrymcCommented Mar 24, 2022 at 9:30
-
Not even by doing some local dns lookup manipulation? So the script instead of getting the pdf document from externaldomain.com/mypdf.pdf, it gets it from myserver.com/mypdf.pdf ? I seem to recall doing something similar on an apache server a long time ago. Commented Mar 24, 2022 at 10:56
What you are describing is basically a link.
You can create a link in your websites HTML which points to an external resource:
<a href="https://www.example.com/mypdf.pdf">link text</a>
-
Yes a link, but I need to change the links target through server configuration. So if I add a link to example.com/mypdf.pdf, instead of going to example.com/mypdf.pdf I want to to go to mysite.com/mypdf.pdf instead.. Not sure if this can be done through nginx settings, beginning to doubt. But maybe some server dns lookup. Commented Mar 23, 2022 at 19:58