Nginx的安装与初步讲解

Nginx的安装与初步讲解


1.2 Nginx的基本编译安装:
编译安装前的准备:
安装Nginx编译安装需要依赖的一些包
[root@localhost ~]## yum install gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel open openssl-devel -y

切换到存放nginx源码包的目录:
使用源码包中的configure脚本 生成makefile文件。
configure脚本支持以下选项:
--help print this message #打印这个信息

--prefix=PATH set installation prefix #设置安装路径
--sbin-path=PATH set nginx binary pathname #设置binary 路径
--conf-path=PATH set nginx.conf pathname #设置nginx 配置文件路径
--error-log-path=PATH set error log pathname #设置错误日志路径
--pid-path=PATH set nginx.pid pathname #设置nginx.pid 路径
--lock-path=PATH set nginx.lock pathname #设置nginx.lock路径

--user=USER set non-privileged user for #设置没有特殊权限的用户执行worker processes
worker processes
--group=GROUP set non-privileged group for #设置没有特殊权限的用户执行worker processes
worker processes

--builddir=DIR set build directory #设置编译目录

--with-rtsig_module enable rtsig module #开启rtsig 模块
--with-select_module enable select module #开启select 模块
--without-select_module disable select module #关闭 select 模块
--with-poll_module enable poll module #启用 poll 模块
--without-poll_module disable poll module #关闭poll 模块

--with-file-aio enable file AIO support #开启file AIO支持
--with-ipv6 enable IPv6 support #开启IPv6 支持

--with-http_ssl_module enable ngx_http_ssl_module #开启ssl 模块
--with-http_spdy_module enable ngx_http_spdy_module #开启spdy模块
--with-http_realip_module enable ngx_http_realip_module #开启 realip 模块
--with-http_addition_module enable ngx_http_addition_module #开启 addition 模块
--with-http_xslt_module enable ngx_http_xslt_module #开启 xslt模块
--with-http_image_filter_module enable ngx_http_image_filter_module #开启 image_filter模块
--with-http_geoip_module enable ngx_http_geoip_module #开启geoip模块
--with-http_sub_module enable ngx_http_sub_module #开启 sub模块
--with-http_dav_module enable ngx_http_dav_module #开启dav 模块
--with-http_flv_module enable ngx_http_flv_module #开启 flv模块
--with-http_mp4_module enable ngx_http_mp4_module #开启 mp4模块
--with-http_gunzip_module enable ngx_http_gunzip_module #开启gunzip模块
--with-http_gzip_static_module enable ngx_http_gzip_static_module #开启 gzip_static模块
--with-http_auth_request_module enable ngx_http_auth_request_module #开启 auth_request(身份验证请求)模块
--with-http_random_index_module enable ngx_http_random_index_module #开启random_index(随机指数)模块
--with-http_secure_link_module enable ngx_http_secure_link_module #开启 secure_link(安全链接)模块
--with-http_degradation_module enable ngx_http_degradation_module #开启degradation 模块
--with-http_stub_status_module enable ngx_http_stub_status_module #开启 stub_status 模块

