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;
}
Related
I would like to serve an index file from a root folder and one from a subfolder from root.
My nginx server conf looks as follows:
server {
listen 80;
server_name localhost;
root /data/;
index index.html index.htm index.txt;
location / {
}
location /a {
alias /data/a/;
}
}
and my directory structure looks as follows:
/data/:
a index.txt
/data/a:
index.txt
If I then do curl localhost, I get the contents of the file /data/index.txt.
But curl /localhost/a gives me a 301.
curl localhost/a/index.txt works.
Why can't I access my index.txt with curl /localhost/a ?
I tried using a root instead of alias in the location /a block and also tried to specify the index.txt for location /a, but no success.
I see similar posts, e.g.
Nginx location configuration (subfolders)
but couldn't yet find the answer.
The index directive works with URIs that end with a /:
So the URI / gives you the contents of /index.txt and /a/ gives you the contents of `/a/index.txt.
If you provide Nginx with a URI of a directory (but without a trailing /), the default behaviour is to redirect to the same URI, but with a trailing /.
This is just how the index directive works. See this document for details.
If you want something other than default behaviour you will have to do it manually using try_files. See this document for details.
For example, to return the contents of an index.txt file by providing the URI of the directory without a trailing /, use:
root /data;
location / {
try_files $uri $uri/index.txt =404;
}
Note that the location /a { ... } block is not necessary in either this example, or the example in your question.
I have nginx configuration like this:
location /myweb/ {
#alias html/myweb/app/;
alias /Users/admin/Documents/Projects/myweb/app/;
index index.html;
}
When I place my project under my local folder(/Users/admin/Documents/Projects/myweb), 403 is returned when I visit with http://localhost/myweb, but if I place it into the html folder under nginx home folder, it works.
I even tried chmod -R 777 xxx, and start nginx with sudo. But it still returns 403.
Did I miss any settings for nginx?
The location /myweb/ does not match the URI /myweb. It is the fact that there is a directory called myweb under html that makes it work at the alternative root. But the simple solution is to fix the location and alias to match that URI, rather than just its descendants.
location /myweb {
alias /Users/admin/Documents/Projects/myweb/app;
index index.html;
}
Remove the trailing / from both the location and alias directives.
Id like to serve to different websites based on url user enter.
For example if user will go to
my-domain.pl/ the content will be served from desktop folder
my-domain.pl/desktop the content will be served from desktop folder
my-domain.pl/mobile the content will be served from mobile folder
root
|
|---mobile
| |--- assets
| |---js,css,img
|
|---desktop
|--- assets
|---js,css,img
I tried that in nginx setup file:
server {
root /desktop
location /mobile {
root /mobile
}
location /desktop{
root /desktop
}
}
but it works only for / path, remaining paths return 404
I tried to add try_files $uri index.html but it seems that it returns index.html files for all request for this location e.g. it returns index.html file instead javascript too.
I am absolutely new in setting up nginx so any help will be appreciated.
You need to make use of the alias directive instead of root: see documentation.
server {
root /;
location /desktop {
}
location /mobile {
alias /mobile;
}
}
(don't forget trailing semicolons)
You should avoid specifying root inside location blocks (cf. nginx pitfalls).
Have you tried the following:
have only one root
use rewrite to serve /desktop by default
The config would look like:
server {
root /;
## this should take care of the redirect to /desktop by default:
location = / {
rewrite ^ /desktop/ redirect;
}
## this one below probably doesn't work:
# rewrite ^/$ /desktop/;
}
P.S. I have no access to a machine with nginx right now so I'm not able to check the rewrite syntax. See also this answer.
On our server we have the /var/www/root/html/web/ directory that contains all the code, and a /var/www/root/html/web/front/ that contains our static frontend code.
Our frontend communicates with the code only via REST API calls, which have the /api/ prefix, so all the calls will be accessible via ourdomain.com/api/products/ , ourdomain.com/api/products/45 and so on. We also have an admin running there, on ourdomain.com/admin
When we want to see the actual frontend, we have to go to ourdomain.com/front in the browser, which is of course not what we want.
We have, among other stuff, this in our config:
root /var/www/html/web;
index index.php index.html;
location /front {
# some magic to make sure the /front folder will not be parsed
index nothing_will_match;
autoindex on;
}
However, what we wish is that if you go to ourdomain.com it will load /var/www/html/web/front/ folder as root, and if you go to ourdomain.com/api/* or ourdomain.com/admin/* it will load the /var/www/html/web/ as root. Is that possible?
NOTE: the /var/www/html/web/front/ folder can be moved somewhere else if needed, to /var/www/html/front/ for example
What you need is the alias directive (instead of root) to rewrite the URI:
root /var/www/html/web/front;
index index.php index.html;
location / {
}
location /api {
alias /var/www/html/web/;
}
location /admin {
alias /var/www/html/web/;
}
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;
}