Multiple projects in folders Nginx - 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

Related

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

Nginx location and try_files in subdirectory

I'm trying to configure nginx vhost for application and stucked.
App is in directory /site/verb
At this moment app have this kind of links and it is working:
http://example.com/verb/lt.php?some_args=some_args&some_args?some_args
What I need? I need add another link for my clients like below:
http://example.com/v/lt.php?some_args=some_args&some_args?some_args
It is only change from /verb to /v but I want to handle both (for compatibility reasons) with all arguments after .php extension.
Is it possible in nginx config? (I want to avoid creating symlinks in directory).
I tried symlinks but it is not good solution.

Nginx Server unable to access index.html when the file is inside two subdirectories

I have a project folder. I can access my index.html directly but cannot access it from when its location is under 2 sub-directories. Is there any depth limitations.
Eg: My nginx root directory is: /home/user/projects
I can access the file directly
localhost/index.html
I can access it by
localhost/abc/index.html
But I cant access it from more depth
localhost/abc/assets/index.html
Please give a solution
Had the same problem. Replacing '\' to '/' works for me. So my root was C:\Projects\node, now it is C:/Projects/node

Applying NGINX location based on extension of default file.

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?

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