Set nginx root based on existance of another directory - nginx

I'm building a system which uses dynamic DNS for user accounts, so you register for a sub domain.
I have a few server directives in place to catch things like api. and www. and other special cases, and I have a directive which reads the wildcard domain name and uses it to set a domain specific assets location.
server {
server_name "~^(?<domain>[a-z0-9]+)\.example\.com$";
root /sites/core;
location /assets {
alias /site-assets/$domain;
}
}
What I want to do is check for the existence of the assets directory and present, on the same domain, a different site.
To clarify, if the assets directory in the example above exists, serve one set of files, otherwise, serve another. Since the site I want to serve won't be IN the directory in question, I can't use try_files or anything like that.
I read all the horror stories about using the if directive but I think I need something like
if (-d /site-assets/$domain) {
... Do something
}
Then change the root, but that doesn't work.
Any ideas how I can achieve this?

OK. I figured this out. If there's a problem with it, let me know.
I have the following in a server directive
server {
server_name "~^(?<domain>[a-z0-9]+)\.example\.com$";
set $site core;
if (!-d /path/to/assets/$domain) {
set $site join;
}
root /path/to/sites/$site;
}
If the assets directory for the given domain name does NOT exist, then I can serve up a joining page for my site, which can pick up the domain name and allow the user to register. If it does exist, then serve up the main app.
This seems to me like a simple use of the if directive so shouldn't get me in too much trouble.

Related

Nginx Config - Case Insensitive = 404

Newby at Nginx on Ubuntu. Main site is a WordPress site "example.com". No issue with this site. However, I need (cause someone did things in IIS and we need to move it to Nginx) to have a another site at "example.com/testsite" (not Wordpress, just static HTML).
Since this "testsite" was originally in IIS, sloppy coding was done and things are a complete mess of upper and lowercase.
So, I added a location block to my /ect/nginx/sites-enabled/example.com.conf
location /testsite {
alias /home/myusername/public_html/testsite;
index index.html;
}
This works! However as soon as a try to make this location case insensitve, I get a 404.
location ~* /testsite {
alias /home/myusername/public_html/testsite;
index index.html;
}
Ideas? Also, this site has tons of links that users can click on that are a mixture of upper/lower/proper and in no relation to the actual files that are on the server. Is there a way to make anything under that location block case insensitive (aka IIS way of doing it).
Thanks everyone.

How to write rewrite rule for a directory and its content Nginx EC2

I am new to Webserver configurations etc.
My question is how can I write a rule for a root directory and all the content to that if someone tries directly a not authorised error appears.
eg in the picture prevent access to ajax and all thats contained
the config below will take care of this: (it is considered that the directory ajax is located in root directory you defined in your nginx config, otherwise you have to specify full address of your ajax directory relative to your root directory.)
location ~* /ajax/* {
return 403;
}

How to restrict access to a folder but still access a safe.html file in that folder in nginx?

Lets say there is a folder in /var/www/html/admin, so i want to restrict access while maintaining access to /var/www/html/admin/login.html in nginx. How could i achieve that?
Don't quite understand what do you want to achieve, what other content you have in that folder and who would have an access to that folder, but you can do something like
location = /admin/login.html {}
location /admin {
deny all;
}

Passing PNG file request to PHP scrpt in 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)

Nginx - Password Protect an entire website but keep one folder open

We're using Nginx on the server reserved for development (testing) and we want to prevent anyone outside the company from gaining access to the sites under development. However one of the sites uses online payment and for that a folder need to be accessible by anyone, used for the callback from the credit card company..
Is there any way we can protect an entire website but leave just one folder and all the files inside open ?
Regards,
Wael
server {
auth_basic "go away";
location /a {
auth_basic off;
}
}

Resources