Applying NGINX location based on extension of default file. - nginx

I'm trying to create an alternate location in NGINX that will only fire for a specific file type. Specifically, I have NGINX acting as a proxy for a server that primarily serves PHP files. There are, however, a bunch of folders that also have ASPX files (more than 120), and I need to use a different configuration when serving them (different caching rules, different Modsecurity configuration, etc).
NGINX is successfully detecting the file type and applying the alternate location when the file name is specifically listed, but it's breaking when the ASPX file is the default file in the folder and the URL simply ends in a slash. When that happens, it's just applying the root location configuration. Is there a way to detect the extension of an index file and apply an alternate location, even when the name of the index file isn't specifically entered?
server {
listen 80;
server_name mysite.com;
# serves php files and everything else
location / {
#general settings applicable to most files
proxy_pass http://#backend;
}
#serves .Net files
location ~* \.(aspx|asmx) {
#slightly different settings applicable to .Net files
proxy_pass http://#backend;
}
}
If a folder has a file called "default.aspx" configured as it's index, the above configuration works perfectly if I enter the url as mysite.com/folder/default.aspx, but it applies only the base location if I enter it as mysite.com/folder, even though it is serving the exact same default.aspx file.
The only solution I've found is to alter the location directive to identify by the folder name instead of the file extension, but this doesn't scale well as there are more than 120 affected folders on the server and I'd end up with a huge conf file.
Is there any way to specify a location by file extension, when the file isn't specifically named in the URL? Can I test a folders index file to determine its extension before a location is applied?

Related

Static not existing though being declared in nginx default config

I'm trying to get my static files accessible so that I can load them with Flask.
ex: https://example.com/static/render.css
I've included this in the https part of my site:
location /static/ {
autoindex on;
alias /root/site/static;
}
But it just returns a 403 always.
The directory is correct and all, I've also ran the chown -R /root/site/static/ command. Idk what's the issue.
Nginx is definitely doing something with the data though, as all other extensions just reply a badgateway.
I decided to just write my CSS inline.

How to serve static files like javascript css images and pdfs in production environment django+static+nginx

1.My environment is Redhat 7, Django 2.2, nginx version: nginx/1.15.12.
I have two static locations and I need to be served at both times. The first location in on the same server and second location is a mounted network drive where store images and all other pdfs.
There are two problems.
1. How to serve both static locations.
The two locations are
a)/www/AutomationServices/static
b)/storage/Investigator_Validator.
In nginx conf i tried
location /static/ {
autoindex on;
root /www/AutomationServices/static;
try_files $uri $uri/ #secondStatic;
}
location /storage/ {
root /storage/Investigator_Validator;
}
location #secondStatic{
root /storage/Investigator_Validator;
}
I have multiple folders under /storage/Investigator_Validator. In each subfolder there are pdfs,images and text files grouped according to their classification. When I tried to serve them its not working. But When I tried python manage.py collectstatic the folders and files in the /storage/Investigator_Validator are copied to this folder /www/AutomationServices/static. It worked that time but the files in /storage/Investigator_Validator changes dynamically so its not good to run python manage.py collectstatic everytime when i create those images.
Need to serve two static locations using nginx webserver. How to search subfolders in folders for js,css,images files using nginx.
So the URI /static/foo/bar.css may be found in /www/AutomationServices/static/foo/bar.css or /storage/Investigator_Validator/foo/bar.css. The latter not including /static/ as part of the pathname.
There are a number of ways to achieve this, one being to use a regular expression location to capture the tail end of the URI. The root is set to a common root directory (in this case /) and try_files is used to test each path in turn.
For example:
location ~ ^/static/(.*)$ {
root /;
try_files /www/AutomationServices/static/$1 /storage/Investigator_Validator/$1 =404;
}

Multiple projects in folders Nginx

I'm trying to configure nginx to handle different projects inside a folder.
I have the foler "www" which has a lot of subfolders, each one of them being an independent project, in which the .html and other files have relative routes to the files in the project.
For example if you go to www.example.com/project1 I want the index.html file located in www/project1/index.html to be served. project2 should serve www/project2/index.html and so on. The problem is that each on of this html files have relatives routes to images and subfolders of the projects and the problem is that I don't know how to handle that in Nginx.
Also when visiting www.example.com/ a different folder should be served, for example www/main/index.html (also with relative routes).
How can I achieve this properly in Nginx? I have been reading documentation and I can serve the .html properly but the images and relative paths are all 404.
Thank you
You may want to configure a location per project like this
location /project1 {
root /<yourPath>/www/project1;
index index.html;
}
location /project2 {
root /<yourPath>/www/project2;
index index.html;
}
Respective docs:
http://nginx.org/en/docs/http/ngx_http_core_module.html#location
http://nginx.org/en/docs/http/ngx_http_core_module.html#root
http://nginx.org/en/docs/http/ngx_http_index_module.html#index

How to get nginx.conf include directive to ignore .bak files

How do I get the include directory for ngnix conf files to ignore *.bak?
I back up my configuration files, for example default -> default.bak. I want to include /etc/nginx/sites-enabled/default but not /etc/nginx/sites-enabled/default.bak in nginx.conf. How do I do this?
I would recommend changing default to default.conf or another extension if possible , then include can properly differentiate from config and bak.
http {
include /etc/nginx/sites-enabled/*.conf;
}

In nginx, how to make the request http://domain/test to index file in the root dictionary instead of making a test folder

I ask this questiion because I may misunderstand the location directive in Ngnix
location /test {
root /home/myfolder/html/
index index.html
}
I alwasy thought the /test is the URI that doesn't need to be a real folder in the root folder
in this case, the browser gives me a 404, after I add the test folder in the root folder, then it works.
How can I do it the right way without having many real folder that corresponding to the uri hiracy?

Resources