I have a location in my nginx configuration :
location ~ \.(js|jpg|png|css|gif|ico|svg|jpeg)$ {
root /some/basic/root;
}
It works for static files in one directory /some/basic/root. But I have some other static files like css and js in other directories :
root /completely/other/root;
root /completely/other/root/dir1;
root /completely/other/root/dir2;
How to make nginx check also other directories?
If there is a common parent directory, you can use try_files to test each subdirectory in turn:
location ~ \.(js|css)$ {
root /common/parent;
try_files /basic/root$uri /other/root$uri /other/root/dir1$uri /other/root/dir2$uri =404;
}
If a common parent directory is not practical, you can cascade location blocks, again using try_files:
location ~ \.(js|css)$ {
root /some/basic/root;
try_files $uri #other;
}
location #other {
root /completely/other/root;
try_files $uri /dir1$uri /dir2$uri =404;
}
See this document for details.
Related
i am trying to use same location path in nginx.
location = / {
root /usr/share/nginx/html/main;
try_files $uri /index.html;
}
location / {
root /usr/share/nginx/html/app;
try_files $uri /index.html;
}
but it is rendering app folder.
i am trying to achieve rendering 'main' folder html file in case of '/' path and 'app' folder html file in case of any other path.
You can't use the same location, you need to use different location for different folder. Even if you see "try_files" which use multiples points, "location" is different.
However, you can use multiple location like media in "/media" and "/" for html content or other.
Example
location /media {
root /usr/share/nginx/html/app;
try_files $uri /index.html;
}
location / {
root /usr/share/nginx/html/main;
try_files $uri /index.html;
}
I have the following nginx configuration:
server {
root /home/user/www/public;
location / {
try_files $uri #proxy;
}
location #proxy {
# my custom app...
}
}
This configuration works well: if I have a static file on the /public directory, it's served. If the file does not exist, by proxified backend takes the request.
Now, I have some directories from public (for example public/cache) that I want to serve from another directory (ex: /datadrive/www-public).
I have edited try_files as :
try_files /datadrive/www-public/$uri $uri #proxy;
But it doesn't work. (fallback to #proxy).
In Nginx, I try to make the uploads directory outside the root in yii2, but it doesn't accessible
server_name teracourses.com;
set $host_path /var/www/html/teracourses;
root $host_path/frontend/web;
location / {
try_files $uri $uri/ /$yii_bootstrap?$args;
}
location /uploads/ {
alias /var/www/html/teracourses/uploads/;
}
Try the following way:
location /uploads {
root /var/www/html/teracourses/uploads/;
}
it should do the job
I am struggling to get nginx conf to work the way we need it.
Basically on the same domain we have many apps, each one in root folder. As the user installs apps it is not possible to know the name of the folders.
location / {
try_files $uri $uri/ /index.php?$args /index.php?q=$uri&$args;
}
location /myfiles {
try_files $uri $uri/ /myfiles/index.php?$args /myfiles index.php?q=$uri&$args;
}
If I specify the second folder, it makes app in myfiles work, URLs are resolving properly. If I do not then the main app tries to resolve the URL and it fails.
So I would like to have something like:
location /* {
try_files $uri $uri/ /$folderrequested/index.php?$args /$folderrequested/index.php?q=$uri&$args;
}
where * would be any root folder, for example myfiles, mycrm, myaccount, which would route the trafic to that folder.
Any suggestions and ideas welcome!
Put all your app root directories in a parent directory.
server {
listen .....;
server_name ....;
root /path/to/apps;
index index.php index.html;
location / {
}
location ~ \.php {
fastcgi_pass localhost:8000;
}
}
Bingo.
I have a two locations where my app will serve static files, one is /my/path/project/static and the other is /my/path/project/jsutils/static.
I'm having a hard time getting the webserver to look in both directories for static content. Here is my entry for static location in the nginx configuration file for my app.
location ^~ /static {
root /my/path/project/static;
alias /my/path/project/jsutils/static;
index index.html index.htm;
}
I get an error that says : "alias" directive is duplicate, "root" directive was specified earlier.
I'm not sure how to go about having nginx look in both these paths for static content.
Thank you in advance for any help.
location ^~ /static {
root /my/path/project/static;
index index.html index.htm;
try_files $uri $uri/ #secondStatic;
}
location #secondStatic {
root /my/path/project/jsutils/static;
}
So first the file will be searched in /my/path/project/static and if that could not be found there, the secondStatic location will be triggered where the root is changed to /my/path/project/jsutils/static.
You may use try_files (http://wiki.nginx.org/HttpCoreModule#try_files). Assuming that you static files are in /my/path/project/static and /my/path/project/jsutils/static. you can try this:
location ^~ /static {
root /my/path/project;
index index.html index.htm;
try_files $uri $uri/ /jsutils$uri /jsutils$uri/ =404;
}
Let me know if it works. Thanks!
Just implement your configuration in nginx language:
location /my/path/project/static {
try_files $uri =404;
}
location /my/path/project/jsutils/static {
try_files $uri =404;
}
I had the exact same problem and it looks like nginx doesn't like when root is overwritten by an alias. I fixed it by firstly removing the root declaration that was inside the server section and instead declared the root and alias appropriately directly in the location sections (note the commented out lines):
server {
# root /usr/share/nginx/html;
location /logs/ {
root /home/user/develop/app_test;
autoindex on;
}
location /logs2/ {
# root /home/user/branches/app_test;
alias /home/user/branches/app_test/logs/;
autoindex on;
}
}