Change Nginx root directory to workspace folder path - nginx

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.

Related

how to have NGINX serve specific directory within a path?

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

nginx cannot find js files under the assets folder

I have a compiled Angular application located on my local disk.
Trying to run it on nginx server.
Added the following configuration:
server{
listen 8080;
.......
location /myapp/ {
root C:/myapp/;
try_files $uri$args $uri$args/ =404;
}
index.html file in project root contains references to js files, located in myapp/assets/js folder:
<script src="/assets/js/myscript.js"></script>
But when I run it on server, I get index.html file, it opens, it tries to locate file in http://127.0.0.1:8080/assets/js/polyfill.min.js, avoiding root "myapp", which leads to 404 error.
Adding additional location "location /assets/" doesn't solve the problem.
Could anyone help me with this?
Thanks.

Want to load same name file from Folder nginx server

I am new to nginx server. I want to load same name file from folder. Suppose:
I have file system as:
folder/xyz/xyz.html
I am want to access xyz.html file using this url: http://localhost:84/xyz using nginx server. I have created the configuration like as:
server {
rewrite_log on;
listen :84;
server_name localhost;
root folder;
location / {
try_files $uri $uri/index.html /index.html;
}
}
With this configuration I am successfully accessible the folder/xyz/index.html file. But I want to access folder/xyz/xyz.html file.
NOTE: name ("xyz") will be dynamically.
Can any please help me.

Nginx: use a directory as root /

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

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