--without-http_charset_module disable ngx_http_charset_module #关闭 charset(字符集) 模块
--without-http_gzip_module disable ngx_http_gzip_module #关闭 gzip模块
--without-http_ssi_module disable ngx_http_ssi_module #关闭 ssi 模块
--without-http_userid_module disable ngx_http_userid_module #关闭 userid模块
--without-http_access_module disable ngx_http_access_module #关闭 access模块
--without-http_auth_basic_module disable ngx_http_auth_basic_module #关闭 auth_basic (基本身份认证)模块
--without-http_autoindex_module disable ngx_http_autoindex_module #关闭 autoindex(自动变址)模块
--without-http_geo_module disable ngx_http_geo_module #关闭 geo模块
--without-http_map_module disable ngx_http_map_module #关闭 map 模块
--without-http_split_clients_module disable ngx_http_split_clients_module #关闭 split_clients (划分客户端)模块
--without-http_referer_module disable ngx_http_referer_module #关闭 referer (推荐人)模块
--without-http_rewrite_module disable ngx_http_rewrite_module #关闭 rewrite (重写)模块
--without-http_proxy_module disable ngx_http_proxy_module #关闭 proxy (代理)模块
--without-http_fastcgi_module disable ngx_http_fastcgi_module #关闭 fastcgi模块
--without-http_uwsgi_module disable ngx_http_uwsgi_module #关闭 uwsgi 模块
--without-http_scgi_module disable ngx_http_scgi_module #关闭 scgi 模块
--without-http_memcached_module disable ngx_http_memcached_module #关闭 memcached 模块
--without-http_limit_conn_module disable ngx_http_limit_conn_module # 关闭 limit_conn 模块
--without-http_limit_req_module disable ngx_http_limit_req_module #关闭 limit_req模块
--without-http_empty_gif_module disable ngx_http_empty_gif_module #关闭 empty_gif 模块
--without-http_browser_module disable ngx_http_browser_module #关闭 browser 模块
--without-http_upstream_ip_hash_module
disable ngx_http_upstream_ip_hash_module #关闭 ip_hash模块
--without-http_upstream_least_conn_module
disable ngx_http_upstream_least_conn_module #关闭 least_conn 模块
--without-http_upstream_keepalive_module
disable ngx_http_upstream_keepalive_module #关闭 keepalive 模块

--with-http_perl_module enable ngx_http_perl_module #开启perl 模块
--with-perl_modules_path=PATH set Perl modules path #设置perl模块路径
--with-perl=PATH set perl binary pathname #设置perl 命令路径

扫描二维码关注公众号,回复: 2698211 查看本文章

--http-log-path=PATH set http access log pathname #设置访问日志 路径
--http-client-body-temp-path=PATH set path to store
http client request body temporary files #设置存储路径 http客户端请求主体临时文件
--http-proxy-temp-path=PATH set path to store
http proxy temporary files #设置存储路径 http代理临时文件
--http-fastcgi-temp-path=PATH set path to store
http fastcgi temporary files #设置 fastcgi临时文件存储路径
--http-uwsgi-temp-path=PATH set path to store
http uwsgi temporary files #设置 uwsgi 临时文件存储路径
--http-scgi-temp-path=PATH set path to store
http scgi temporary files #设置 scgi 临时文件存储路径

--without-http disable HTTP server #关闭 http server
--without-http-cache disable HTTP cache #关闭 http cache

--with-mail enable POP3/IMAP4/SMTP proxy module #开启 POP3/IMAP4/SMTP 协议模块
--with-mail_ssl_module enable ngx_mail_ssl_module #开启 nginx mail ssl 模块
--without-mail_pop3_module disable ngx_mail_pop3_module #关闭 mail pop3模块
--without-mail_imap_module disable ngx_mail_imap_module #关闭 mail imap 模块
--without-mail_smtp_module disable ngx_mail_smtp_module #关闭 mail smtp 模块

--with-google_perftools_module enable ngx_google_perftools_module #开启 nginx google_perftools (谷歌性能工具)模块
--with-cpp_test_module enable ngx_cpp_test_module #开启 cpp_test 模块

--add-module=PATH enable an external module #开启external(外部)模块

--with-cc=PATH set C compiler pathname #设置C 编译器 路径
--with-cpp=PATH set C preprocessor pathname #设置 C预编译器 路径
--with-cc-opt=OPTIONS set additional C compiler options #设置额外的C编译器选项
--with-ld-opt=OPTIONS set additional linker options #设置额外的链接器选项
--with-cpu-opt=CPU build for the specified CPU, valid values:
pentium, pentiumpro, pentium3, pentium4,
athlon, opteron, sparc32, sparc64, ppc64 #建立指定的CPU 有效值:

--without-pcre disable PCRE library usage #关闭 PCRE 库 使用
--with-pcre force PCRE library usage #强制使用指定的PCRE 库
--with-pcre=DIR set path to PCRE library sources #指定 pcre 使用路径
--with-pcre-opt=OPTIONS set additional build options for PCRE #为PCRE设置额外的构建选项
--with-pcre-jit build PCRE with JIT compilation support #构建PCRE JIT编译的支持

