nginx篇(一)

来源:互联网 发布:淘宝类似图片怎么搜索 编辑:程序博客网 时间:2024/06/06 01:24

        Nginx是由C编写的一款较apache更轻量级的web服务器,同时它也可以用作http的反向代理服务器,邮件的代理服务器,tcp的代理服务器。通过查看http://www.netcraft.com/这个网站的Internet Data Mining版块可以看到目前世界上主流的web服务器各自的使用份额,其中可以看到nginx近几年来的不错表现。在我看来,相对apache的厚重来说,nginx就像反应敏捷的年轻小伙子和聪明伶俐的少女,干起活来效率高,反应快,而且能干的活还挺多,给人带来愉悦的心情。

       为什么如此受欢迎?

             亮点一:支持在线升级程序版本,在线更换日志,以及在线更换配置文件,即称之为热部署。

             亮点二:低内存消耗,据测试,10000个keep-alive连接模式下的非活动连接仅消耗2.5M内存。

     nginx的安装:

     方法一:红帽并没有将nginx收录,到官网下载rpm包,或者本地同步nginx的yum源,直接yum安装

      ]# vim  /etc/yum.repos.d/nginx.repo 
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/x86_64/
gpgcheck=0
enabled=1

而后使用yum安装

yum  install nginx

    方法二:tar包安装,

        去官网下载tar包:

[root@localhost ~]#yum install pcre2-devel.x86_64 openssl-devel  zlib-devel

[root@localhost ~]# cd /usr/local/src/

[root@localhost src]# wget http://nginx.org/download/nginx-1.9.10.tar.gz

[root@localhost src]# tar xf nginx-1.9.10.tar.gz -C /usr/local/
[root@localhost src]# cd /usr/local/ 
[root@localhost local]# mkdir nginx

[root@localhost local]# cd nginx-1.9.10/

[root@localhost nginx-1.9.10]# yum install openssl-devel

[root@localhost nginx-1.9.10]# ./configure --prefix=/usr/local/nginx --conf-path=/etc/nginx/nginx.conf  --error-log-path=/var/log/nginx/error_log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --with-select_module  --with-poll_module --with-threads --with-http_ssl_module  --with-http_gzip_static_module --with-http_stub_status_module  --http-proxy-temp-path=/tmp  --http-uwsgi-temp-path=/etc/uwsgi --http-scgi-temp-path=/etc/scgi  --with-mail --with-mail_ssl_module  --with-pcre  --with-debug

[root@localhost nginx-1.9.10]# make -j 4 && make install

]# /usr/local/nginx/sbin/nginx    ///启动nginx进程

]# /usr/local/nginx/sbin/nginx -s stop   ///关闭nginx进程

//设置nginx环境变量

[root@localhost nginx-1.9.10]# vim /etc/profile.d/nginx.sh
[root@localhost nginx-1.9.10]# cat !$
cat /etc/profile.d/nginx.sh
export PATH=$PATH:/usr/local/nginx/sbin
[root@localhost nginx-1.9.10]# source !$
source /etc/profile.d/nginx.sh

//启动nginx,ps查看可以看到默认一个master启用了一个worker,这个值在默认的配置文件里

[root@localhost nginx-1.9.10]# nginx 
[root@localhost nginx-1.9.10]# ps aux|grep nginx
root       6548  0.0  0.0  47512  1124 ?        Ss   16:48   0:00 nginx: master process nginx
nobody     6549  0.0  0.1  50020  1968 ?        S    16:48   0:00 nginx: worker process
root       6551  0.0  0.0 112640   960 pts/0    S+   16:49   0:00 grep --color=auto nginx

测试浏览器访问url: http://172.16.52.59/


nginx服务管理

启动: nginx

关闭|重载:  nginx  -s stop|reload


配置nginx:

nginx的配置页面分为三个部分: main block,event,http

core module:

//worker_processes是设置worker线程的数量,通常为cpu总cores减1

worker_processes  3;

//worker_cpu_affinity 设置cpu绑定,在1.9.10开始可以自动绑定

