nginx安装

centos7安装nginx - zhangyukun

nginx的常见命令

查看版本:nginx -v

启动:nginx

立即关闭:nginx -s stop

处理完请求后关闭:nginx -s quit

检查配置:nginx -t

输出配置:nginx -T

重新加载配置:nginx -s reload

下面是nginx的配置文件例子
#user  nobody;
worker_processes  1;                     #一般是1,可以和线程数量一样

#error_log  logs/error.log;              #异常日志位置,默认不开启
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;             #mater进程存放位置


events {
    worker_connections  1024;            #单个work-process允许的最大连接数
}


http {
    include       mime.types;        #这里包含的types块也可以包含http下面的块比如server等
    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;                      #linux的sendfile使用DMA技术传输文件,不用cpu参与
    #tcp_nopush     on;                      # 减少网络报文段的数量

    #keepalive_timeout  0;
    keepalive_timeout  65;                   # 保持连接的时间,也叫超时时间,单位秒

    #gzip  on;

    server {                                     #一个server就是一个虚拟主机
        listen       80;                         #虚拟主机端口
        server_name  localhost;                  #IP或者域名

        #charset koi8-r;

        #access_log  logs/host.access.log  main;  #每个虚拟主机可以独立设置日志位置

        location / {                            #location里面是虚拟主机映射的位置
            root   html;                        #根目录文件夹
            index  index.html index.htm;        #首页
            deny  all;                          #拒绝所有IP
            allow 192.168.1.6                   #只允许192.168.1.6
        }

        #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 {                   #精确匹配50x.html资源位置
            root   html;
        }



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

    #    ssl_certificate      cert.pem;              #指定https证书
    #    ssl_certificate_key  cert.key;              #指定https证书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;
    #    }
    #}

}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.

部分配置详解和示例

main配置

worker_processes

nginx工作时候work进程数量,nginx默认是一个master process,一个 work process,work process默认是1,可以设置为和cpu线程数想等,master接受请求,然后worker通过争抢的方式获取转发任务。每个worker是一个独立进程,他们可以部分重启(reload 的时候,不停机更新配置),如果单个worker异常退出不影响别的worker正常工作。

[root@localhost conf]# ps -ef|grep nginx
root       1432      1  0 01:07 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody     1433   1432  0 01:07 ?        00:00:00 nginx: worker process
root       1910   1885  0 19:15 pts/1    00:00:00 grep --color=auto nginx
  • 1.
  • 2.
  • 3.
  • 4.
worker_processes  auto;             # Nginx 进程数,一般设置为和 CPU核心数,有超线程的cpu是2倍
  • 1.

events配置

worker_connections

单个work porcess 可以允许同时建立外部连接的数量,默认1024,可以设置的很大

worker_connections的值和两个因素有关,一是内存,二是操作系统允许进程试用的最大文件数

  • 内存
    每个连接数分别对应一个read_event、一个write_event事件,一个连接数大概占用232字节,2个事件总占用96字节,那么一个连接总共占用328字节,通过数学公式可以算出100000个连接数大概会占用 31M = 100000 * 328 / 1024 / 1024,当然这只是nginx启动时,connections连接数所占用的nginx。
  • 进程最大可打开文件数
    进程最大可打开文件数受限于操作系统,可通过 ulimit -n 命令查询,以前是1024(centos 7 默认是 1024),现在是65535。
    nginx提供了worker_rlimit_nofile指令,这是除了ulimit的一种设置可用的描述符的方式。 该指令与使用ulimit对用户的设置是同样的效果。此指令的值将覆盖ulimit的值,如:worker_rlimit_nofile 20960。

nginx一个请求占用2个或者4个链接(如果是反向2代理就是4个,作为静态资源服务器就是2个)。

如果ngixn配置为worker_processes=1,worker_connections=1024,用它做反向代理服务器最多只能同时承受256个请求。

http配置

