How to serve images with nginx

I am completely new to nginx, and I am asked to find a way to serve Map Tiles, which are divided according to zoom levels. The structure of the image file is similar to ~/data/images/7/65/70.png , where 7 is the zoom level, 65 and 70 are the lon-lat values. Folder 65 contains many files, such as 71.png, 72.png, etc.

I installed Nginx correctly and I can get a Welcome to nginx message. I followed the instructions at http://nginx.org/en/docs/beginners_guide.html and created the directories /data/www and /data/images . I placed the index.html file under the /data/www symbol and tile in the /data/images section. Then I changed the configuration file by adding the following lines to the http tags:

 server { location / { root /data/www; } location /images/ { root /data; } } 

After reloading the configuration file and entering localhost in the browser, I can neither get the index.html file nor see the images.

What I'm trying to do is show the image when I enter something like:

 http://localhost/1.0.0/basemap/7/65/70.png 
  • 7: folder with 7th zoom level
  • 65: folder with latitude
  • 70.png: file indicating the longitude (folder 65 includes many png files)

What am I missing?

+7
nginx configuration configuration-files
source share
2 answers

Ok, let me explain something, you already have a localhost server that is defined inside a file called default , which is a file that calls β€œWelcome to nginx” or something appears, and I believe t create a new server with the same server_name , allow to remove this and make your localhost serve only those images

  • First we need to remove the default file from sites-enabled , it will still exist inside sites-available if you ever want to return it. (note that all files inside sites-enabled are just symbolic links from files inside sites-available )
  • We create a new file inside sites-available and call it anything, images-app for example
  • create a new server inside the images-app file, I assume that the root of the application is inside the folder with the name /data , of course, you will draw this on 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 that we created inside sites-available

     sudo ln -s /etc/nginx/sites-available/images-app /etc/nginx/sites-enabled/ 
  • make sure all nginx settings are correct.

     sudo nginx -t 
  • If nothing happened, we can continue and reload nginx settings

     sudo service nginx reload 
+9
source share

I am also new to nginx. Here is my solution, similar to the answer of Mohammad Abu Shadi:

  • remove sites-enabled/default
  • create whatever.conf in /etc/nginx/conf.d/

The reason is this:

sites-enabled/default defined server

which listens for 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 contains other conf files

 include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; 

pay attention to resolution

1st edition my.conf is root on / home / scotv / but will get 403 Forbidden error, check 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" 
+2
source share

All Articles