############ 认识nginx #############
Nginx:(发音同 engine x)是一款轻量级的Web服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。由俄罗斯的程序设计师Igor Sysoev所开发,最初供俄国大型的入口网站及搜寻引擎Rambler(俄文:Рамблер)使用。 其优点是轻量级(占有内存少),高并发(并发能力强),事实上nginx的并发能力确实在同类型的网页伺服器中表现较好.目前中国大陆使用nginx网站用户有:新浪、网易、 腾讯,另外知名的微网志Plurk也使用nginx######## 搭建基于nginx的web服务器 ############1.通常情况下nginx只处理静态的网页请求,即html.如果是来自动态的网页请求,如*.php,则nginx就要根据正则表达式查询路径,然后把*.PHP交给PHP去处理2.1)安装make、gcc、源码安装nginxyum install -y make gcctar zxf nginx-1.10.1.tar.gz ##解压包cd nginx-1.10.1 ##进入目录/configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module ##安装配置yum install -y pcre-devel ##在安装配置时,安装缺少的函数库(缺少什么安装什么)pcre是nginx支持具备URI重写功能的rewrite模块yum install -y openssl-devel ##在使用HTTPS服务时候必须装openssl-develmake;make install ##make进行编译 make install生成文件到系统ln -s /usr/local/nginx/sbin/nginx /usr/sbin ##建立软链接,方便之后管理
2)vim auto/cc/gcc # debug#CFLAGS="$CFLAGS -g" ##注释debug功能,可减少编译占用资源vim src/core/nginx.h#define NGINX_VERSION "1.10.1”#define NGINX_VER "nginx" ##修改此行,去掉后面的“NGINX_VERSION”,为了安全,这样编译后外界无法获取程序的版本号3)检查错误:[root@localhost nginx-1.10.1]# nginx -t ##检查nginx配置文件是否有语法错误nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful4)开启nginx并查看nginx服务状态nginx ##开启nginxnetstat -anlpt|grep 80 ##查看状态5)浏览器访问:http://172.25.8.1【注意防火墙:/etc/init.d/iptables stop】6)nginx -s reload ##重新加载nginx服务(现在使用的)kill -s HUP nginx ##重新加载配置文件,相当于“killall -1 nginx”(之前使用的)kill -s QUIT nginx ##安全退出,相当于“kill -3 nginx”kill -s TERM nginx ##快速退出,不等待处理完当前连接############ nginx的多核配置、进程限制 ############1.多核配置cd /usr/local/nginx/conf ##进入目录useradd -u 900 -d /usr/local/nginx nginx ##-u指定uid,-d指定目录(如果有就不用建立了)vim nginx.conf ##编辑文件 user nginx nginx; ##添加该内容,定义nginx的用户和用户组nginx -t ##查看nginx服务是否配置成功nginx -s reload ##重新加载nginx服务ps aux| grep nginx ##查看nginx进程个数关闭server1虚拟机,打开虚拟机管理器,将CPU个数添加为两个,点击processor,cpu添加为2个,重新打开server1 lscpu ##查看CPU个数vim nginx.conf ##编辑文件 user nginx;worker_processes 2; ##将work_processes 后接参数改为2 worker_cpu_affinity 01 10; ##利用多核cpu配置,01 10表示开启两个进程,第一个进程对应第一个CPU内核,第二个进程对应第二个CPU内核【8核cpu,开启两个进程,举例:: 10101010 01010101; 两组数表示开启了2个进程,位数可对应着cpu个数,10101010表示开启了2,4,6,8内核,01010101表示开启了1,3,5,7内核】nginx -t ##查看nginx服务是否配置成功nginx ##开启nginxnginx -s reload ##重新加载nginx服务【如果出现nginx: [error] open() "/usr/local/lnmp/nginx/logs/nginx.pid" failed (2: No such file or directory) 则说明nginx没开,使用nginx命令开启】ps aux| grep nginx ##查看nginx进程个数返回内容:root 2567 0.0 0.1 45220 1776 ? Ss 19:51 0:00 nginx: master process nginxnginx 5261 0.0 0.1 45636 2004 ? S 20:51 0:00 nginx: worker processnginx 5262 0.0 0.1 45636 1836 ? S 20:51 0:00 nginx: worker process2.进程限制1)su nginxulimit -a ##查看当前用户进程限制返回:open files (-n) 1024 max user processes (-u) 10242)su rootvim /etc/security/limits. conf在最后添加: nginx - nofile 200 ##该用户文件限制为200nginx - nproc 200 ##该用户进程限制为2003)su - nginx 切换到该用户ulimint -a 来查看返回内容:open files (-n) 200max user processes (-u) 2004)cd /usr/local/nginx/conf/ vim nginx.conf ##编辑文件修改参数 events { use epoll; worker_connections 1024; ##这里为root的总进程数} ##在server位置编添加下列参数location /status { ##/status为状态文件目录 stub_status on; ##开启查看nginx状态信息 access_log off; }5)检查、重新加载nginx -t ##查看nginx服务是否配置成功nginx -s reload ##重新加载nginx服务6)测试curl 172.25.8.1/status ##命令行测试查看进程数返回内容:Active connections: 1 server accepts handled requests 3 3 4 Reading: 0 Writing: 1 Waiting: 0 ab -n 1000 -c 500 http://172.25.8.1/index.html ##压力测试,确保已经启动nginx(-n表示请求次数,1000个进程 -c表示一次请求并发次数,500个)】curl 172.25.8.1/status ##再次查看进程数返回内容:Active connections: 1 ##表示活跃的连接数server accepts handled requests 1112 1112 1113 ##总共处理了1112个连接,1112次握手,总共处理了1113个请求Reading: 0 Writing: 1 Waiting: 0 ##Reading读取客户连接数,Writing响应数据到客户端的数量,Waiting正在等候下一次请求指令的驻留连接############## nginx加密协议https的访问 ##############1)vim nginx.conf ##打开文件去掉下列参数并修改server { listen 443 ssl; server_name server1.example.com; ##修改为服务器名 ssl_certificate cert.pem; ssl_certificate_key cert.pem; ##修改为cert.pem 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; } }2)cd /etc/pki/tls/certs/ make cert.pem ##生成这个文件 参数为下Country Name (2 letter code) [XX]:CNState or Province Name (full name) []:ShaanxiLocality Name (eg, city) [Default City]:xi'anOrganization Name (eg, company) [Default Company Ltd]:redhatOrganizational Unit Name (eg, section) []:linuxCommon Name (eg, your name or your server's hostname) []:server1.example.comEmail Address []:root@server1.example.commv cert.pem /usr/local/nginx/conf ##cert.pem放置在/usr/local/nginx/conf下3)nginx -t ##查看nginx服务是否配置成功nginx -s reload ##重新加载nginx服务4)浏览器访问https://172.25.8.1(选择 I Unserstand The Risks,添加认证)########### nginx配置参数的解释 ###########
--prefix=path 定义一个目录,存放服务器上的文件 ,也就是nginx的安装目录。默认使用 /usr/local/nginx。
--sbin-path=path 设置nginx的可执行文件的路径,默认为 prefix/sbin/nginx.--conf-path=path 设置在nginx.conf配置文件的路径。nginx允许使用不同的配置文件启动,通过命令行中的-c选项。默认为prefix/conf/nginx.conf.--pid-path=path 设置nginx.pid文件,将存储的主进程的进程号。安装完成后,可以随时改变的文件名 ,在nginx.conf配置文件中使用 PID指令。默认情况下,文件名 为prefix/logs/nginx.pid.--error-log-path=path 设置主错误,警告,和诊断文件的名称。安装完成后,可以随时改变的文件名 ,在nginx.conf配置文件中 使用 的error_log指令。默认情况下,文件名 为prefix/logs/error.log.--http-log-path=path 设置主请求的HTTP服务器的日志文件的名称。安装完成后,可以随时改变的文件名 ,在nginx.conf配置文件中 使用 的access_log指令。默认情况下,文件名 为prefix/logs/access.log.--user=name 设置nginx工作进程的用户。安装完成后,可以随时更改的名称在nginx.conf配置文件中 使用的 user指令。默认的用户名是nobody。--group=name 设置nginx工作进程的用户组。安装完成后,可以随时更改的名称在nginx.conf配置文件中 使用的 user指令。默认的为非特权用户。--with-select_module --without-select_module 启用或禁用构建一个模块来允许服务器使用select()方法。该模块将自动建立,如果平台不支持的kqueue,epoll,rtsig或/dev/poll。--with-poll_module --without-poll_module 启用或禁用构建一个模块来允许服务器使用poll()方法。该模块将自动建立,如果平台不支持的kqueue,epoll,rtsig或/dev/poll。--without-http_gzip_module 不编译压缩的HTTP服务器的响应模块。编译并运行此模块需要zlib库。--without-http_rewrite_module 不编译重写模块。编译并运行此模块需要PCRE库支持。--without-http_proxy_module 不编译http_proxy模块。--with-http_ssl_module 使用https协议模块。默认情况下,该模块没有被构建。建立并运行此模块的OpenSSL库是必需的。--with-pcre=path 设置PCRE库的源码路径。PCRE库的源码(版本4.4 - 8.30)需要从PCRE网站下载并解压。其余的工作是Nginx的./ configure和make来完成。正则表达式使用在location指令和 ngx_http_rewrite_module 模块中。--with-pcre-jit 编译PCRE包含“just-in-time compilation”(1.1.12中, pcre_jit指令)。--with-zlib=path 设置的zlib库的源码路径。要下载从 zlib(版本1.1.3 - 1.2.5)的并解压。其余的工作是Nginx的./ configure和make完成。ngx_http_gzip_module模块需要使用zlib 。--with-cc-opt=parameters 设置额外的参数将被添加到CFLAGS变量。例如,当你在FreeBSD上使用PCRE库时需要使用:--with-cc-opt="-I /usr/local/include。.如需要需要增加 select()支持的文件数量:--with-cc-opt="-D FD_SETSIZE=2048".--with-ld-opt=parameters 设置附加的参数,将用于在链接期间。例如,当在FreeBSD下使用该系统的PCRE库,应指定:--with-ld-opt="-L /usr/local/lib".