how to have NGINX serve specific directory within a path? - nginx

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

Related

How to deploy Next.js static export with Nginx? (deep links not working)

I made a next.js export into the out folder.
Folder structure is:
out
index.html
terms.html
privacy.html
I set up nginx to serve files from this folder:
server {
root /var/www/myproject/out;
index index.html index.htm index.nginx-debian.html;
server_name myproject.com;
location / {
try_files $uri $uri/ /index.html;
}
}
The main page (index) opens fine. Navigation from within the app to urls like myproject.com/privacy works fine. The problem is if I try to open these links directly, it will serve the main page (index) instead of the actual pages, since those urls don't exist in the folder. The only way to open the privacy page directly is adding the html extension to the url: myproject.com/privacy.html.
How to configure nginx to serve the actual page myproject.com/privacy.html when someone enters the myproject.com/privacy url?
Issue is in try_files.
As current configuration includes:
/ (which default route to index.html at root path)
index.html
/index.html
test/*.html
To access pages route path name without extension i.e., /privacy that format should be included in try_files insdie location /
Try this:
try_files $uri $uri.html /$uri /index.html
I needed a 2nd location block because of the way NextJS does ids in the url. NextJS will have files like [id].html or whatever.
location / {
try_files $uri $uri.html /$uri /index.html;
}
location ~* /(.*)(\d+)$ {
try_files $1/[id].html /$1/[id].html /index.html;
}
So I needed the 2nd block to catch urls of the form /whatever/etc/5 and redirect nginx to /whatever/etc/[id].html

How do I correctly use try_files when looking in two different directories for files to serve?

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

Change Nginx root directory to workspace folder path

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.

NGINX try_files does not find the correct folder

I've been trying to get a host set up for my personal portfolio site, and I'm just not understanding why NGINX isn't serving up my files as I expect. My root directory, /usr/local/var/apps, holds symlinks to the latest versions of my web apps, including the main site. I've tried dozens of different configurations, but I think this is the best so far:
server {
listen 8080;
server_name mysupertestsite.com;
index index.html;
root /usr/local/var/apps/;
location / {
try_files portfolio-page$uri portfolio-page$uri/ $uri $uri/ =404;
}
}
I want http://mysupertestsite.com:8080 to serve up the page under /usr/local/var/apps/portfolio-page/, and my web app names in the path to serve up their respective files, i.e. http://mysupertestsite.com:8080/wikipedia-viewer should serve up /usr/local/var/apps/wikipedia-viewer. Currently, the apps do work. The main portfolio page does not, and I'm getting a 403.
I'm not married to this configuration or directory structure. Any help would be very much appreciated.

Removing index extension in nginx

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

Resources