您的位置:首页 > 运维架构 > Nginx

nginx的启动、访问和配置

2016-01-07 14:09 447 查看
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://11.8.8.85: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://11.8.8.85:9993;

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

}

location /images {

root /data/images/9996;

}

}

如下三种配置比较:

http://11.8.8.85: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://11.8.8.85: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://11.8.8.85: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://11.8.8.85:9996/images/test.jpg

server{

listen 9996;

server_name localhost;

location /{

root /data/www/9996;

proxy_pass http://11.8.8.85:9993;

# root /data/www/9995;

}

location /images {

root/data/images2;
}
location ~ \.(gif|jpg|png)$ { (会匹配这个location)
root/data/images;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: