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、可以作为静态资源的web服务器、可以缓存文件的资源描述符——加速
		2、支持对http、stmp、pop3等多种协议的反向代理
		3、支持实现缓存和负载均衡
		4、支持fcgi
		5、支持uWSCGI
		6、支持模块化
		7、支持过滤器,对特定文件进行压缩传输
		8、支持ssl
		9、支持图像大小调整
		
二.、nginx的特性
		1、模块
		2、高性能
		3、低内存消耗
		4、支持热部署
		5、支持异步IO
		6、支持事件驱动
		7、支持内存映射

三、核心模块
		1、标准http模块
		2、拓展http模块
		3、邮件拓展模块
		4、第三方模块
		
四、安装依赖
[root@156 ~]# yum groupinstall "Development Tools" -y
[root@156 ~]# yum install pcre-devel openssl openssl-devel -y 

五、安装nginx
(1)创建nginx的运行用户
[root@156 ~]# groupadd -r nginx
[root@156 ~]# useradd -r -s /sbin/nologin -g nginx nginx
[root@156 ~]# tar xvf nginx-1.6.2.tar.gz 
[root@156 ~]# cd nginx-1.6.2
[root@156 nginx-1.6.2]# ./configure --help | more
[root@156 nginx-1.6.2]# ./configure \
--prefix=/usr/local/nginx \
--conf-path=/etc/nginx/nginx.conf \
--user=nginx --group=nginx \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--with-http_ssl_module \
--with-http_stub_status_module \
--without-http_gzip_module \
--with-http_mp4_module \
--with-http_flv_module \
--http-client-body-temp-path=/var/tmp/nginx/client \
--http-proxy-temp-path=/var/tmp/nginx/proxy \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi \
[root@156 nginx-1.6.2]# make && make install

