[英]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. 我是nginx的新手,我被要求找到一种方法来提供根据缩放级别分离的Map Tiles。 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. 图像文件结构类似于
~/data/images/7/65/70.png
,其中7是缩放级别,65和70是lon-lat值。 The folder 65 contains many files such as 71.png, 72.png and etc. 文件夹65包含许多文件,例如71.png,72.png等。
I have installed Nginx properly and I can get Welcome to nginx
message. 我已经正确安装了Nginx,我可以获得
Welcome to nginx
消息。 I have followed the instructions in http://nginx.org/en/docs/beginners_guide.html
and created the /data/www
and /data/images
directories. 我已按照
http://nginx.org/en/docs/beginners_guide.html
的说明操作,并创建了/data/www
和/data/images
目录。 I have placed index.html file under /data/www
and tile images under /data/images
. 我已将index.html文件放在
/data/www
并将图像放在/data/images
。 Then I modified the configuration file by adding following lines in http tags: 然后我通过在http标签中添加以下行来修改配置文件:
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. 重新加载配置文件并在浏览器上输入localhost后,我既无法获取index.html文件,也无法查看图像。
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? 我错过了什么?
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, 好吧,让我解释一下,你已经有了一个localhost服务器,它在一个名为
default
的文件中定义,该文件导致“欢迎使用nginx”或者出现的东西,我相信你不能创建一个新的服务器使用相同的server_name
,让我们删除它并使您的localhost只提供那些图像,
default
file from sites-enabled
, it will still exist inside sites-available
if you ever want to get it back. sites-enabled
删除default
文件,如果您想要将其恢复,它仍将存在于sites-available
内部。 ( note that all files inside sites-enabled
are simply symlinks from the files inside sites-available
) sites-enabled
所有文件都只是来自sites-available
内文件的符号链接sites-available
) sites-available
and call it whatever you want, images-app
for example sites-available
内创建一个新文件sites-available
随意调用它,例如images-app
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. 在
images-app
文件中创建新服务器,我假设应用程序的根目录在名为/data
的文件夹中,当然您将把它映射到您自己的服务器结构。
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
现在我们转到
sites-enabled
并启用我们在sites-available
内创建的sites-available
sudo ln -s /etc/nginx/sites-available/images-app /etc/nginx/sites-enabled/
make sure that all the nginx config are correct 确保所有nginx配置都正确
sudo nginx -t
If nothing is wrong we can go ahead and reload nginx settings 如果没有错,我们可以继续并重新加载nginx设置
sudo service nginx reload
I'm also new to nginx, Here is my solution that is similar with Mohammad AbuShady's answer : 我也是nginx的新手,这是我的解决方案,与Mohammad AbuShady的答案类似:
sites-enabled/default
sites-enabled/default
whatever.conf
in /etc/nginx/conf.d/
/etc/nginx/conf.d/
创建whatever.conf
The reason is: 原因是:
sites-enabled/default
has defined a server sites-enabled/default
定义了一个服务器 that is listening on 80 rooting with /var/www/html 那是用/ var / www / html监听80
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;
}
}
nginx.conf
file includes other conf files nginx.conf
文件包含其他conf文件 include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
the 1st edition of my.conf is root on /home/scotv/, but will get 403 Forbidden error, check the error.log: my.conf的第1版是root / home / scotv /,但是会出现403 Forbidden错误,请检查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"
For my case I just edited /etc/nginx/sites-enabled/default
file. 对于我的情况,我刚刚编辑了
/etc/nginx/sites-enabled/default
文件。
I added following config: 我添加了以下配置:
location /images/ {
root /data;
}
and placed images under /data/images
: 并在
/data/images
下放置/data/images
:
and url works: http://localhost/images/example.png 和网址有效: http://localhost/images/example.png
I use VS Code as SuperUser. 我使用VS Code作为SuperUser。 (I know it is bad, but I accept risks) It helps a lot with root access file editing:
(我知道这很糟糕,但我接受风险)它对root访问文件编辑有很大帮助:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.