--with-md5=DIR set path to md5 library sources #设置路径md5库资源
--with-md5-opt=OPTIONS set additional build options for md5 #为md5设置额外的构建选项
--with-md5-asm use md5 assembler sources #使用md5汇编源

--with-sha1=DIR set path to sha1 library sources #设置 sha1 库的路径
--with-sha1-opt=OPTIONS set additional build options for sha1 #为sha1设置额外的构建选项
--with-sha1-asm use sha1 assembler sources #使用sha1汇编源

--with-zlib=DIR set path to zlib library sources #指定 zlib 库的路径
--with-zlib-opt=OPTIONS set additional build options for zlib #为zlib 设置额外的构建选项
--with-zlib-asm=CPU use zlib assembler sources optimized
for the specified CPU, valid values:
pentium, pentiumpro #使用 zlib汇编优化的来源

--with-libatomic force libatomic_ops library usage #强制 使用libatomic
--with-libatomic=DIR set path to libatomic_ops library sources #设置 libatomic 路径

--with-openssl=DIR set path to OpenSSL library sources #设置 openssl 库的路径
--with-openssl-opt=OPTIONS set additional build options for OpenSSL #为 openssl 设置额外的构建选项

--with-debug enable debug logging #开启debug 日志

我在生成makefile文件时,使用以下编译参数
./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 --with-http_gzip_static_module --with-http_flv_module --with-http_mp4_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/fastcgi --with-debug

这些编译参数指定了:
--prefix=/usr/local/nginx #nginx的安装路径
--conf-path=/etc/nginx/nginx.conf #指定nginx.conf的路径
--user=nginx --group=nginx #指定nginx进程的属主和属组
--error-log-path=/var/log/nginx/error.log #指定错误日志路径
--http-log-path=/var/log/nginx/access.log #指定访问日志路径
--pid-path=/var/run/nginx/nginx.pid #指定nginx.pid路径
--lock-path=/var/lock/nginx.lock #指定nginx锁文件路径
--with-http_ssl_module #声明启用http的ssl模块
--with-http_stub_status_module #声明启用Server Status页
--with-http_gzip_static_module #声明启用http的gzip模块
--with-http_flv_module #声明启用http的flv模块
--with-http_mp4_module #声明启用http的mp4模块
--http-client-body-temp-path=/var/tmp/nginx/client #设置存储路径 http客户端请求主体临时文件
--http-proxy-temp-path=/var/tmp/nginx/proxy #设置存储路径 http代理临时文件
--http-fastcgi-temp-path=/var/tmp/nginx/fastcgi #设置 fastcgi临时文件存储路径
--with-debug #声明启用nginx的调试日志

编译并安装nginx
[root@localhost nginx-1.12.2]# make && make install

到此为止,我们我们的nginx程序就安装好了。

Nginx的配置文件:
NGINX配置文件:
/etc/nginx/nginx.conf 主配置文件
/etc/nginx/conf.d/.conf (需要在主配置文件中 使用include conf.d/.conf)
/etc/nginx/fastcgi_params Fastcgi 配置文件
/etc/logrotate.d/nginx Nginx日志轮转,用于logrotate服务的日志切割
/etc/nginx/mime.types 设置http协议的Content-Type与拓展名对应关系
/usr/lib64/nginx/modules /etc/nginx/modules Nginx模块目录
/var/cache/nginx Nginx的缓存目录
/var/log/nginx Nginx的日志目录

Nginx服务的启停控制
通过信号机制来实现:
Nginx服务在运行时,会保持一个主进程和一个或多个worker process工作进程。
我们通过给Nginx服务的主进程发送信号就可以控制服务的启停了。
NGINX控制信号:
TERM,INT 快速关闭
QUIT 从容关闭
HUP 用新的配置开始新的工作进程,从容关闭旧的工作进程
USR1 重新打开日志文件
USR2 平滑升级可执行程序
WINCH 从容关闭工作进程

        2行

	使用语法:kill -信号 nginx主进程号

