Passing PNG file request to PHP scrpt in Nginx - nginx

Here is what I need to do - I need my server to serve up two versions of a set of images. The first set of images are real PNG files that sit in a folder on my server, call it /realimages. The second set of images do not actually exist. When they are requested I want to take the requested image file name e.g.
https://example.com/nosuchimage/img1.png
generate the image via the index.php script sitting inside the /nosuchmage folder (or elsewhere if easier).
That script does the following
Picks up a related image from the /realimages folder
Uses PHP GD to generate a background
Superimposes the related image on the background
serves up the result
That script has been written and is fully functional. What I do not know is how to force all PNG image requests to the /nosuchimage folder to be redirected to that script with the original request passed as a script parameterr.
I know that doing this a relatively trivial configuration issue with NGINX which involves writing a rewrite rule. However, NGINX rewriting is still quite a black art to me. Hopefully, someone here will be able to tell me just what that rewrite rule should be.

To redirect all the images of a certain folder, just use the code below in your configuration file.
In my case I will use the default file.
/etc/nginx/sites-available/default
location ~ \/realimages/(.+\.png)$ {
return 302 $scheme://$server_name/nosuchimage/$1;
}
// Access: http://www.example.com/images/valdeirpsr.png (Exists & Redirect to /nosuchimage/valdeirpsr.png)
// Access: http://www.example.com/images/valdeirpsr2.png (Not Exists & Redirect to /nosuchimage/valdeirpsr2.png)
To redirect only the images not found in a particular folder, just use the code below in your configuration file.
location ~ \/realimages/(.+\.png)$ {
error_page 404 $scheme://$server_name/nosuchimage/$1;
}
// Access: http://www.example.com/images/valdeirpsr.png (Exists & Show Image)
// Access: http://www.example.com/images/valdeirpsr2.png (Not Exists & Redirect to /nosuchimage/valdeirpsr2.png)

Related

Redirect a domain name to a specific page in NGINX and Next JS

I explain you my situation,
I have a website that we'll called website-a.com that is link to my personal project.
On this project, every users can have a page, with the url my-project.com/[username]. So the urls will then be my-project.com/user1, my-project.com/user2, ...
For the context, I'm building it in Next JS, with the following code architecture :
--
pages
-- [username]
-- index.tsx
-- about.tsx
--
And I'm using Nginx with pm2 and a proxy pass that points to my http://localhost:3000
Now, I'd like to redirect a second domain name to a specific page of a user.
So I would like that when I go to user1.com, it displays my-project.com/user1 without modifying the url.
I pointed the DNS of user1.com to my server, I created a new host nginx and I made a proxy_pass to http://localhost:3000/user1. It's working, but I don't have the ressources css and js, because next is trying to load them from user1.com and they don't exists (user1.com/css/abc.css, ...).
I also tried to modify the build-manifest.json in the .next folder created after the build and it didn't work
So either I would like to find a way for Next JS to take the resources from my-project.com even if it is on user1.com (let it be an absolute path), or put a configuration with the nginx host but I don't don't know which one.
Thanks you for your help

Drupal doesn't generate thumbnails

I don't know why Drupal stopped generating thumbnails. So, I get error on files like this:
/sites/default/files/styles/choices/public/a1531172504.jpg?itok=Wn-VWDKd
Although the full image is found in:
/sites/default/files/a1531172504.jpg
I'm working on Nginx, Drupal 7
Last time I've answers question related to image derivatives I've got minuses so let be thorough here.
1.Drupal core will NOT create image derivative upon image upload - fact.
2.Thumbnail image (derivative from original image) is physically created upon HTTP request eg. if you visit the listing where image is used (when you visit the listing with thumbnail image the derivative is created on the fly via HTTP request)
3.To alter this behavior and to get a derivative image instantly upon image upload via CCK field you must use module Imageinfo Cache https://www.drupal.org/project/imageinfo_cache
With this info form above please recheck your site (go to listing where the thumbnail should appear and than check your thumbnail folder) If the image is still missing please provide more details like: Are you using CCK field or is it a custom field, are you creating derivative programmatically? Some code snippets are required here to solve you problem.
Also please check your .htaccess located in sites/default/files/.htaccess (not the general .htaccess)
and while you there check the permissions on files folder.
The solution for me was to add this lines to the nginx.conf files:
location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
expires max;
log_not_found off;
try_files $uri $uri/ #rewrite;
}
location #rewrite {
# not working with Drupal rewrite module: rewrite ^/?(.*?)/?$ /index.php?q=$1&$args;
rewrite ^/?(.*?)/?$ /index.php/$1&$args;
}
My environment is:
- docker-compose with nginx (nginx:1.17.4-alpine), drupal
(drupal:9.0.6-fpm-alpine) and mysql (mysql:8.0)
- drupal (9.0.6), nginx/1.17.4, PHP 7.4.10, MySql 8.0.21
The server responded with a 404 error because:
The image styles are generated when drupal receives a request.
Nginx receives a request for an image that is not in the filesystem; then returns a 404 instead redirect the request to drupal.
The rule I propose for the nginx.conf redirect the image request to drupal and solve the problem after restart the web server:
docker-compose restart webserver
My fix was to disable and re-enable the clean-url. .../admin/config/search/clean-urls".

