Nginx(2)---搭建一个静态web服务

1.配置文件语法及参数说明:nginx.conf

worker_processes  1;  #工作进程多少个

events {
    worker_connections  1024; #连接数
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on; #资源从硬盘->内核态-应用态-网卡,开启之后:硬盘->内核态-网卡

    keepalive_timeout  65; #长连接超时时间

    #配置一个具体的站点server
    server {
        #端口
        listen 8079;
        #域名
        server_name www.bluedarkni.com;
        #站点资源根目录 server中配置则所有location共享
        root /website/test;    
        #站点资源位置
        location / {
            index index.html;
        }
        location /error {
            #alias   别名,匹配location的资源路径使用alias的值作为根
            alias /website/test;
            index error.html;
        }
    }    
}

 {}属于配置块,其他的是参数以及参数值,注意以;结尾。可以使用./nginx -t 查看配置文件语法是否正确

2.配置一个静态站点:

上诉配置文件中的http->server配置块及注释说明:端口8079,站点资源根目录/website/test,

3.创建几个静态资源文件

/website/test/index.html

/website/test/error.html

/website/test/other/aaa.html

/website/test/log/errorlog.html

4.启动nginx服务:./nginx

location的匹配优先级:精确匹配,正则匹配,前缀最大匹配,都一样就使用配置靠前的

1.http://192.168.0.67:8079/  匹配第一个location,访问/website/test/index.html资源

2.http://192.168.0.67:8079/other/aaa.html  匹配第一个location,将other/aaa.html拼接到根下面去,因此访问/website/test/other/aaa.html资源

3.http://192.168.0.67:8079/error/      匹配第二个location,访问 /website/test/error.html资源

4.http://192.168.0.67:8079/error/log/errorlog.html   匹配第二个location,按2的资源拼接来说应该是访问/website/test/error/log/errorlog.html,但是第二个location中我们配置alias,将到/error这部分直接换成alias的值/website/test,再去拼接log/errorlog.html,因此访问的是/website/test/log/errorlog.html资源

猜你喜欢

转载自www.cnblogs.com/nijunyang/p/12285174.html