通过命令控制:
nginx -s [ stop|quit|reopen|reload]
stop:强制停止NGINX,不管工作进程当前是否在处理用户请求,都会立即退出
quit:优雅地退出NGINX,工作进程会将当前正在处理的请求处理完毕后,再退出
reload:重载配置信息
reopen;重新打开日志文件
nginx -t 不运行,仅仅测试配置文件
nginx -c /path/to/config 为nginx指定一个配置文件,来代替缺省的
nginx -v 查看NGINX版本
nginx -V 查看nginx编译参数

Nginx主配置文件的配置释义:
Nginx配置文件中,
全局生效的指令:
正常运行的必备配置:
正常运行的必备配置:
1、user USERNAME [GROUPNAME];
指定运行worker进程的用户 和组,例如:
user nginx nginx;
只有被设置的用户或组成员才有权限启动Nginx进程。

        43行

			如果希望所有用户都可以启动Nginx进程,有俩种方法:
				一种是将此行注释掉;
				一种是将用户和组设置为nobody;
				
				
				
			
		2、pid /path/to/pid_file;
			指定nginx的pid文件;
			
		3、worker_rlimit_nofile #;
			指定一个worker进程所能够打开的最大文件句柄数;
			
		4、worker_rlimit_sigpending #;
			指定每个用户能够发往worker的信号的数量;
			
			
	优化性能相关的配置:
		1、worker_processes #|auto:
			worker线程的个数;通常应该为物理CPU核心个数减1;
			可设置为auto,表示自动分配
			
		2、worker_cpu_affinity cpumask ...;
			绑定worker进程至指定的CPU上;
				CPUMASK(cpu掩码)
					0001
					0010
					0100
					1000 
				例如:
					worker_cpu_affinity 00000001 00000010 00000100;
				优点:提升缓存的命中率
					
		3、timer_resolution t; 计时器解析度
			gettimeofday(); 
			
		4、worker_priority nice;设定worker优先级范围
			取值范围:-20, 19  数字越大优先级越小,默认为0
			
配置文件的引入:
	include file;
	file表示要引入的配置文件。

event相关配置
events {
事件驱动相关配置
}
1、accept_mutex [on|off]
master调度用户请求至各worker时用的负载均衡锁;on表示能让多个worker轮流地、序列化地与响应新请求;

        20行

		2、lock_file /path/to/lock_file; 指定锁文件的存放位置
		
		3、accept_mutex_delay #ms;
			
		4、use [epoll|rgsig|select|poll];
			定义使用的事件模型;建议让Nginx自动选择;
			
		5、worker_connections #;
			每个worker进程所能够响应的最大并发请求数;
			
	用于调试、定位问题:
		1、daemon [off|on]
			是否以守护进程方式启动nginx;调试时应设置为off
			
		2、master_process on|off;
			是否以master/worker模型来运行nginx; 调试时可以设置为off
			
		3、error_log /path/to/error_log level;
			错误日志文件及其级别;出于调试的目的,可以使用debug级别,但此级别只有在编译nginx时使用了--with-debug选项才有效;

http {
}

        100行

