1 原理

Nginx内核和模块组成,其中,内核的设计非常微小和简洁,完成的工作也非常简单,仅仅通过查找配置文件将客户端请求映射到一个location block(location是Nginx配置中的一个指令,用于URL匹配),而在这个location中所配置的每个指令将会启动不同的模块去完成相应的工作。

Nginx相对于Apache优点:

1)       高并发响应性能非常好,官方Nginx处理静态文件并发5w/s

2)       反向代理性能非常好。(可用于负载均衡)

3)       内存和cpu占用率低。(为Apache的1/5-1/10)

4)       功能较Apache少(常用功能均有)

5)       对php可使用cgi方式和fastcgi方式。

2 安装

首先需要安装pcre库,然后再安装Nginx:
#安装pcre支持rewrite库,也可以安装源码,注*安装源码时,指定pcre路径为解压
源码的路径,而不是编译后的路径,否则会报错。
(make[1]: *** [/usr/local/pcre/Makefile] Error 127 错误)
yum installpcre-devel pcre -y
[root@57135test ~]# wget http://nginx.org/download/nginx-1.10.3.tar.gz
[root@57135test ~]# tar -xzf nginx-1.10.3.tar.gz 
[root@57135test ~]# cd nginx-1.10.3
[root@57135test nginx-1.10.3]# useradd nginx ;./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
[root@57135test nginx-1.10.3]# make && make install
[root@57135test nginx-1.10.3]# cd /usr/local/nginx/
[root@57135test nginx]# ls
conf  html  logs  sbin
[root@57135test conf]# ll nginx.conf  (nginx主配置文件)
-rw-r--r--. 1 root root 2656 11月 29 03:26 nginx.conf
[root@57135test conf]# pwd
/usr/local/nginx/conf
[root@57135test conf]# 
注:
a nginx下载   一般国外的官网后缀都是org(非营利组织)
http://nginx.org/en/download.html
b 反向代理:通过外网访问内网的机器(正向代理 内网-外网)
正向是出去,反向是进来

3 启动

[root@57135test sbin]# /usr/local/nginx/sbin/nginx -t  (检查,每次修改配置文件都要检查一下)
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@57135test nginx]# /usr/local/nginx/sbin/nginx  (启动)
[root@57135test nginx]# ps -ef|grep nginx   (查看进程,默认为80端口)
root       6618      1  0 03:35 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx      6619   6618  0 03:35 ?        00:00:00 nginx: worker process
root       6621   3619  0 03:35 pts/0    00:00:00 grep --color=auto nginx
[root@57135test nginx]# pkill nginx  (关闭)
[root@57135test nginx]# /usr/local/nginx/sbin/nginx -s reload  (平滑重启)
[root@57135test conf]# /usr/local/nginx/sbin/nginx -s stop(停止)
[root@57135test conf]# /usr/local/nginx/sbin/nginx -v (查看版本)
nginx version: nginx/1.10.3
[root@57135test conf]# /usr/local/nginx/sbin/nginx -V(获取旧版本nginx的configure选项)
nginx version: nginx/1.10.3
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-4) (GCC) 
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

4 虚拟主机

