使用nginx提供静态内容服务


本文摘自PHP中文网,作者(*-*)浩,侵删。

一个重要的Web服务器任务是提供文件(如图像或静态HTML页面)。

根据请求,文件将从不同的本地目录提供:/data/www(可能包含HTML文件)和/ data/images(包含图像)。这将需要编辑配置文件,并使用两个位置块在http块内设置服务器块。 ( 推荐学习:nginx使用 )

首先,创建/data/www目录,并将一个包含任何文本内容的index.html文件放入其中,并创建/data/images目录并在其中放置一些图像。创建两个目录 -

1

2

3

[root@localhost ~]# mkdir -p /data/www

[root@localhost ~]# mkdir -p /data/images

[root@localhost ~]#

分别在上面创建的两个目录中放入两个文件:/data/www/index.html 和 /data/images/logo.png,/data/www/index.html文件的内容就一行,如下 -

1

<h2> New Static WebSite Demo.</h2>

接下来,打开配置文件(/usr/local/nginx/conf/nginx.conf)。 默认的配置文件已经包含了服务器块的几个示例,大部分是注释掉的。 现在注释掉所有这样的块,并启动一个新的服务器块:

1

2

3

4

http {

    server {

    }

}

通常,配置文件可以包括服务器监听的端口和服务器名称区分的几个server块。当nginx决定哪个服务器处理请求后,它会根据服务器块内部定义的location指令的参数测试请求头中指定的URI。

将以下location块添加到服务器(server)块:

1

2

3

4

5

6

7

http {

    server {

        location / {

            root /data/www;

        }

    }

}

该location块指定与请求中的URI相比较的“/”前缀。 对于匹配请求,URI将被添加到root指令中指定的路径(即/data/www),以形成本地文件系统上所请求文件的路径。 如果有几个匹配的location块,nginx将选择具有最长前缀来匹配location块。 上面的location块提供最短的前缀长度为1,因此只有当所有其他location块不能提供匹配时,才会使用该块。

接下来,添加第二个location块:

1

2

3

4

5

6

7

8

9

10

http {

    server {

        location / {

            root /data/www;

        }

        location /images/ {

            root /data;

        }

    }

}

它将是以/images/(位置/也匹配这样的请求,但具有较短前缀,也就是“/images/”比“/”长)的请求来匹配。

server块的最终配置应如下所示:

1

2

3

4

5

6

7

8

server {

    location / {

        root /data/www;

    }

    location /images/ {

        root /data;

    }

}

这已经是一个在标准端口80上侦听并且可以在本地机器上访问的服务器( http://localhost/ )的工作配置。 响应以/images/开头的URI的请求,服务器将从/data/images目录发送文件。 例如,响应http://localhost/images/logo.png请求,nginx将发送服务上的/data/images/logo.png文件。 如果文件不存在,nginx将发送一个指示404错误的响应。 不以/images/开头的URI的请求将映射到/data/www目录。 例如,响应http://localhost/about/example.html请求时,nginx将发送/data/www/about/example.html文件。

要应用新配置,如果尚未启动nginx或者通过执行以下命令将重载信号发送到nginx的主进程:

1

2

3

4

[root@localhost ~]# /usr/local/nginx/sbin/nginx -t

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload

如果错误或异常导致无法正常工作,可以尝试查看目录/usr/local/nginx/logs或/var/log/nginx中的access.log和error.log文件中查找原因。

打开浏览器或使用CURL访问Nginx服务器如下所示 -

nginx-43.png

完整的nginx.conf文件配置内容如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

#user  nobody;

worker_processes  1;

#error_log  logs/error.log;

#error_log  logs/error.log  notice;

#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {

    worker_connections  1024;

}

http {

    include       mime.types;

    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '

    # '$status $body_bytes_sent "$http_referer" '

    # '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;

    #tcp_nopush     on;

    #keepalive_timeout  0;

    keepalive_timeout  65;

    #gzip  on;

    ## 新服务(静态网站)

    server {

        location / {

            root /data/www;

        }

        location /images/ {

            root /data;

        }

    }

}

以上就是使用nginx提供静态内容服务的详细内容,更多文章请关注木庄网络博客

相关阅读 >>

nginx和haproxy的区别

ribbon和nginx的区别

nginx配置文件详解

linux下nginx的启动与重启方法

nginx关闭/重启/启动方法介绍

nginx是否在启动

nginx怎么发音

怎么在linux系统中以单容器安装nginx+asp.net core

nginx如何优化

linux配置nginx伪静态

更多相关阅读请进入《nginx》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...