As the title tells I would like to use a directory as my root on my Nginx server.
I have a directory tree something like this:
/www
folder A
Folder B
Folder C
And I would like to configure Nginx so that the root / should be i folder A (example.com/ would be in the folder A). But if a user types example.com/FolderB/ that user should jump up one directory and get to the folder B. And the same for folder C.
I've tried to use locations, alias and root to change where the user is but I don't get it to work proper.
Set the root of the site to /www.
Then add a try_files to the root location:
root /www;
location / {
try_files $uri $uri/ /A/$uri /A/$uri/ =404;
}
Related
I have an NGINX server block that is serving files from demos.example.com - within this folder, i have a bunch of directories, each with a dist folder within them. I want to make it so if i want to view my example1 demo, i would visit demos.example.com/example1 and it would serve its root from /var/www/demos.example.com/example1/dist/. I have tried using 'alias' within individual specific location blocks but doesn't seem to be the correct way to do this - any tips on getting my files to serve from the /dist directory of each path?
So basically, my file structure is like this
example1
- /dist
- index.html
- /src
example2
- /dist
- index.html
- /src
I want to be able to navigate to /example1, but have its index file served from /dist folder.
I have tried to set specific location blocks with the specific directories name and set alias/root, but even that didn't work - I preferably would like it so i could just create a new directory, build to /dist and then automatically be able to navigate to that directories name and be served /dist/index.html.
Heres my current server block
server {
listen 80;
listen [::]:80;
root /var/www/demos.example.com/html/demos;
index index.html index.htm index.nginx-debian.html;
server_name demos.example.com www.demos.example.com;
location /example1 {
# I had also tried 'alias' to no prevail
root /var/www/demos.example.com/html/demos/example1/dist;
index index.html index.php;
try_files $uri $uri/ =404;
}
location / {
try_files $uri $uri/ =404;
}
}
Any help on this would be appreciated. Thank you
I'm quite new to Nginx so I might be misunderstanding of what try_files can do.
For my local development set up I have multiple installations that will each be accesible via their own subdomain. These installations are being migrated into a new folder structure but I still want to have the ability to support both at the same time. When pulled via git the new full path looks like this :
/home/tom/git/project/v3/[installation]/public/
The old structure goes 1 directory deeper namely as follows:
/home/tom/git/project/v3/[installation]/workspace/public
Where installation is variable according to the installation name and the /public folder will be the root for nginx to work from.
The root is determined by the subdomain and is extracted via regex like so:
server_name ~^(?<subdomain>[^.]+)\.local\.project\.test;
So far I've managed to get all this working for one of the folder structures but not both at the same time. My Nginx configuration for this local domain looks like this. Below is what I've tried but just can't seem to get working. As soon as I pass the #workspace named location as fallback for try_files it always defaults to 404.
index index.html index.htm index.nginx-debian.html index.php;
server_name ~^(?<subdomain>[^.]+)\.local\.project\.test;
root /home/tom/git/project/v3/$subdomain/public/;
location / {
try_files $uri #workspace =404;
}
location #workspace {
root /home/tom/git/project/v3/$subdomain/workspace/public/;
try_files $uri =404;
}
I have also tried shortening the root and passing the following parameters to try_files
root /home/tom/git/project/v3/$subdomain;
location / {
try_files /public/$uri /workspace/public/$uri =404;
}
But this still defaults to a 404, with a $uri/ as a third parameter there it will emit a 403 forbidden trying to list the directory index of the root.
I hope someone can provide some advice or an alternative as to how to approach this issue I am facing. If I need to provide additional data let me know,
Thanks in advance.
The named location must be the last element of a try_files statement.
For example:
location / {
try_files $uri #workspace;
}
location #workspace {
...
}
See this document for details.
The $uri variable includes a leading /, so your constructed pathnames contain a // which may be why they fail.
For example:
location / {
root /home/tom/git/project/v3/$subdomain;
try_files /public$uri /workspace/public$uri =404;
}
I want to create a Preview URL for my project. Each User has some directorys (home directory) on the drive, which contains a folder named /docs/.
When the user calls their URL (for example https://preview.example.com/microsoft/VisualStudioCode):
http(s)://preview.example.com/<company>/<project>/
How i can tell nginx, that he should internal append the /docs to the URL?
Following Directory structure is given:
- /opt
- /users
- <company>
- <project>
- <project>
- /microsoft
- /VisualStudioCode
- /docs
- /downloads
- /temp
- config.json
The DocumentRoot of nginx is by default /opt/users/ and the <company>/<project> exists as a real directory.
How i can create an Alias or another configuration to navigate to the /docs directory, if the user opens the URL?
Assuming the rest of your nginx.conf is set up correctly you'd have to add this server block:
server {
server_name preview.example.com;
root /opt/users;
location / {
try_files $uri/docs =404;
}
}
If you want to serve ANY file from within the secret docs directory then you're gonna have to do regex in your location block, like so:
location ~ ^/(?<company>.+)/(?<project>.+)/(?<filepath>.+)$ {
try_files /$company/$project/docs/$filepath =404;
}
Issue
I installed Nginx with brew on Mac OSX. Then I modified /usr/local/etc/nginx/nginx.conf as below and got a server 500 error:
server {
listen 666;
server_name localhost;
root /Users/username/Desktop/workspace/projectname/dist;
location / {
index index.html;
try_files $uri $uri/ /index.html;
}
}
What I tried
I knew that brew install default root is /usr/local/var/www so I was able to get it working by paste dist folder into /usr/local/var/www and update Nginx config like this:
server {
listen 666;
server_name localhost;
root /usr/local/var/www/dist;
location / {
index index.html;
try_files $uri $uri/ /index.html;
}
}
Other issue
Even after I paste dist folder into /usr/local/var/www I still got 500 error by updating root like the followings:
root /dist
root dist
root ./dist
Why does default
root index.html
gets displayed when I first run Nginx ? but I have to specify my dist folder with full path like /usr/local/var/www/dist ? Can someone please explain.
Goal
I want to be able to change root directory to my dist folder in workspace so I don't need to paste the dist folder into /usr/local/var/www every time after I rebuild.
I'm going to assume permissions is the problem. nginx runs as user "www-data" unless you change it. Your directories and files need to allow that user and have their directories set as 755 and the files set to 644.
With Apache the directive DirectoryIndex index along with DefaultType application/x-httpd-php within a particular vhost worked quite well to exclude a file extension from index files without rewriting. How can I duplicate this in Nginx? So far all I've been able to find is regex rewriting solutions.
The .conf file would look something like this:
server {
server_name example.com;
# Set the docroot directly in the server
root /var/www;
# Allow index.php or index.html as directory index files
index index;
# See if a file or directory was requested first. If not, try the request as a php file.
location / {
try_files $uri $uri/;
}
}
the line try_files $uri should try the files without extensions on the backend