配置文件及虚拟主机(两种方法,一种是配置nginx.conf文件,另一种是新建vhosts文件夹)
在真实的服务器环境,为了充分利用服务器资源,一台nginx web服务器同时会配置N个虚拟域名主机,即多个域名对于同样一个80端口。然后服务器IP数量很多,也可以配置基于多个IP对应同一个端口。
a [root@57135test conf]# pwd
/usr/local/nginx/conf
[root@57135test conf]# vi nginx.conf  修改配置文件后要检查并重启
 server {
        listen       80;
        server_name  localhost;

        location / {
            root   html;        
            index  index.html index.htm;
        }
虚拟主机-一个server就是一个虚拟主机
#virtual hosts config 2017/2/6
#www.jf1.com
    server {
        listen       80;
        server_name  www.xiaochang1.com;
        location / {
            root   /usr/local/nginx/html/jf1;
            index  index.html index.htm index.php;
        }
       }
#www.jf2.com
      server {
        listen       80;
        server_name  www.xiaochang2.com;
        location / {
            root   /usr/local/nginx/html/jf2;
            index  index.html index.htm index.php;
        }
       }
}
创建两个不同的目录mkdir –p /usr/local/nginx/html/{a,b},然后分别在两个目录创建两个不同的index.html网站页面即可。通过客户端配置hosts指向两个域名,然后在IE浏览器访问测试效果。
[root@57135test html]# mkdir jf1 jf2
[root@57135test html]# ls
50x.html  index.html  jf1  jf2
[root@57135test html]# cp index.html jf1/
[root@57135test html]# cp index.html jf2/
[root@57135test html]# pwd
/usr/local/nginx/html
b 新建vhosts
[root@57135test jf2]# cd /usr/local/nginx/conf/
[root@57135test conf]# ls
fastcgi.conf            mime.types          scgi_params.default
fastcgi.conf.default    mime.types.default  uwsgi_params
fastcgi_params          nginx.conf          uwsgi_params.default
fastcgi_params.default  nginx.conf.bak      win-utf
koi-utf                 nginx.conf.default
koi-win                 scgi_params
[root@57135test conf]# cd /usr/local/nginx/conf/
[root@57135test conf]# mkdir vhosts
[root@57135test conf]# cd vhosts/
[root@57135test vhosts]# ls
[root@57135test vhosts]# vi vhosts.conf 
#www.jf1.com
    server {
        listen       80;
        server_name  www.xiaochang1.com;
        location / {
            root   /usr/local/nginx/html/jf1;
            index  index.html index.htm index.php;
        }
       }
#www.jf2.com
      server {
        listen       80;
        server_name  www.xiaochang2.com;
        location / {
            root   /usr/local/nginx/html/jf2;
            index  index.html index.htm index.php;
        }
       }
[root@57135test vhosts]# pwd
/usr/local/nginx/conf/vhosts
[root@57135test conf]# vi nginx.conf
将配置文件中虚拟主机部分删除,并添加引用,如下:
 include vhosts/*;
或者:
将vhosts.conf文件删掉,以域名命名,把单独的网站放在单独的域名下面,如下
[root@57135test vhosts]# pwd
/usr/local/nginx/conf/vhosts
[root@57135test vhosts]# vi www.xiaochang
[root@57135test vhosts]# vi www.xiaochang1.com 
[root@57135test vhosts]# ls
www.xiaochang1.com  www.xiaochang2.com

5 报错排查

报错排查:
[root@57135test conf]# /usr/local/nginx/sbin/nginx -t
nginx: [emerg] unexpected end of file, expecting "}" in /usr/local/nginx/conf/nginx.conf:44
nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed
解决:配置文件中  } 括号没加    句尾的;没加

6 测试时修改本机hosts进行测试

windows配置hosts
作用:将ip和域名绑定(DNS-域名解析服务)
a 查找:C:\Windows\System32\drivers\etc\hosts
修改配置文件:
192.168.57.135  www.xiaochang1.com
192.168.57.135  www.xiaochang2.com
保存hosts文件即可(ip为域名指定的ip)

7 nginx升级

下载所需版本的Nginx
wget http://www.nginx.org/download/nginx-1.4.2.tar.gz 
获取旧版本nginx的configure选项
/usr/local/nginx/sbin/nginx-V 
编译新版本的nginx
tar  -xvf  nginx-1.4.2.tar.gz 
cd nginx-1.4.2 
./configure --prefix=/usr/local/nginx --user=www--group=www --with-http_stub_status_module --with-http_ssl_module 
make
备份旧版本的nginx可执行文件,复制新版本的nginx这行文件
mv/usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.old 
cp  objs/nginx  /usr/local/nginx/sbin/
测试新版本nginx是否正常
/usr/local/nginx/sbin/nginx -t
平滑重启升级nginx
kill –USR2  `cat /usr/local/nginx/log/nginx.pid` 
旧版本Nginx的pid变为oldbin,这是旧版本和新版本的nginx同时运行,过一段时间等就nginx处理完用户请求后,执行下面操作
从容关闭旧版本的Nginx进程
kill -WINCH `cat /usr/local/nginx/log/nginx.oldbin` 
决定是否升级到新版的nginx
kill –HUP `cat/usr/local/nginx/log/nginx.oldbin` ##nginx在不重载配置文件启动工作进程
kill –QUIT `cat/usr/local/nginx/log/nginx.oldbin` ##关闭旧版nginx 
验证nginx是否升级成功
/usr/local/nginx/sbin/nginx –V ###显示下图则升级成功

wKioL1io_XySibgAAAFlcvyZA88025.png-wh_50

8 配置文件