Nginx pointing to the static directory
I can’t figure out where I am going wrong here. I am trying to point Nginx to my static directory for the CSS and JS.
The true location of my static folder is as follows:
/var/www/site/app/static
/var/www/site/app/static/js
/var/www/site/app/static/css
In my app_nginx.conf I have this:
location /static {
root /var/www/site/app/static;
index index.html;
}
When I go to my site it is looking for the content in this folder:
http://localhost/static/css/main.css
Where am I going wrong here?
You should be able to use something similar to the following.
root /var/www/site/app/static;
location / {
index index.html;
}
Then you should be able to use the following url to hit your content
http://localhost/css/main.css
I corrected this, you shouldnt include the static folder in the root directive, it should have been like this:
root /var/www/site/app;
Related
I don't understand why my nginx.conf file does not work.
This is just supposed to serve a static web site.
Later I want to add a wordpress to /blog.
Currently I want to make the main static site work.
The related conf is this:
location / {
index index.html
root /var/www/html/xlanding;
try_files $uri $uri/ =406;
}
This nginx is dockerized. I log in to the docker container and I can confirm that /var/www/html/xlanding/ exists and there is an index.html file there.
When I visit the url I see a 406 error (just changed from 404 to confirm that block is entered). So the block is matched.
How can I fix this?
Ah, I missed a semicolon after the "index index.html".
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...
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.
I have a very simple case of serving static files via nginx yet I can't figure it out.
I want all URLs beginning with /static/ to serve static files from directory /foo/bar/dir1 and if the file isn't there, serve from /foo/bar/dir2, if not there, return 404.
So for example when handling URL /static/some/file.png I want nginx to first try
/foo/bar/dir1/some/file.png
and then
/foo/bar/dir2/some/file.png
I know I should probably use something like this
location /static/ {
try_files .... something .....
}
but the documentation on try_files is very unclear to me. I tried a lot of combinations but nothing seems to work. Multiple alias directives would do the job but it won't work. I think the solution must be very simple but I cant get it right. It's kind of hard to debug how nginx resolves all these locations and files...
You can customize the root (make sure to update the try_files after). And also make sure there is no root directive in location /
location ~* ^/static/(.+)$ {
root /;
try_files /foo/bar/dir1/some/$1 /foo/bar/dir2/some/$1 =404;
}
Edit: Removed the need of the static folder.
I am setting up nginx as a sort of static file server. For some reason it is only working when I go to 123.123.123.123/ or 123.123.123.123. However, when I go to 123.123.123.123/static/content/ or 123.123.123.123/static/content/another.mp3 it returns a 404 not found. Here is the config file that is located in /etc/nginx/sites-available and linked to /etc/nginx/sites-enabled. I am really stumped as to why it is not working.
Any pointers or tips would be appreciated.
server {
listen 123.123.123.123:80;
server_name "";
location / {
root /srv/homepage;
index index.html;
}
location /static/content/ {
root /srv/static/content;
index song.mp3;
}
}
Look into logs.
Seems like nginx tries to open paths like /srv/static/content/static/content/file.mp3
You need to rewrite url here, try this:
rewrite /static/content/(.*) /$1 break;