(2)创建临时文件的保存目录
[root@156 nginx-1.6.2]# mkdir /var/tmp/nginx/{client,proxy,fcgi} -pv
mkdir: created directory `/var/tmp/nginxclient'
mkdir: created directory `/var/tmp/nginxproxy'
mkdir: created directory `/var/tmp/nginxfcgi'

(3)启动nginx
[root@156 ~]# /usr/local/nginx/sbin/nginx
[root@156 ~]# ss -tnl | grep 80
LISTEN     0      128                       *:80   
[root@156 ~]# lsof -i :80
COMMAND  PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   8395  root    6u  IPv4  20287      0t0  TCP *:http (LISTEN)
nginx   8396 nginx    6u  IPv4  20287      0t0  TCP *:http (LISTEN)
[root@156 ~]# ps aux | grep nginx
root      8395  0.0  0.1  44572  1040 ?        Ss   22:05   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx     8396  0.0  0.1  45000  1620 ?        S    22:05   0:00 nginx: worker process      
root      8513  0.0  0.0 103308   848 pts/0    S+   22:39   0:00 grep nginx
测试:在浏览器上输入 服务器的IP地址即可访问nginx界面,例如 http://192.168.70.156/

nginx配置详解
六、和nginx正常运行相关的配置
	1、worker_processes  1;
		 设置发起几个nginx的worker进程

	2、user  userName [groupName];
	      指定nginx以谁的身份来运行

	3、pid /path/to/file
		 pid文件

	4、worker_rlimit_nofile number;
		 设置所有worker进程一共可以打开的文件的最大数量值
       (linux上默认每个用户可以打开1024个文件、套接字也是一个文件)

七、nginx性能优化相关配置
	5、worker_processes  number | auto;
		 设置发起几个nginx的worker进程
		 number:应该比物理核心少一个
		 auto:让系统自动判断
	
	6、worker_cpu_affinity cpumask ……| auto;
		 设置cpu掩码,将worker进程绑定在一个固定的cpu
		 举例:
		 worker_processes	4;
		 worker_cpu_affinity	0001 0010 0100 1000;	
		 举例:
		 worker_processes	2;
		 worker_cpu_affinity	0001 0100;
		 注意:可以用auto,但是是1.9.10之后可以用

   7、time_resolution interval;
		 降低发起系统调用gettimeofday()次数 
		 time_resolution 100ms;
	
   8、worker_priority number;
		修改worker进程的nice值(默认是0)
	
八、和事件相关的配置
    9、accept_mutex on | off
		  设置master进程将客户端的请求调度到worker进程的
		  调度方式:轮询、随机 
		  on:使用轮询
		  默认方式:off
	
   10、accept_mutex_delay time; 默认500ms
	      设置master延迟多久才将客户端的请求调度到worker进程
	
   11、user [epoll|poll|select] 
	      通常是不需要指定连接的处理方法(起始就是所使用的事件模型)的 
     	 建议让系统自动判断所要使用的方法
	
   12、worker_connections number;
	      指定每个worker进程可以处理的最大并发连接的数量
	      默认是1024
	
   13、master_process on | off;
	      指定是否使用master-worker
	      默认是:on
	
九、和调试和定位bug相关配置
   14、damon off | on
		   指定nginx工作在前台还是后台
	   	默认是 on 

   15、error_log file [level];
	       指定错误日志的保存位置以及日志级别
			总结:经常要修改的参数
			worker_processes
			worker_connections
			worker_cpu_affinity
			worker_priority
	


十、和http相关的配置
[root@156 ~]# vim /etc/profile.d/nginx.sh
export PATH=$PATH:/usr/local/nginx/sbin
[root@156 ~]# source /etc/profile.d/nginx.sh	
[root@156 ~]# nginx
[root@156 ~]# nginx -s reload
[root@156 ~]# ss -tnl | grep 80	
上下文:http {指令}
例子:http{
	全局配置:对所有的虚拟主机都生效的配置
	sendfile on;
	keepalive_timeout 10;
	……
	虚拟主机配置
	server{
		listen 80;指定该虚拟主机所监听的端口
		server_name www.lichao.com;指定虚拟主机的域名
		root /vhost/web1;指定虚拟主机的网站根目录
		location /{
		
		}
	}
	server{
	
	}
}			

	16、server_name
			设定虚拟主机的域名,可以使用正则表达式来表示域名
			适用范围:server上下文中

			server{
				server_name www.zxhk.com;

			}
			server {
				server_name *.zxhk.com;

			}
			server {
				server_name www.zxhk.*;

			}
			server {
				server_name *;

			}
			server {
				server_name ~ ^.*\.www.zxhk.com;

			}
			
			www.zxhk.com

注意:nginx在进行域名匹配的时候,是有一定的规则,匹配次序如下
	1、做精确匹配,server_name中的内容必须和用户请求的内容完全一样
	2、检查左侧的通配符
	3、检查右侧的通配符
	4、检查通配符
	5、检查正则表达式
	6、通过所有检查都失败,则访问default_server,如果没有指定default_server,
	那么第一个server就是default_server

	17、listen
		指定所监听的套接字
		例子:
		listen 127.0.0.1:80;
		listen 172.0.0.1;
		listen 80;
		listen *:80;
		listen localhost:80;
	
	18、root 
		指定网站根目录
		使用范围:http、server、location
		注意:如果将root写在了http部分,则会对全部的server都有效

	例子:
		[root@156 ~]# cd /etc/nginx/
		[root@156 nginx]#cp nginx.conf{,.bak}
		[root@156 nginx]#vim nginx.conf
		[root@156 ~]# mkdir -pv /vhost/web1
		[root@156 ~]# echo "test for web1">/vhost/web1/index.html	
		[root@156 ~]# cat /vhost/web1/index.html
		[root@156 ~]# nginx -s reload
		http://192.168.70.156/
		test for web1
		
19、location
		location [ = | ~ | ~* | ^~ ] uri {……}
		url:统一资源定位符
		uri:统一资源标识符
		server{
			server_name www.zxhk.com;
			listen 80;
			location / {
				root /vhost/web1;
			}
		}
		这里的location的含义就是将用户所访问的根映射为/vhost/web1路径
		有多个location的例子
		server{
			server_name www.zxhk.com;
			listen 80;

			location / {
				root /vhost/web1;
			}

			location /img/ {
				root /tmp/img;
			}

			location ~ \.php$ {
				root /phpfile;
			}
		    }
	
		location作路径映射的时候,有多种方式
		不带符号 
		=
		~
		~*
		^~

		1、不带符号,如下
			location /img {
				root /vhost/web1;
			}

			http://www.baidu.com/img
			http://www.baidu.com/a.html
	
	
		2、= 表示的是精确匹配
			location = /img {
				root /vhost/web1;
			} 

			要求只有用户所请求的uri和location所指定的uri必须
			完全一致,否则匹配失败,如下
			http://www.baidu.com/img               <<匹配成功
			http://www.baidu.com/img/index.html    <<匹配失败

		3、~表示对用户所请求的uri做正则表达式,检查过程中区分大小写
			 location ~ .jgp$ {
					root /vhost/web1;
		 	}
		4、~*表示对用户所请求的uri做正则表达式,检查过程中不区分大小写
		5、^~不对用户所请求的uri做正则表达式检查,而是只检查uri的左半部分

		各种写法的优先级
		精确匹配优先级最高(=)
		uri的左半部检查
		区分大小写的正则表达式检查
		不区分大小写的正则表达式检查
		不带符号的location
	
		location /img {
			root /vhost/web1;
		}

	[root@156 ~]# cd /vhost/web1/
	[root@156 web1]# mkdir img
	[root@156 web1]# echo "this is test" >index.html 
	[root@156 web1]# echo "this is image" >img/index.html 
	http://192.168.70.156/
	this is test
	http://192.168.70.156/img/index.html
	this is image
	
	[root@156 ~]# vim /etc/nginx/nginx.conf
			server {
				listen 80;
				server_name www.zxhk.com;
				location / {
					root /vhost/web1/;
				}

				location /img/ {
					root /vhost/web2/;
				}

		}
	[root@156 ~]# cd /vhost/
	[root@156 vhost]# mkdir web2
	[root@156 vhost]# echo "this is web2" > web2/index.html
	[root@156 vhost]# source /etc/profile.d/nginx.sh 
	[root@156 vhost]# nginx -s reload
		http://192.168.70.156/img    
		404 Not Found 
	[root@156 vhost]# vim /var/log/nginx/error.log 
		2019/09/05 08:52:38 [error] 1915#0: *1 open() "/vhost/web1/favicon.ico" failed
		(2: No such file or directory),
	[root@156 vhost]# cd web2/
	[root@156 web2]# ls
	index.html
	[root@156 web2]# mkdir img
	[root@156 web2]# mv index.html  img/
	http://192.168.70.156/img 
    this is web2 

	20、alias path
		   将用户所请求的uri映射为一个指定的路径 

server {
								  listen 80;
									server_name www.zxhk.com;
									location /img1/ {
													root /vhost/web1/;
									}

									location /img2/ {
													alias /vhost/web1/;
							    	}
	 			}
cd /vhost/web1
ls
echo "/vhost/web1">index.html
mkdir img1
echo "/vhost/web1/img1">img1/index.html
source /etc/profile.d/nginx.sh 
nginx -t
nginx -s reload
ls
mkdir img2
echo "/vhost/web2/img/2">img2/index.html
tree
http://192.168.70.156/img2/
/vhost/web1
http://192.168.70.156/img2
404 Not Found

vim /var/log/nginx/error.log 
2019/09/05 09:51:08 [error] 2392#0: *13 open() 
"/usr/local/nginx/html/img2" failed (2: No such 
file or directory), client: 192.168.70.1, server: 
www.zxhk.com, request: "GET /img2 HTTP/1.1", host:
"192.168.70.156"