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;
}
}
Related
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 directory/files structure such as:
root/
a/
utils.js
b/
assets/
styles.css
app.js
index.html
And I want to configure nginx to serve files from a directory directly if exist and have single page app in directory b (if file in path exists the it wil be served directly, nd if not the fallback will end up at index.htm file.
For example:
myapp.com/a/utils.js will return that file.
myapp.com/b/ or myapp.com/b/foo will display index.html
myapp.com/b/assets/style.css will return directly css file
I tries multiple different configurations and non had worke so far. For exampe the simplest:
server {
listen 80;
root /root;
index index.html;
location / {
try_files $uri $uri/ /index.html =404;
}
}
I also tries something to serve different directories:
server {
listen 80;
root /root;
index index.html;
location /a {
try_files $uri $uri/ =404;
}
location /b {
try_files $uri $uri/ /index.html =404;
}
}
I tried to define different roots as well:
server {
listen 80;
index index.html;
location /a {
root /root/a;
try_files $uri $uri/ =404;
}
location /b {
root /root/b;
try_files $uri $uri/ /index.html =404;
}
}
Nginx seems to ignore existing files and ends up returning 404 page at all times. When I try to access soe existing file directly it gets redirected to / (root) url regardless.
The last parameter of a try_files statement is the default action. There can only be one. Many of your examples have two. See this document for details.
The correct URI for your index.html file is /b/index.html which is what you need to use for the default action of the try_files statement.
This should meet your requirements:
location / {
try_files $uri $uri/ /b/index.html;
}
You do not state what should happen with the URI /a/foo. In the above case, it would also return index.html. If you need it to return a 404 response, you would use:
location / {
try_files $uri $uri/ =404;
}
location /b {
try_files $uri $uri/ /b/index.html;
}
See this document for more.
I have this folder structure:
/document/root/
|-- main
`-- wishlist
I want to get my nginx to work like this: If I point my browser to example.com/wishlist it will display the index.html on the wishlist folder. If I point my browser to example.com, I want it to fallback to main/index.html (and, of course, related main/style.css and other files that are in the main directory).
I don't want to write a location rule for every folder I have under my root, so I want this to be as generic as possible. I have found this questtion and it has helped me to get most of the job done, but there's something not working: if I point the browser to wishlist/index.html it works perfectly. But if I remove the index.html and point it just to example.com/wishlist the browser will return a 404. My current Nginx config is below. Can someone point me in the right direction? Thanks.
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
root /document/root/main;
location ~ ^/([^/]+)(/.+)?$ {
if (!-d "$document_root/$1") {
return 404;
}
try_files /$1$2 /main$2 =404;
}
}
All you need to do for the index file is:
index index.html
location / {
try_files $uri.html $uri/index.html =404;
}
location /wishlist {
try_files $uri.html $uri/index.html =404;
}
Turns out I found out a way that worked for me: using a custom #location on nginx. My final piece of code turned out something like this:
location / {
root /document/root/main;
index index.html;
try_files $uri $uri/ index.html;
}
location ~ ^/(.+)$ {
root /document/root;
index index.html;
try_files $uri $uri/ index.html #main;
}
location #main {
try_files /main/$uri /main/$uri/;
}
Now example.com uses /document/root/main as it's root and example.com/wishlist uses /document/root/wishlist :) Hope this helps someone else.
Keep it simple:
server {
root /document/root/main/;
index index.html;
location /wishlist {
root /document/root/;
}
}
This is my nginx configuration file:
server {
listen 80;
server_name localhost;
location / {
root d:/www;
index index.html index.htm;
}
location /js/api/ {
root D:/workspace/javascript/maplib/;
autoindex on;
}
}
And the directory of the document is like this:
D:/workspace/javascript/maplib
-- v1.0
--main.js
-- v1.1
Now I want to access the v1.0/main.js by http://localhost/js/api/v1.0/main.js.
And it returns a 404 error.
It seems that ngnix will tried to get the file through D:/workspace/javascript/maplib/js/api/v1.0/main.js which does not exist.
It seems that the string path in the location(in the url) must exist at the file system.
How to fix it to meet my requirement?
BTW, there is not only the js but also some other kinds of files like .gif,.png,.html inside the D:/workspace/javascript/maplib/.
Use alias. Ref: http://nginx.org/en/docs/http/ngx_http_core_module.html#alias
That is, replace
root D:/workspace/javascript/maplib/;
by
alias D:/workspace/javascript/maplib/;
Use rewrite inside location /js/api/ for this, like:
rewrite ^/js/api(.*)$ $1;
You can use root with try_files, just add the try_files line
location /js/api/ {
root D:/workspace/javascript/maplib/;
autoindex on;
try_files $uri $uri/ =404;
}
I'm looking to serve the root url of a subdomain and directory of a subdomain to two different folders on my server. Here is the simple set-up that I have and is not working...
server {
index index.html index.htm;
server_name test.example.com;
location / {
root /web/test.example.com/www;
}
location /static {
root /web/test.example.com/static;
}
}
In this example going to test.example.com/ would bring the index file in /web/test.example.com/www
and going to test.example.com/static would bring the index file in /web/test.example.com/static
You need to use the alias directive for location /static:
server {
index index.html;
server_name test.example.com;
root /web/test.example.com/www;
location /static/ {
alias /web/test.example.com/static/;
}
}
The nginx wiki explains the difference between root and alias better than I can:
Note that it may look similar to the root directive at first sight, but the document root doesn't change, just the file system path used for the request. The location part of the request is dropped in the request Nginx issues.
Note that root and alias handle trailing slashes differently.
The Location directive system is
Like you want to forward all request which start /static and your data present in /var/www/static
So a simple method is separated last folder from full path , that means
Full path : /var/www/static
Last Path : /static and First path : /var/www
location <lastPath> {
root <FirstPath>;
}
So lets see what you did mistake and what is your solutions
Your Mistake :
location /static {
root /web/test.example.com/static;
}
Your Solutions :
location /static {
root /web/test.example.com;
}
server {
index index.html index.htm;
server_name test.example.com;
location / {
root /web/test.example.com/www;
}
location /static {
root /web/test.example.com;
}
}
https://nginx.org/en/docs/http/ngx_http_core_module.html#root
A little more elaborate example.
Setup: You have a website at example.com and you have a web app at example.com/webapp
...
server {
listen 443 ssl;
server_name example.com;
root /usr/share/nginx/html/website_dir;
index index.html index.htm;
try_files $uri $uri/ /index.html;
location /webapp/ {
alias /usr/share/nginx/html/webapp_dir/;
index index.html index.htm;
try_files $uri $uri/ /webapp/index.html;
}
}
...
I've named webapp_dir and website_dir on purpose. If you have matching names and folders you can use the root directive.
This setup works and is tested with Docker.
NB!!! Be careful with the slashes. Put them exactly as in the example.
If you use this, I will suggest you set up this command too.
location /static/ {
proxy_set_header Host $host/static; // if you change the directory and the browser can't find your path
alias /web/test.example.com/static/;
}
If you want to check two different directories for the same URI use this config:
server {
...
root /var/www/my-site/public/;
...
index index.php index.html index.htm;
...
location / {
root /var/www/old-site/dist/;
try_files $uri $uri/ /index.php$is_args$args;
}
...
}
If Nginx couldn't find file in /var/www/old-site/dist/ directory, then it will try file in /var/www/my-site/public/ directory, but as we said to Nginx to try files with $uri $uri/ /index.php$is_args$args patterns, so Nginx will try /index.php$is_args$args in /var/www/my-site/public/ directory. not $uri
If you want to complete your fallthrough, then replace /index.php$is_args$args with /fallthrough$uri and then add the location /fallthrough { ... } with the alias key to your target directory.
https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/#root-inside-location-block