nginx的启动、访问和配置

2、nginx的启动、访问和配置

1、

nginx-s signal
Where signal may be one of thefollowing:

  • stop — fast shutdown
  • quit — graceful shutdown
  • reload — reloading the configuration file
  • reopen — reopening the log files

关闭:

kill -s QUIT pid号

 

源文档 <http://nginx.org/en/docs/beginners_guide.html#control>

 

2.访问地址:

默认启动端口是80;这里选择9996作为测试端口:

http://1.1.1.1:9996/

3、配置 (配置文件更改后要在sbin/nginx目录下运行./nginx -sreload )使改动生效。

Serving Static Content

在nginx.conf配置文件中http后的{  }中可以配置虚拟server(一个或多个)。

server{

    listen    9996

    server_name    localhost

   location / {         /*这里是路径前缀,这个前缀和request中的uri进行匹配,如果有多个location均匹配,则选择最长的那个*/
        root /data/www;            //这是绝对路径加上location的相对路径构成资源的绝对路径。

    }

location/images/ {
        root /data;
    }
}

在上述配置中:如果访问http://localhost:9996/images/test.jpg   虽然两个location都和/images匹配,但选择最长的那个 就是第二个:location /images/

匹配后的资源路径是 location中的root路径加上request中请求的资源路径。即:/data/images/test.jpg

 

Setting Up a Simple Proxy Server

Oneof the frequent uses of nginx is setting it up as a proxy server, which means aserver that receives requests, passes them to the proxied servers, retrievesresponses from them, and sends them to the clients.

一个server中可以配置多个location;当root目录配置在server中时,如果location中有root目录,就用自己的,如果没有就用server中的。

server{

        listen 9993;

        root /data/up1;

 

        location /{

 

        }

      location /image {

      root /data/up2                  //这个root会覆盖server中的root。

    }

}

server{

        listen 9996;

        server_name localhost;

        location /{

                proxy_pass http://1.1.1.1:9993;

                root /data/www/9993;            //此处的root是无效的。因为proxy_pass跳转后不会执行之后的配置。

        }

        location /images {

                root /data/images/9996;

        }

   }

如下三种配置比较:

http://1.1.1.1:9996/index.html

//可以访问到/data/www/9993/index.html资源

 server{

        listen 9996;

        server_name localhost;

        location /{

                root /data/www/9993;

        }

   }

 

//无法访问到/data/www/9993/index.html资源

 server{

        listen 9993;

        root /data/up1/9993;

 

        location /{

                root /data/www/9993;

        }

   }

 

server{

        listen 9996;

        server_name localhost;

        location /{

                root /data/www/9993;

              proxy_pass http://1.1.1.1:9993;

#               root /data/www/9993;

        }

        location /images {

                root /data/images/9996;

        }

 

   }

server{

        listen 9996;

        server_name localhost;

        location /{

                root /data/www/9996;(当存在proxy_pass时,他之前的root 不会生效)

       #       proxy_pass http://1.1.1.1:9993;(此处若同时配置root和proxy_pass则优先匹配proxy_pass)

#               root /data/www/9995;(当代理地址找不到资源时,会继续在此root地址中寻找,若仍找不到,则404)

        }

        location /images {

                root /data/images/9996;

        }

   }

 

location支持正则表达式:优先匹配正则表达式

例如在/data/images/images/test.jpg

/data/images2/images/test.jpg中都有资源。

request:http://1.1.1.1:9996/images/test.jpg

server{

        listen 9996;

        server_name localhost;

        location /{

        root /data/www/9996;

                proxy_pass http://1.1.1.1:9993;

#               root /data/www/9995;

        }

        location /images {

                root/data/images2;

        }

       location ~ \.(gif|jpg|png)$ {      (会匹配这个location)

                root/data/images;

        }

   }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值