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 in http://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
What am I missing?
Configure NGINX and NGINX Plus to serve static content, with type-specific root directories, checks for file existence, and performance optimizations.
Host a Simple HTML Website on NGINX As for Red Hat, as indicated on the NGINX test page, the default website root is /usr/share/nginx/html and this is where you should put your website content. Once you are in the default website root, run the command below to rename any existing index. html file.
The try_file directive is in the server and location blocks and specifies the files and directories in which Nginx should check for files if the request to the specified location is received. A typical try_files directive syntax is as: location / { try_files $uri $uri/ /default/index.html; }
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,
default
file from sites-enabled
, it will still exist inside sites-available
if you ever want to get it back. ( note that all files inside sites-enabled
are simply symlinks from the files inside sites-available
)
sites-available
and call it whatever you want, images-app
for examplecreate 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 inside sites-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
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With