nginx入门

安装

# tar zxvf nginx-1.10.0.tar.gz
# cd nginx-1.10.0/
# ./configure --prefix=/usr/local/nginx-1.10.0 \ 
              --user=nobody                    \
			  --group=nobody                   \
			  --without-select_module          \
			  --without-poll_module            \
			  --with-http_ssl_module
# make
# make install 


注:

  • --without-select_module  --without-poll_module:因为在linux下,有更高效的epoll可用,所以禁用select和poll。
  • --user=nobody --group=nobody:一个非特权的用户。worker process将使用这个用户。


启动、停止与重新加载

# /usr/local/nginx-1.10.0/sbin/nginx
# ps -ef | grep nginx
root       8195      1  0 10:33 ?        00:00:00 nginx: master process /usr/local/nginx-1.10.0/sbin/nginx
root       8196   8195  0 10:33 ?        00:00:00 nginx: worker process

# /usr/local/nginx-1.10.0/sbin/nginx -s stop    	//fast shutdown

# /usr/local/nginx-1.10.0/sbin/nginx            
# /usr/local/nginx-1.10.0/sbin/nginx -s quit    	//graceful shutdown


# /usr/local/nginx-1.10.0/sbin/nginx
# /usr/local/nginx-1.10.0/sbin/nginx -s reload		//reloading the configuration file


配置文件结构

nginx包含一些模块,这些模块的行为由配置文件(通常是nginx.conf)中的指令(Directive)控制;指令分为简单指令(SimpleDirective)和块指令(BlockDirective)。
SimpleDirective := Name [Space-Separated-Param-List];
BlockDirective  := Name [Space-Separated-Param-List] { Directive-List }
Directive-List  := Null | SimpleDirective Directive-List | BlockDirective Directive-List

如果一个BlockDirective有内部Directive,那么它就是内部Directive的context; 在配置文件中,最外层的Directive的context是不可见的main context。例如:

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile      on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
        }
    }
}


worker_processes, worker_connections, include, default_type, sendfile, keepalive_timeout, listen, server_name, root, index是SimpleDirective;
events, http, server, location是BlockDirective;
location的context是server,server的context是http,http和events的context是main;


配置例1:服务静态内容

作为http服务器, nignx最基本的功能是服务静态内容(html页面、图片)。本例中,我们搭建这样一个服务器,它根据用户的请求,返回html页面(位于本地文件系统/data/www)或者图片(位于本地文件系统/data/images)。
为此,我们需要配置一个BlockDirective server,它将保护两个BlockDirective  location:一个提供html页面服务,另一个提供图片服务。用户请求的URI的最长匹配若为页面location,则返回html页面;若为图片location,则返回图片。

创建本地目录,对应两个location

mkdir -p /data/www
mkdir -p /data/images</span>


示例html页面和图片

# vim /data/www/index.html
<!DOCTYPE html>
<html lang="UTF-8">
    <head>
        <title>
            helloworld
        </title>
    </head>
    <body>
        Hello, Nginx!
    </body>
</html>

# cp basketball.jpg /data/images/


配置文件

# cp /usr/local/nginx-1.10.0/conf/nginx.conf /usr/local/nginx-1.10.0/conf/nginx.conf.bak
# vim /usr/local/nginx-1.10.0/conf/nginx.conf

user  nobody;
worker_processes  4;

events {
    worker_connections  1024;
}

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

    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;

        location / {
           root /data/www;
        }
        location /images/ {
           root /data;
        }
    }
}


测试

reload nginx

访问 http://192.168.75.138/index.html

访问 http://192.168.75.138/images/basketball.jpg

注:

  • 显然,在nginx.conf中,我们配置了一个server,它监听80端口,server name是localhost。有意思的是两个location,它们通过最长匹配的方式来定位用户请求的静态文件。例如:
  • 访问http://192.168.75.138/index.html时,URI是"/index.html"。它和location /是匹配的,匹配内容就是 "/",匹配长度为1;它和localtion /images/不匹配。所以由location /来定位用户请求的文件。
  • 访问http://192.168.75.138/images/basketball.jpg时,URI是"/images/basketball.jpg"。它和location /是匹配的,匹配内容是"/",匹配长度是1;它和localtion /images/也是匹配的,匹配内容是"/images/",匹配长度是8,为最长匹配。所以由location /images/来定位用户请求的文件。
  • 如何定位请求文件呢?最长匹配的location的root + URI。对于第一个,"/data/www" + "/index.html",即本地文件/data/www/index.html;对于第二个,"/data" + "/images/basketball.jpg",即本地文件/data/images/basketball.jpg。

配置例2:简单的代理服务器

在本例中,我们将配置两个http server,一个server作为另一个的代理。为了简单起见,这两个server运行在同一个nginx实例之内,只是端口不同。实际上,这两个server完全可以运行于不同主机的不同nginx之内。
  • 代理server(proxy server):       监听80端口,若用户请求的是图片(URI匹配"/images/"),则自己从本地文件系统提供服务。若为其他请求,转发给"被代理server";
  • 被代理server(proxied server):监听8080端口。它也使用本地文件系统(/data/upstream1)来提供静态服务。实际上,这个被代理server可以是其他服务器,例如动态页面服务器tomcat。

本地文件结构

# tree /data/
/data/
├── images
│   └── basketball.jpg
└── upstream1
    └── index.html

配置文件


worker_processes  4;

events {
    worker_connections  1024;
}

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

    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       8080;
        server_name  localhost-proxied;
        root /data/upstream1;

        location / {
        }
    }

    server {
        listen       80;
        server_name  localhost-proxy;

        location / {
            proxy_pass http://localhost:8080/;
        }
        location ~\.(gif|jpg|png)$ {
            root /data/images;
        }
    }
}


测试

reload nginx
访问http://192.168.75.138/index.html:    默认端口是80,所以请求发送至proxy server;由于最长匹配是location /,故转发至proxied server来服务;浏览器显示"Hello, Nginx!"
访问http://192.168.75.138/basketball.jpg:默认端口是80,所以请求发送至proxy server;由于最长匹配是location ~\.(gif|jpg|png)$,所以由proxy server服务,返回的文件是"/data/images" + "/basketball.jpg",即/data/images/basketball.jpg;浏览器显示该图片。



注:
  • 在proxied server中,location的root位于它的context中,这样能够工作的原因是"内部覆盖外部",也就是,若location定义了自己的root则使用自己的root,若没有定义,则使用最近外层的root;
  • 在proxy server中,location ~\.(gif|jpg|png)$的参数是一个正则表达式,它匹配所有以.gif, .jpg, .png结尾的URI。正则表达式以"~."开始(但是需要转意,故写为"~\.")。若一个URI和正则表达式匹配,匹配字符串相当于整个URI,故为最长匹配,由这个location提供服务。


配置例3:FastCGI代理

和配置例2类似,只是被代理服务是fastCGI
server {
    location / {
        fastcgi_pass  localhost:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param QUERY_STRING    $query_string;
    }

    location ~ \.(gif|jpg|png)$ {
        root /data/images;
    }
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值