Proxy external images with nginx - image

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.

Related

Magento redirecting AWS Load Balancer

Good afternoon,
I have a Magento installation with Nginx running on an Auto Scaling Group on AWS with a standard 3 instances. To redirect traffic I use a load balancer with SSL causing my structure looks as follows:
User> Load Balancer (Port 443)> Instance (Port 80)
I changed into my database in the table mg_core_config_data the URLs to use https.
The problem I think is happening is the following:
Every time I access my URL, Load Balancer attempts to fetch the bodies content to send me the information I am requesting, in this way, as I am using port 80, when the Load Balancer reaches my instance and attempts to load the Magento, the base_url that is in the database redirects to https. With this redirection, the process is repeated again because I'm redirected back to https: // and try to get the instance information on port 80 again. I think that every problem is among this base_url and I can not return the magento information with port 80.
Follow my nginx configuration:
server {
listen 80 DEFAULT_SERVER;
server_name _;
root /home/ubuntu/www/mysite;
index index.php index.html index.htm;
location / {
try_files $ uri $ uri / /index.php$is_args$args;
}
location ~ \ .php $ {
try_files $ uri /index.php = 404;
fastcgi_pass unix: /run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $ document_root $ fastcgi_script_name;
fastcgi_params include;
fastcgi_cache CACHE;
fastcgi_cache_methods GET HEAD;
fastcgi_cache_valid 200 1m;
fastcgi_cache_bypass $ no_cache;
fastcgi_no_cache $ no_cache;
}
...
}
The error I'm getting accessing my load balancer via https:
Anyone have any suggestions or have been through this?
Thank you.
Looking at your description, the problem is that your instances believe they're accessed over HTTP (not HTTPS) and are redirecting to HTTPS.
This is not an ELB problem, the ELB just forwards the HTTP requests. It's also not an EC2 problem since EC2 doesn't operate on HTTP level.
The redirects are either caused by
- your nginx server
- your magento installation
Typically, ssl redirect behaviour is configured on web server level.
There are no redirects present in the nginx config snippet you shared. You didn't share the entire config file, so look for redirects or 301's or 302's in your nginx config file. Also investige other nginx config files and .htaccess files in your magento installation folder.
I'm not familiar with Magento, but Magento could also cause the http(s) redirects.
Make sure you set the 'outside' url (the one that is mapped to the ELB) as your base url, as this will be the base url from the end users' point of view.
On https://www.siteground.com/tutorials/magento/magento_ssl.htm, you can find instructions for configuring Magento for SSL. It might be worth the try to set the base URL to http:// and activate the 'secure urls' options.

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 using etags with gzipped content when proxying

I want to use nginx to be a proxy between clients and a set of my apps, the problem I encountered is:
I have an app(on different machine than nginx) that is having static content(images, css etc.)
That static content is gzipped
I configured nginx as follows:
location ^~ /static/ {
etag on;
expires 1d;
more_set_headers "Vary: Accept-Encoding";
location ~* \.(css|js)$ {
gzip_static on;
expires -1;
proxy_pass http://my_upstream;
}
proxy_pass http://my_upstream;
}
and I was expecting to have etags working for things like js and css but they are not. I'm supposing it's because that js and css files are not on the same machine as nginx and it's the problem that gzip_static on suppose to fix.
So basically my question is, is it possible to have it working that way? And if it is how to do it:)
According to this forum post, etag on is not compatible with a proxy_pass that may modify the response (as gzip_static does):
The ETag header used in it's strong from means that it must be
changed whenever bits of an entity are changed. This basically
means that ETag headers have to be removed by filters which change
a response content.
As suggested in that same post, passing through Last-Modified will usually be enough for clients to make conditional requests.

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;
...
}

ProxyPass GET parameters via image using Nginx

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.

Resources