ProxyPass GET parameters via image using Nginx - image

I have an image that is served as static file using Nginx, but I need to call it with different GET parameters. Is it possible to proxypass those parameters to different server BUT (Here is the trick) return the static image back?
Thank you in advance.

If I'm understanding you correctly, you want to pass the request arguments to a different server, but mask this from the user by returning a static image?
If you can make the remote server return a 404, then this should work:
root /path/to/webroot;
location #otherserver {
include proxy_params;
proxy_pass http://some.other.server;
}
location /image.jpg {
try_files #otherserver /real_image.jpg;
}
You could probably leverage the error_page directive to similar effect.

Related

Extending Nginx config on Beanstalk doesn't rewrite urls properly

I have an existing Laravel API application running on Beanstalk. It's been lagging in updates on EBS, so currently I'm in the process of upgrading the platforms and CI/CD for this app. There one remaining problem I'm running into, which leaves me scratching my head at the 'but it should work'-level.
What I want
All URLs containing https://example.com/index.php/endpoint to be redirected to https://example.com/endpoint and show the same content as https://example.com/index.php/endpoint would (incl. subsequent the URL's slugs)
How I'm trying to do this
Due to this wonderful answer by cnst, I have the configuration below that seems to work for many (incl. some other online sources).
server{
index index.php;
if ($request_uri ~* "^(.*/)index\.php/*(.*)$") {
return 301 $1$2;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
# Remove from everywhere index.php
if ($request_uri ~* "^(.*/)index\.php(/?)(.*)") {
return 301 $1$3;
}
}
if (!-d $request_filename) {
rewrite ^/(.+)/$ /$1 permanent;
}
if ($request_uri ~* "\/\/") {
rewrite ^/(.*) /$1 permanent;
}
}
I'm putting this configuration in a file located at my_project/.platform/nginx/conf.d/proxy.conf, which according to AWS' documentation, should upload with the project and extend the nginx configuration. As far as I can tell, it does pick it up, since any typo will result in an error after eb deploy. I can also see on the server it has been added to /etc/nginx/conf.d/proxy.conf.
The Problem
Even though the extending proxy.conf is being deployed and the configuration in it seems to be picked up, the application won't pick up the rewrite and leave the application URLs running with the index.php instead of the rewrite.
https://example.com/index.php/endpoint → works
https://example.com/endpoint → results in a server generated 404
Nginx logs show 2021/02/12 14:23:24 [error] 7523#0: *35 open() "/var/www/html/public/api" failed (2: No such file or directory) which tells me it has searched for a file and never tried running it through index.php.
The Questions
What am I missing in my configuration?
Or is it something about EBS that I overlooked or misunderstood?
Is the index.php angry since I'm trying to hide its face from public view?
Solution moved from the question to an answer:
I gave it a weekend to see if anyone would know and went back to work.
First thing, I did is see if Beanstalk was picking up any
configuration, so I put an invalid variable in and see if that would
break the server. It didn't...
Second, I checked if my Beanstalk instance was actually using Nginx
(default) or got switch to Apache (httpd) for some reason (it includes
both). Via its GUI config I could easily tell it's Nginx.
Third, I viewed the nginx.conf on the server and checked how other
.conf files were being included. Part of it is seen here;
http {
[...]
include conf.d/*.conf;
map $http_upgrade $connection_upgrade {
[...]
}
server {
listen 80 default_server;
access_log /var/log/nginx/access.log main;
[...]
# Include the Elastic Beanstalk generated locations
include conf.d/elasticbeanstalk/*.conf;
}
}
Here lays the problem; I was including a file at the conf.d/*.conf
level with a second nginx server configuration block, which is
effectively overwritten with the standard server configuration block
by Beanstalks own config.
So there's two solutions here;
override the entire nginx.conf by including a new .platform/nginx/nginx.conf in your project, where you extend the
server block with your own config
or, in my opinion more gracefully, add .platform/nginx/elasticbeanstalk/proxy.conf to your project,
extending the server block specifically (but remove any server
blocks from your own config)
Solution 2 will gard that AWS can always update its default nginx.conf
without you having to watch out for it (unless they change the
location of the elasticbeanstalk configs).
I did try putting my configuration in
.platform/nginx/elasticbeanstalk/proxy.conf before, but that would
break the server, since I was including a server block, causing it
to double nest.
Lesson here;
add .platform/nginx/nginx.conf to override your entire Beanstalk Nginx configuration
add .platform/nginx/conf.d/your_conf.conf for any extensions to the http block
add .platform/nginx/conf.d/elasticbeanstalk/your_conf.conf for any extensions to the server block (or nesting within)

How to setup nginx cache for script-generated images?

I have a script, that generates and outputs images directly (uses http://glide.thephpleague.com/).
All images are served from /img/.
How can I configure NGINX to cache them, bypass the script and serve images directly?
E.g. it should catch response from script, put the image somewhere (the best if on a separate server) and serve directly on subsequent requests
You'll need to provide your nginx config if you'd like a more complete answer.
Following the standard cache setup found here should do the trick. If you're serving files out of the /img/ always, then you could do the following:
location ^~ /img/ {
alias /absolute/path/to/img/folder;
expires 31d; #or whatever you prefer
add_header Vary Accept-Encoding;
add_header Pragma public;
add_header Cache-Control public;
error_page 404 = #your_upstream_generating_the_files;
}
What this does is it first checks the /img/ folder if the file is there. If it is not, you want to pass it to your application so that it can generate it for you. Next time the resource is requested, it will serve it out of the /img/ folder.

nginx proxy_pass to Jboss+Spring/Websphere+Portal projects

I have an nginx up at the front serving as a proxy to two servers, one running Websphere Portal Server and one running Spring on a Jboss server.
I'm currently having problems with the proxying of certain requests, for instance, I have the following:
server{
listen:8080;
server_name:localhost;
location /jbossSpring/ {
proxy_pass http://177.21.1.15:9000/Spring_project/;
}
location /webspherePortal/ {
proxy_pass http://177.21.1.15:9400/Portal_project/;
}
}
Now, this does the proxy from localhost:8080/jbossSpring/ and localhost:8080/webpsherePortal/ correctly, however, the pages I get keep requesting files that are located on localhost:8080/Spring_project/ and localhost:8080/Portal_project/.
Is there anyway for me to handle these in nginx? or do I have to modify the Spring/Portal projects to get the right url? (path dependencies probably?)
You may achieve this result by using http rewrite module, documented at ngx_http_rewrite_module
To give an idea, I guess your rewrites shall look like below (I haven't validated this)
server {
...
rewrite ^/Spring_project/(.*) /jbossSpring/$1 last;
rewrite ^/Portal_project/(.*) /webspherePortal/$1 last;
...
}

Proxy external images with nginx

Is there a way to proxy external images with nginx without downloading and storing them locally ? I.e. passing image's URI to my proxy server like this :
`http://myproxydomain.com/http://example.com/image.jpg`
And including this URI in the src attribute for images on my site.
Yes -- this is possible:
merge_slashes off;
location ~ /(?<r>http://.*) {
resolver 127.0.0.1;
proxy_pass $r;
}
Whether you should do it is another matter. Having merge_slashes off may not be a good idea.

Redirect 301 in Nginx (images and dynamic pages)

I am changing my site address and I am having difficulty in two types of 301 redirects on the server Nginx. I've tried MANY ways to do this, but everything went wrong so far.
I want to change the:
^/downs/pafiledb.php?action=download&id=(NUMBER)
to other site:
http://www.newsite.com/downloads/NUMBER
And also a problem with the redirect with the pictures, see below.
I want to change the entire folder to another site:
^/images/
to
http://www.newsite.com/images/
How can I do this? I've tried every way possible in Nginx and nothing works.
location = /downs/pafiledb.php {
if ($arg_action = download) {
return 301 http://www.newsite.com/downloads/$arg_id;
}
}
location /images/ {
return 301 http://www.newsite.com$request_uri;
}
http://nginx.org/r/location
http://nginx.org/en/docs/http/ngx_http_core_module.html#variables
http://nginx.org/r/return
http://nginx.org/r/if
http://nginx.org/en/docs/http/request_processing.html

Resources