http配置:http core 配置一个静态web服务器
	ngx_http_core_module
	
	配置框架:
	http {
		upstream {
			.,..
		}
		
		server {
			listen IP:PORT;
			# 虚拟主机配置   NGINX没有中心主机,只有虚拟主机
			location /URL {
				if ...{
					...
				}
				root "/path/to/somewhere";  #根目录
				...
			}
		}
		server {
			,,.
		}
	}
	
		注意:与http配置相关的指令必须放在http、server、location、upstream、if块中;
		
	虚拟主机相关的配置:
		1、server {}
			定义一个虚拟主机;
			
		2、listen
			监听的端口
			完整格式 :listen address[:port] [default_server] [ssl] [spdy] [proxy_protocol] [setfib=number] [fastopen=number] [backlog=number] [rcvbuf=size] [sndbuf=size] [accept_filter=filter] [deferred] [bind] [ipv6only=on|off] [so_keepalive=on|off|[keepidle]:[keepintvl]:[keepcnt]];
			
			listen address[:port] [default_server] ssl 
			ssl:用于限制只能通过ssl连接提供服务
			backlog=number: 指明TCP协议backlog队列的大小。默认为-1,表示不设置;
			rcvbuf=size:设定监听句柄的SO_RCVBUF参数;
			default_server:设定默认虚拟主机
			例如:
				listen 172.16.100.8:80 default_server;   默认虚拟主机
				
		3、server_name name [...];
			后可跟多个主机名;名称还可以使用通配符和正则表达式(~);
			当存在多个主机名时,主机名的匹配优先顺序:	
			(1) 先做精确匹配;www.magedu.com: 
			(2) 左侧通配符匹配,例如:*.magedu.com; 
			(3) 右侧通配符匹配,例如:www.*;
			(4) 正则表达式匹配,例如: ~^.*\.magedu\.com$  (使用正则表达式匹配,要使用"~"起头)
			(5) default_server
			
		4、location [=|~|~*|^~] /uri {...}
			location @name
			功能:允许根据用户请求的URI来匹配定义的各location,匹配到时,此请求将被相应的location块中的配置所处理;
			      (为每个URI提供单独配置)
				=: 精确匹配检查;
				~: 正则表达式模式匹配,区分字符大小写;
				~*:正则表达式模式 匹配,不区分字符大小写;
				^~:URI的左半部分匹配,不检查正则表达式;
				
			匹配优先级:精确匹配(=)、^~、~和~*、由不带符号的URL进行左侧匹配;
				
		5、root
			设置web资源路径映射;用于指明请求的URL所对应的文档的目录路径;
			
			root /PATH/TO/DOCUMENTROOT;

			http://www.magedu.com/images/logo.jpg--->/PATH/TO/DOCUMENTROOT/images/logo.jpg
			

			location /images/ {
				root /data/imgs/;  表示访问/inages/路径下的资源,均来自/data/imgs/
			}
             http://www.magedu.com/images/logo.jpg--->/data/imgs/images/logo.jpg

		6、alias path
			只能用于location配置段,定义路径别名 
			
			location /images/ {
				alias /www/pictures/;
			}
			
			  /images/1.jpg--->/www/pictures/1.jpg
			注意:root表示指明路径为对应location的"/"URL;alias表示路径映射,即location中的URL是相对于alias所指明的路径而言;
			
		7、index file
			默认主页面
				index index.html; 
				
		8、error_page code [...] [=code] URI | @name
			根据http响应状态码重定向错误页面
			error_page  404   /404.html
			error_page  410=301  /abc.gif  #Nginx服务器产生410的http消息时,使用Nginx安装路径下/html/abc.gif返回给用户端301消息。
			=[code]: 以指定的响应码进行响应;省略code表示以新资源的响应码为响应码;
			
		9、try_files
			try_files path1[,path2,...] URI
				尝试查找第一个至N-1个文件,第一个即为返回给请求者的资源,如1至N-1个文件均不存在,则跳转至最后一个uri(应该由其他location定义,不能匹配当前location,否则会导致死循环)

配置Nginx的访问权限:

        8行

ngx_http_access模块的配置(基于IP的访问控制)		
  allow address | CIDR | unix: | all;
  deny address | CIDR | unix: | all;
  
   注:Nginx在配置解析的过程中,遇到deny指或者allow指令是按照顺序对当前客户端的连接进行访问权限检查的,如果遇到匹配的配置时,则停止继续向下搜索相关配置。

  应用上下文:http,server,location,limit_except

ngx_http_auth_basic_module模块的配置(基于用户的basic认证配置)

        16行

	auth_basic  string | off  #开启认证功能并配置指示信息,或者关闭认证功能
		使用http basic认证协议对用户进行认证
	auth_basic_user_file file #定义认证文件(file为密码文件的绝对路径)
	 这里的密码文件支持明文或者密码加密后的文件,明文的格式如下:
	    认证文件格式:
	    	name1:passwd1
	    	name2:passwd2[;注释]

		可以htpasswd命令创建用户账号加密文件;

		范例:
			location /admin/ {
				auth_basic "Admin Area"
				auth_basic_user_file /etc/nginx/.nginxhtpasswd;
			}
			
			
			
			
			
			
			
			
			-----------------------------本文章整理自马哥教育和<<Nginx 高性能web服务器详解释>> 版权归原作者所有---------------------------------------------


猜你喜欢

转载自blog.51cto.com/3037673/2158092