nginx cannot find js files under the assets folder - http

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.

Related

How to route the custom URL path using nginx server?

I am building the angular app and want to route the URL to http://localhost/sample/AngularApp/. but don't want to give the entire URL in the browser. I will give the only localhost. In angular, while building the dist we are using the command ng build --base-href=/sample/AngularApp/ and created the folder structure /sample/AngularApp in Nginx mentioned path /usr/share/nginx/html.so while accessing the application still, we are giving the entire path in browser. so, How could I resolve this issue and how to configure that path in nginx.conf file to autoroute the URL?
I would go with standard configuration:
server {
listen 80;
listen [::]:80;
root /var/www/html/YourAngularApp/dist;
index index.html index.htm;
location / {
try_files $uri $uri/ /index.html;
}
}
This gives opportunity to serve other files (if found) or fall back to index.html located at the project's root.
Now, if you have everything in place you should build your project with
ng build --base-href=/
--base-href=/ can be omitted as it defaults to /. Please have a look at Angular docs about using --base-href and --deploy-url

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 how to serve pictures or media files?

I'm having issues serving pictures with nginx. I originally started with a Django project and I wanted to serve the user uploaded media files via nginx but I wasn't able to get it working no matter what I tried.
So, I've make a second temporary droplet/server and am trying a bare bones setup with no Django project, just Nginx, to see if I can get it to simply serve an index and a couple pictures in a folder called 'media'. Here is the server block:
server {
listen 80;
listen [::]:80;
root /var/www/example.com/html;
index index.html;
server_name 159.89.141.121;
location / {
try_files $uri $uri/ =404;
}
location /media/ {
root /var/www/example.com/media;
}
}
Now, the index.html is served fine but if I try to go to 159.89.141.121/media/testpic.jpg it still doesn't work and returns a 404 error. I'm at a complete loss here, I've tried using alias instead of root, I've tried changing the folder structure and setting all permissions to 777 and changing folder and file ownership, permissions shouldn't be a problem because the index.html works fine with the same permissions; I just cant seem to figure out what I'm doing wrong. The picture is in the folder but nothing I try allows me to access it via the uri. Are there any obvious problems with my server block?
Well I decided to read the documentation and realized that the location block adds to the root directory specified..
So the pathing of
`location /media/ {
root /var/www/example.com/media;
}`
would end up routing example.com/media/testpic.jpg to /var/www/example.com/media/media/testpic.jpg
I've changed the location block to look like this
location /images/ {
root /var/www/example.com/media;
}
and it will now route example.com/images/testpic.jpg to /var/www/example.com/media/images/testpic.jpg
I'm not sure why it didn't work when I tried the alias directive, though...

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.

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.

Resources