Cross origin framing Google VR

I'm trying to use the VR viewer from Google that you can find here:
https://developers.google.com/vr/concepts/vrview
Setup is pretty easy, no issue with this but I'm now having a cross origin framing issue on my own site. Here is an example:
http://laurentwillen.be/flat_files/vr.php
There I call the JS file and the JPG file from the same domain and same directory on the server but I still get this in my console:
Load denied by X-Frame-Options: http://www.laurentwillen.be/flat_files/index.html?image=http://www.laurentwillen.be/flat_files/VR/catane.jpg does not permit cross-origin framing.
I see here that the error refers to a html file that doesn't exist. Should I modify something in my htaccess file ?
Thanks
Laurent
The html file it is looking for ( index.html ) is part of the embed api. In the default setup for vrview, it is served from /
for example, here is the online demo root folder: http://googlevr.github.io/vrview/
(it will just show you an error atm, because you don't load it directly)
that directory (or some other you set up, i like to use /embed ) must serve that index page and its needed parts. (in /build and /images )
The examples are served from /examples, but the other directories are needed for the api to work.

What is the default file to be visited behind a website?

When I open a website's url such as www.stackoverflow.com via curl, which file is actually being visited in the server? I know usually it is index.html. But I cannot find such a convention in the RFC2616 document. How can I know it?
BR
The document dilivered by calling a website without a path in the URL is configured by the webserver. So you have no standard there. Is a users joice.
Curl will download the file the webserver is delivering him, or follow the redirect (if -L option is given) when webserver responses a redirect.
There is no way for the client to know how the data for the HTTP response was generated. It might not even be related to a specific file.
The last time I wrote a significant bit of server side code, everything outside of /static/ was routed (via mod_rewrite) though a FastCGI program that got its data from a few different controller libraries, a dozen database schema libraries, a database and a dozen template files.
The WWW is built on links between URLs, not files. Don't worry about files if you are writing client code.
It's not necessarily index.html, and you can't actually know that it could be anything depending on the Server Configuration, for instance in Apache you can change the directory index to the one that suits you
DirectoryIndex home.php
in this case the default file accessed is home.php
in IIS you can take a look about default index and how to change it
but the defaults are
in Apache
index.php (usually: depending on the server configuration)
index.html (is the default that comes with a fresh install)
in IIS
Default.htm
Default.asp
Index.htm
Index.html
Iisstart.htm

Referencing relative (parent) path with sudomain

I have a website with several subdomains that direct the user into a subfolder on my site. Inside each subfolder is a Default.aspx file which does some processing and then redirects the user to "../Default.aspx".
This works fine if you type the full URL to that page. If you try to access it through the subdomain, the ".." parent is not being parsed correctly, and just concatenates the subfolder path into the main path and I get a page not found.
The root path of my application is www.domain.com/root.
The subdomain points to subdomain.domain.com/root/subfolder
When I navigate to subdomain.domain.com, I get this error:
"404 - /root/subfolder/root/Default.aspx not found"
All I want is for subdomain.domain.com to redirect the user up one folder level to www.domain.com/root/Default.aspx
Can anyone help? Is this a feature/restriction of using a shared hosting provider - the subdomains are restricted to the folder where they are pointed?
Your description is a bit confusing, since you mix local paths and URLs together. Am I right that you are trying to do: Page at subdomain.domain.com/root/subfolder/Default.aspx redirects to www.domain.com/root/Default.aspx?
That means you want to do 2 things:
Redirect from sub-domain subdomain to sub-domain www, and
Navigate to a file one folder up.
Both things you can do in a single HTTP redirect. For this, use the Response.Redirect method, and make sure that in the URL you use the www sub-domain, and the correct absolute path to the page you want to show.
Response.Redirect("http://www.domain.com/root/Default.aspx");
Update
Or, redirect to a URL relative to the current page, in the same domain.
Response.Redirect( Page.ResolveClientUrl( "../Default.aspx" ) );
Update 2
Or, use the Host HTTP header to distinguish on subdomains and switch programatically in your shared codebase.
The answer is to point all the subdomains to the same folder (the main code base) and then in the Master file, switch based on the http header. If they are coming in from partner1.domain.com, use css1 - if they are from partner2.domain.com, use css2, etc.
This allows me to use relative paths throughout the code AND preserve the subdomain in the browser's URL bar.
One caveat - if you are testing in multiple environments (I have a DEV and a TST) you need some code to detect which environment you are in and operate a little differently, since the http header host would show something like "localhost:51510". For me, those subdomains only exist in my Production environment.
Credit to bgever - thanks!

Resources