server配置例子
server {                                     #一个server就是一个虚拟主机
        listen       80;                     #监听端口
        server_name  localhost;              #设置域名

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {                            #location里面是虚拟主机映射的位置,/表示所有
            root   html;                        #静态文件目录
            index  index.html index.htm;        #设置首页文件名字
        }

        #error_page  404              /404.html;   #设置异常页面


    }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
location配置例举
location / {                            #location里面是虚拟主机映射的位置,/表示所有
            root   html;                        #静态文件目录
            index  index.html index.htm;         
            proxy_pass   http://127.0.0.1;      #proxy_pass表示转发
        }
        
        location /order {                       #固定 /order
            proxy_pass   http://127.0.0.1;      #proxy_pass表示转发道指定位置
        }
        
        location ~ /user {                       # /user开头的
            proxy_pass   http://127.0.0.1;      #proxy_pass表示转发道指定位置
        }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.

location 配置 proxy_pass 以后 root 无效。

location 转发规则
  • = 精确匹配
#只匹配 /log/log1 一个地址
location = /log/log1
  • 1.
  • 2.
  • 前缀匹配
  • / 通用匹配,任何请求都会匹配到
#表示所有的地址都接受
location /{
    proxy_pass http://192.168.1.6;
}


#匹配log开头的地址,考虑是文件和文件
location /log{
    proxy_pass http://192.168.1.6;
}

