Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to serve images with nginx

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
  • 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?

like image 380
iso_9001_ Avatar asked Sep 23 '13 08:09

iso_9001_


People also ask

Can Nginx be used for serving static content?

Configure NGINX and NGINX Plus to serve static content, with type-specific root directories, checks for file existence, and performance optimizations.

How do I host with nginx?

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.

What is Try_files in nginx?

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


2 Answers

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 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 )
  • 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 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
    
like image 176
Mohammad AbuShady Avatar answered Sep 26 '22 12:09

Mohammad AbuShady


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:

enter image description here

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:

enter image description here

like image 35
Teoman shipahi Avatar answered Sep 23 '22 12:09

Teoman shipahi