worker_cpu_affinity auto;
//worker_priority  设置worker的优先级,即nice值  [-20,19],默认为0
查看nice值:
[root@localhost nginx-1.9.10]# ps axo comm,pid,nice |grep nginxnginx             6548   0nginx             6699   0nginx             6700   0nginx             6701   0
http cores:
//error_page 这个模块式配置错误页面的,有两种方式:
eg1:
error_page 404 /error_page/404.html ;
//下面这个要指定code值的时候一定要404空格,而后等于号后面接数字不能有空格
error_page 404 =260 /error_page/404.html ;

    server {         listen 8080;         server_name www.b.com; root /web/vhosts/;index index.html index.htm;        location /images/ {root  /web/vhosts/;             #  error_page 404 =260 /error_page/404.html ;auth_basic "just a important page:g";auth_basic_user_file /web/vhosts/.htpasswd;       # try_files test2.html  test3.html /redict_page/chuju.jpg ;         } 
  • 定义防盗链
 server {
listen 80;
server_name node31.huahualin.com;
root /data/web/nginx;
        location ~* \.(gif|jpg|jpeg)$ {
                root /data/web/nginx3/;
}
        error_page 403 /403.html;

valid_referers none blocked server_names example.*  *.example.com www.example.org/galeries/ ~\.google\.;
if ($invalid_referer) {
return 403;
}
}

说明:
none:请求报文不存在referer首部;blocked:请求报文中存在referer首部,但其没有有效值,或其值非以http://或https://开头;server_names:其值为一个主机名;arbitrary string:直接字符串,可以使用*通配符;regular expression:以~起始的正则表达式;
  • 基于主机名配置虚拟机

listen address[:port] [default_server] [ssl] [http2 | spdy] 
listen port [default_server] [ssl] [http2 | spdy]

///更改环境变量           
vim  /etc/profile.d/nginx.sh
     export   PATH=/usr/loca/nginx/sbin:$PAHT
exec  bash

http{
      server {
    listen 80;
            server_name node3.huahualin.com;
            root    /data/web/nginx1;
            }
     
   server {
    listen 80;
            server_name node31.huahualin.com;
            root    /data/web/nginx2;
            }
}
///创建目录
mkdir -pv   /data/web/nginx1
mkdir -pv   /data/web/nginx2
vim    /data/web/nginx1/index.html
vim    /data/web/nginx2/index.html
///平滑重载配置文件
nginx   -s reload
测试:http://node3.huahualin.com   
           http://node31.huahualin.com

  • 基于端口配置虚拟机
http{
      server {
    listen 80;
            server_name node3.huahualin.com;
            root    /data/web/nginx1;
            }
     
   server {
    listen 8080;
            server_name node31.huahualin.com;
            root    /data/web/nginx2;
            }
}
///创建目录
mkdir -pv   /data/web/nginx1
mkdir -pv   /data/web/nginx2
vim    /data/web/nginx1/index.html
vim    /data/web/nginx2/index.html
///平滑重载配置文件
nginx   -s reload
测试:http://node3.huahualin.com   
           http://node31.huahualin.com:8080
  • 也可以基于ip:port,但是不常用
  • 重定向图片
vim    /data/web/nginx1/index.html
 server {
listen 80;
server_name node31.huahualin.com;
root /data/web/nginx;
        location ~* \.(gif|jpg|jpeg)$ {
                root /data/web/nginx3/;
}

  • 定义uri重定向,可实现seo搜索引擎优化
server {
listen 80;
server_name node31.huahualin.com;
root /data/web/nginx;
        location ~* \.(gif|jpg|jpeg)$ {
                root /data/web/nginx3/;
rewrite ^/img/(.*)$ /images/$1 redirect;
}
        error_page 404 /404.html;
}

说明:
                 rewrite  regex  replacement [flag];
regex:正则表达式,用于匹配用户请求的url;对应的目录不存在,也是会重写的
replacement:重写为的结果;
[flag]:
last:重写完成之后停止对当前uri的进一步处理,改为对新url的新一轮处理;类似continue,用户不知道重写的
break:重写完成之后停止当uri的处理,转向其后面的其它配置;
redirect:重写完成之后会返回客户端一个临时的重定向,由客户端对新的url重新发起请求(302);用户知道是重写的
permanent:重写完成之后会返回客户端一个永久的重定向,由客户端对新的url重新发起请求(301);
  • if条件匹配
server {
listen 80;
server_name node31.huahualin.com;
if ( $uri ~ /fool ) {
return https://www.baidu.com;
}
}

测试:http://node31.huahualin.com/fool


说明:if (condition) { ... }
条件判断,引用新的配置上下文;

condition:
比较表达式:
==,!=
~:模式匹配,区分字符大小写;
~*:模式匹配,不区分字符大小写;
!~:模式不匹配,区分字符大小写;
!~*: 模式不匹配,不区分字符大小写;
文件及目录判断:
-f, !-f:是否存在且为普通文件;且的反义就是或,!-f是否不存在或者不为普通文件
-d, !-d: 是否存在且为目录;
-e, !-e:是否存在;
-x, !-x:是否存在且可执行;

return code [text];
return code URL;
return URL;
立即停止对请求的uri的处理,并返回指定的状态码;

  • 使用文本压缩,用时钟周期换大量的带宽,节约带宽
http {
 gzip on;
gzip_http_version 1.0;
gzip_comp_level 6;
gzip_disable msie6;
gzip_min_length 2;
gzip_types  image/gif image/jpg image/jpeg image/png text/plain text/css text/xml application/json  application/xml application/java-script application/x-javascript;


    server {
listen 80;
server_name node31.huahualin.com;
root /data/web/nginx;
        location ~* \.(gif|jpg|jpeg)$ {
                root /data/web/nginx3/;
rewrite ^/img/(.*)$ /images/$1 redirect;
}
        error_page 404 /404.html;
location /admin {
root /data/web/nginx3;
auth_basic "Admin";
auth_basic_user_file /etc/nginx/.ngxhtpasswd;
}
}

测试:http://node31.huahualin.com/admin/fstab.txt

如图:response headers 为Content-Encoding:gzip



0 0
原创粉丝点击