#匹配log开头的地址,只匹配是文件夹后面的
location /log/{
    proxy_pass http://192.168.1.6;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.

proxy-pass 地址后面的/:proxy_pass  http://192.168.100.6;地址后面没有 / 所以匹配部分会附加到转发地址后面,如果有/结尾那么匹配部分就不会附加到转发地址后面。

匹配地址后面的/: (/log )没有/,匹配的时候优先找按照目录查找,如果有就查询下面的index.html文件,如果没j有找/log文件。(/log/)有/,匹配的时候只会按照目录查找,不考虑/log文件。

  • ^~ 前缀匹配;和/的前缀匹配类似,感觉就是把 ^~ 省略了
location ^~ /log/{
    proxy_pass http://192.168.1.6;
}
  • 1.
  • 2.
  • 3.
  • 正则匹配
  • ~ 开头表示区分大小写的正则匹配
  • ~* 开头表示不区分大小写的正则匹配
  • !~!~*分别为区分大小写不匹配及不区分大小写不匹配的正则
location匹配优先级
  • 等号类型(=)的优先级最高。一旦匹配成功,则不再查找其他location的匹配项
  • ^~和通用匹配。使用前缀匹配,不支持正则表达式,如果有多个location匹配成功的话,不会终止匹配过程,前缀匹配会找到匹配表达式最长的那个
  • 如果前缀匹配找到就不会使用正则匹配。
  • 前缀匹配找不到才会使用正则匹配,继续匹配正则表达式,只要有一个正则成功,则使用这个正则的location,立即返回结果,并结束解析过程
location里面 root ,alias,proxy_pass区别

他们的区别在于要不要加上匹配的部分的地址

  • root 加匹配串命中部分
    如下配置,匹配部份是projectNameA,如果请求的projectNameA/a,那么实际访问的是html/projectNameA/a
    root是指定根目录,匹配部分作为根目录里面的子目录
location /projectNameA {                            
            root   html;                       
            index  index.html index.htm;         
        }
  • 1.
  • 2.
  • 3.
  • 4.
  • alias 不加匹配串命中部分
    如下配置,匹配部份是projectNameA,如果请求的projectNameA/a,那么实际访问的是html/a
    alias的是用匹配到的部分替换 alias指定的别名部分
location /projectNameA {                            
        alias   html;                       
        index  index.html index.htm;         
    }
  • 1.
  • 2.
  • 3.
  • 4.
  • proxy_pass适用于反向代理
  • proxy_pass 地址后面没有/,加匹配串命中部分,和root类似
location /projectNameA {                            
    alias   html;                       
    proxy_pass http://192.168.1.6;      
}
  • 1.
  • 2.
  • 3.
  • 4.

如下配置,匹配部份是projectNameA,,如果请求的projectNameA/a,那么实际访问的是http://192.168.1.6/projectNameA/a

  • proxy_pass 地址后面有/, 不加匹配串命中部分,和alias类似
location /projectNameA {                            
    alias   html;                       
    proxy_pass http://192.168.1.6/;      
}
  • 1.
  • 2.
  • 3.
  • 4.

如下配置,匹配部份是projectNameA,,如果请求的projectNameA/a,那么实际访问的是http://192.168.1.6/a

location三个位置的/
location /匹配串/ {           #位置1,匹配串 后面的斜杆                 
    alias   映射目录/;        #位置2,映射目录 后面的斜杆               
    proxy_pass 映射地址/;     #位置3,映射地址 后面的斜杆
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 匹配串的斜杆 如果不写可以精确匹配文件,如果写了只能是目录,除了精确匹配单个文件的时候,别的时候没区别
    建议不写
  • root,alias 映射目录后面的斜杆 ,这里的斜杆写不写几乎没区别,只有匹配串和映射目录都不写斜杆,并且在alias的情况有异常
#访问  http://ip/te/a.html,请求的是/usr/local/nginx/htmla.html
        location /te/ {
            alias  html;
            index  index.html index.htm;
            #proxy_pass http://192.168.1.6/;
        }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

建议写,因为alias时是直接替换,如果上面有斜杆,替换后没有地址就不对了。如果清楚alias的原理建议不写。

  • proxy_pass 地址后面的斜杆
    没有斜杆是子目录,有斜杆是替换,根据需要选择或者不写

检查匹配的时候可以参考nginx的logs目录下 access.log 和 error.log(路径不对的时候里面有真实访问的磁盘路径),nginx日志默认没有开启,需要手动开启。

负载均衡
server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location /te/ {
            #root   html;
            #index  index.html index.htm;
            proxy_pass http://lbServer/;                  #指向upstream
        }
    }


     upstream lbServer {                                  #定义upstream
              server 192.168.1.6:80 weight=2;             #负载均衡下面的主机1
              server 192.168.1.7:80 weight=1;             #负载均衡下面的主机2  
     }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
nginx负载均衡策略
  • random 轮询默认值 ,不写就是轮询
upstream lbServer {                          #定义upstream
          server 192.168.1.6:80;             #负载均衡下面的主机1
          server 192.168.1.7:80;             #负载均衡下面的主机2  
 }
  • 1.
  • 2.
  • 3.
  • 4.
  • weight 权重
upstream lbServer {                                  #定义upstream
              server 192.168.1.6:80 weight=2;             #负载均衡下面的主机1
              server 192.168.1.7:80 weight=1;             #负载均衡下面的主机2  
     }
  • 1.
  • 2.
  • 3.
  • 4.
  • ip_hash 客户端ip
upstream lbServer {                         #定义upstream
              ip_hash;                           #指定负载均衡策略
              server 192.168.1.6:80;
              server 192.168.1.7:80;
     }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • url-hash(三方)
upstream lbServer {                         #定义upstream
              ip_hash;                           #指定负载均衡策略
              server 192.168.1.6:80;
              server 192.168.1.7:80;
              hash $request_uri; 
			  hash_method crc32; 
     }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • fair(三方)
upstream lbServer {                         #定义upstream
              fair;                              #指定负载均衡策略
              server 192.168.1.6:80;
              server 192.168.1.7:80;
     }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • least_conn 最小链接数
upstream demo_server {
  zone test 10M; # zone可以设置共享内存空间的名字和大小
  least_conn;
  server 121.42.11.34:8020;
  server 121.42.11.34:8030;
  server 121.42.11.34:8040;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • least_time 最小响应时间