How do you proxy a specific image with Nginx - nginx

I have this url
https://example.com/image/test.jpg
And i would like to proxy with Nginx the image at this url
https://cdn0.tnwcdn.com/wp-content/blogs.dir/1/files/2018/02/google-pacman-796x419.jpg
without the url changing i.e. staying the same as this https://example.com/image/test.jpg
What would you put in the location block to get this to happen?

You can use the proxy_pass parameter in your nginx config:
server {
# ...
server_name example.com;
# ...
location /image/test.jpg {
proxy_pass https://cdn0.tnwcdn.com/wp-content/blogs.dir/1/files/2018/02/google-pacman-796x419.jpg;
}
}

Related

Nginx Proxy Pass parameter issue

Here is a URL : http://123.com/stat/api/abc?a=123
if I want to set proxy pass to : http://456.com/api/abc?a=123
My nginx config as below :
server {
listen 80;
server_name 123.com;
location /stat {
proxy_pass http://456.com;
}
}
And this is not work.
Any ideas?
Finally, I found the solution.
The final config as below :
location /stat/ {
proxy_pass http://456.com/;
}
the end of slash in location section and proxy_pass section must be have.

Nginx same location 2 blocks with diffrent proxy pass

Trying to configure nginx to have 2 proxy passes for the same location.
Example:
domain.com to go to the homepage proxy
domain.com/user to go to user profile proxy
What i need to do:
What I tryed:
location = / { proxy_pass http://home_page; }
location ~ ^/(.+) { proxy_pass http://user_profile_page; }
If I go to domain.com/ and domain.com/username it goes to the same proxy.
I have to mention that the config has also the /auth location block with exact match "="
that has domain.com/auth, domain.com/auth/login. This location block works as expected
location ^~ /auth { proxy_pass http://auth_page; }

Nginx revers proxy config showing 404 not found

i am going to set a location /attendance and want to make all the requests could redirect to https://example.me.com
follows the nginx.conf i've set
// nginx.conf
// upstream
upstream attendance {
server https://example.me.com/
}
// location
server {
listen 80 default_server;
...
location /attendance {
proxy_pass http://attendance/;
}
}
which shows 404 and how to deal with it?
example:
working api is : https://example.me.com/xx/xxx
and i am going to have a post request like:
http://my.nginx.server.ip/attendance/xx/xxx
Remove the upstream block and pass the requests directly to your backend:
// nginx.conf
server {
listen 80 default_server;
...
location /attendance/ {
proxy_pass http://my.example.com/;
}
}
Your reverse proxy will forward requests to http://upstream/attendance/..., If you want forward without /attendance sub-directory, add this rewrite rule inside location block.
rewrite /attendance(.*) $1 break;

nginx proxy_pass removing base path

So I currently have a site at http://www.example.com and I would like that all requests from
http://www.example.com/api/v2/<anything>
to be proxied to
http://api.v2.com/<anything>
Note that http://api.v2.com does not have a base path.
My config:
server {
listen 80;
server_name www.example.com;
location /api/v2/ {
proxy_pass http://api.v2.com;
}
location / {
index index.html;
root /var/www/example.com/htdocs;
}
}
This way, however, the requests are being proxies to http://api.v2.com/api/v2/<anything> keeping the original path, and not http://api.v2.com/<anything>.
I've already noticed that:
location /api/v2 {
proxy_pass http://api.v2.com/api;
works as desired - http://api.v2.com/api/v2/<anything> proxies to http://api.v2.com/api/<anything>.
Is there a way to achieve the first behavior (that is , http://api.v2.com/api/v2/<anything> to http://api.v2.com/<anything>)? Do I have to use a rewrite? I know it it related to normalized URIs and that is expected, just would like to know if there's a way to do it.
If you want to remove the proxy path from resulting url to the server, either you should have the uri part in the proxy url, or you should specify the rewrite part.
In this specific case, it should be like as follows,
location /api/v2/ {
rewrite /api/v2/(.*) /$1 break;
proxy_pass http://api.v2.com/;
}
for more information visit nginx documentation https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass
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.
server {
listen 80;
server_name www.example.com;
location /api/v2/ {
proxy_pass http://api.v2.com/;
}
location / {
index index.html;
root /var/www/example.com/htdocs;
}
}
Source: http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass

Nginx Reverse Proxy

I have an Nginx instance set up with a reverse proxy like so:
server {
listen 80;
server_name sitep-rate00.sv.walmartlabs.com;
location / {
proxy_pass http://www.example.com;
}
}
How can I set the css style sheet to a specific version, for example something like:
Substitute 's|styles/sheets.1.1.1(.*)\.css"|styles/sheets.1.1.33.css"|i'
If you are planning to proxy pass it too you can try a rewrite before the proxy pass
location / {
rewrite /styles/sheets.1.1.1(.*)\.css /styles/sheets.1.1.33.css last;
proxy_pass example.com;
}

Resources