How to serve images with nginx
Solution 1
Ok, let me explain something, you already have a localhost server, which is defined inside a file called default
that is the file that causes the "Welcome to nginx" or something to appear, and I believe you can't create a new server with the same server_name
, let's remove that and make your localhost serve only those images,
- First we need to delete the
default
file fromsites-enabled
, it will still exist insidesites-available
if you ever want to get it back. ( note that all files insidesites-enabled
are simply symlinks from the files insidesites-available
) - We create a new file inside
sites-available
and call it whatever you want,images-app
for example -
create the new server inside the
images-app
file, I'll assume that the root of the app is inside a folder called/data
of course you will map that to your own server structure.server { server_name localhost; root /data; index index.html; location / { try_files $uri =404; } }
-
now we go to
sites-enabled
and enable this site we created insidesites-available
sudo ln -s /etc/nginx/sites-available/images-app /etc/nginx/sites-enabled/
-
make sure that all the nginx config are correct
sudo nginx -t
-
If nothing is wrong we can go ahead and reload nginx settings
sudo service nginx reload
Solution 2
For my case I just edited /etc/nginx/sites-enabled/default
file.
I added following config:
location /images/ {
root /data;
}
and placed images under /data/images
:
and url works: http://localhost/images/example.png
I use VS Code as SuperUser. (I know it is bad, but I accept risks) It helps a lot with root access file editing:
Solution 3
I'm also new to nginx, Here is my solution that is similar with Mohammad AbuShady's answer :
- delete
sites-enabled/default
- create the
whatever.conf
in/etc/nginx/conf.d/
The reason is:
sites-enabled/default
has defined a server
that is listening on 80 rooting with /var/www/html
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
}
the nginx.conf
file includes other conf files
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
pay attention on permission
the 1st edition of my.conf is root on /home/scotv/, but will get 403 Forbidden error, check the error.log:
2016/04/07 20:12:44 [error] 12466#0: *2 open() "/home/scotv/data/a" failed (13: Permission denied),
client: 127.0.0.1, server: , request: "GET /a HTTP/1.1", host: "localhost"
Related videos on Youtube
Comments
-
iso_9001_ over 3 years
I am completely new to nginx and I am asked to find a way to serve Map Tiles that are separated according to the zoom levels. The image file structure is like
~/data/images/7/65/70.png
where 7 is the zoom level, 65 and 70 are the lon-lat values. The folder 65 contains many files such as 71.png, 72.png and etc.I have installed Nginx properly and I can get
Welcome to nginx
message. I have followed the instructions inhttp://nginx.org/en/docs/beginners_guide.html
and created the/data/www
and/data/images
directories. I have placed index.html file under/data/www
and tile images under/data/images
. Then I modified the configuration file by adding following lines in http tags:server { location / { root /data/www; } location /images/ { root /data; } }
After reloading the config file and entering localhost on the browser I can neither get the index.html file nor see the images.
What I am trying to do is to display the image when I enter something as:
http://localhost/1.0.0/basemap/7/65/70.png
- 7: folder indicating 7th zoom level
- 65: folder indicating the latitude
- 70.png: file indicating the longitude (folder 65 includes many png files)
What am I missing?
-
iso_9001_ over 10 yearsThis is very detailed and instructive. I had to delete my virtual machine due to disk error. I am now re-installing it and will try it when installation is done. Thank you very much
-
iso_9001_ over 10 yearsI have just had the chance to try this out and it worked like a charm. Thank you for the detailed answer and your time
-
Rohan Devaki about 2 yearscan i create image hosting service with ngnix? and custom domain ?