I. Nginx简介

1.1 Nginx概述

Nginx (engine x) 是一个高性能的HTTP反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务。Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рамблер)开发的,第一个公开版本0.1.0发布于2004年10月4日。
其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。2011年6月1日,nginx 1.0.4发布。
Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,在BSD-like 协议下发行。其特点是占有内存少并发能力强,事实上nginx的并发能力在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。
nginx官网

1.2 Nginx作为Web服务器

Nginx可以作为静态页面的Web服务器,同时还支持CGI协议的动态语言,比如perl、php等。但不支持Java。Nginx专门为性能优化而开发,性能是其最重要的考量,实现上非常注重效率。有报告表明,Nginx能支持高达50000个并发连接数。

1.3 Nginx功能

  • 正向代理
  • 反向代理
  • 负载均衡
  • 动静分离
  • 高可用

II. Nginx的安装

2.1 依赖安装

  1. yum -y install gcc zlib zlib-devel pcre pcre-devel openssl openssl-devel

2.2 安装Nginx

2.2.1 官网安装

  1. 官网下载压缩包

http://nginx.org/en/download.html

  1. 解压缩

    tar -zxvf nginx-xx.tar.gz ./
    
  2. 进入解压出的目录,执行configure

    cd ./nginx-xx
    ./configure
    
  3. 编译并安装

    make && make install
    

    2.2.2 yum安装

    yum -y install nginx
    

    2.2.3 brew安装

    brew install nginx
    

    III. Nginx命令

    3.1 nginx脚本

  • 所在目录:

nginx/sbin/nginx

  • 启动nginx:
    cd nginx/sbin
    ./nginx
    ps -ef | grep nginx            # 查看nginx进程
    

    3.1.1 nginx常用命令

    nginx -V                    # 查看nginx版本号(-v)
    nginx -s start        # 启动nginx
    nginx -s stop            # 停止nginx
    nginx -s start        # 重启nginx
    nginx -s reload        # 重新加载配置文件
    

IV. Nginx配置

4.1 配置文件

  • 所在位置:

nginx/conf/nginx.conf

  • 内容 ```shell

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 {
    listen       8080;
    server_name  localhost;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

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

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}


# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
#    listen       8000;
#    listen       somename:8080;
#    server_name  somename  alias  another.alias;

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


# HTTPS server
#
#server {
#    listen       443 ssl;
#    server_name  localhost;

#    ssl_certificate      cert.pem;
#    ssl_certificate_key  cert.key;

#    ssl_session_cache    shared:SSL:1m;
#    ssl_session_timeout  5m;

#    ssl_ciphers  HIGH:!aNULL:!MD5;
#    ssl_prefer_server_ciphers  on;

#    location / {
#        root   html;
#        index  index.html index.htm;
#    }
#}
include servers/*;

}

<a name="ommwp"></a>
## 4.2 Nginx配置
Nginx配置文件由三部分组成:全局块、events块以及http块。
<a name="M2FnV"></a>
### 4.2.1 全局块
从配置文件开始到events块之间的内容成为全局块,主要设置一些影响nginx服务器整体运行的配置命令,主要包括配置运行Nginx服务器的用户(组)、允许生成的worker process数、进程PID存放路径、日志存放路径与类型以及配置文件的引入等。
```shell
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;

4.2.2 events块

events块涉及的指令主要影响Nginx服务器与用户的网络连接,常用的设置包括是杏开启对多worker_processes下的网络连接进行序列化,是否允许同时接收多个网络连接,选取哪种事件驱动模型来处理连接请求,每个worker_processes可以同时支持的最大连接数等。这部分的配置对 Nginx的性能影响较大,在实际中应该灵活配置。

events {
    worker_connections  1024;            # 支持的最大连接数
}

4.2.3 http块

这是 Nginx服务器配置中最频繫的部分,代理、缓存和日志定义等绝大多数功能和第三方模块的配置都在这里。需要注意的是:http块也可以包括http全局块与server块。http全局块配置的指令包括文件引入MIME-TYPE定义、日志自定义、连接超时时间、单链接请求数上限等。

⑴ 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块:

    server {
        listen       8000;
        listen       somename:8080;
        server_name  somename  alias  another.alias;

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

4.2.4 location匹配路径规则

⑴ location语法

符号 含义
= = 开头表示精确匹配
^~ ^~开头表示uri以某个常规字符串开头,理解为匹配 url路径即可。nginx不对url做编码,因此请求为/static/20%/aa,可以被规则^~ /static/ /aa匹配到(注意是空格)
~ ~ 开头表示区分大小写的正则匹配
~* ~* 开头表示不区分大小写的正则匹配
!~和!~* !~和!~*分别为区分大小写不匹配及不区分大小写不匹配的正则
/ 用户所使用的代理(一般为浏览器)
$http_x_forwarded_for 可以记录客户端IP,通过代理服务器来记录客户端的ip地址
$http_referer 可以记录用户是从哪个链接访问过来的

1)示例
  • 匹配任意请求

    location  / {
          # ...
    }
    
  • 匹配以 gif, jpg, or jpeg结尾的请求.

    location ~* .(gif|jpg|jpeg)$ {
           #     ...
    }
    
  • 区分大小写匹配以.txt结尾的请求,并设置此location的路径是/usr/local/nginx/html/。也就是以.txt结尾的请求将访问/usr/local/nginx/html/ 路径下的txt文件

    ⑵ alias与root

    | root | 实际访问文件路径会拼接URL中的路径 | | —- | —- | | alias | 实际访问文件路径不会拼接URL中的路径 |

1)示例
  • 请求:http://test.com/sta/sta1.html,实际访问:/usr/local/nginx/html/static/sta1.html 文件

    location ^~ /sta/ {  
     alias /usr/local/nginx/html/static/;  
    }
    
  • 请求:http://test.com/tea/tea1.html,实际访问:/usr/local/nginx/html/tea/tea1.html 文件

    location ^~ /tea/ {  
     root /usr/local/nginx/html/;  
    }