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网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。 1、nginx能够实现的功能 一、能够做为静态资源的web服务器、能够缓存文件的资源描述符——加速 二、支持对http、stmp、pop3等多种协议的反向代理 三、支持实现缓存和负载均衡 四、支持fcgi 五、支持uWSCGI 六、支持模块化 七、支持过滤器,对特定文件进行压缩传输 八、支持ssl 九、支持图像大小调整 二.、nginx的特性 一、模块 二、高性能 三、低内存消耗 四、支持热部署 五、支持异步IO 六、支持事件驱动 七、支持内存映射 3、核心模块 一、标准http模块 二、拓展http模块 三、邮件拓展模块 四、第三方模块 4、安装依赖 [root@156 ~]# yum groupinstall "Development Tools" -y [root@156 ~]# yum install pcre-devel openssl openssl-devel -y 5、安装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配置详解 6、和nginx正常运行相关的配置 一、worker_processes 1; 设置发起几个nginx的worker进程 二、user userName [groupName]; 指定nginx以谁的身份来运行 三、pid /path/to/file pid文件 四、worker_rlimit_nofile number; 设置全部worker进程一共能够打开的文件的最大数量值 (linux上默认每一个用户能够打开1024个文件、套接字也是一个文件) 7、nginx性能优化相关配置 五、worker_processes number | auto; 设置发起几个nginx的worker进程 number:应该比物理核心少一个 auto:让系统自动判断 六、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以后能够用 七、time_resolution interval; 下降发起系统调用gettimeofday()次数 time_resolution 100ms; 八、worker_priority number; 修改worker进程的nice值(默认是0) 8、和事件相关的配置 九、accept_mutex on | off 设置master进程将客户端的请求调度到worker进程的 调度方式:轮询、随机 on:使用轮询 默认方式:off 十、accept_mutex_delay time; 默认500ms 设置master延迟多久才将客户端的请求调度到worker进程 十一、user [epoll|poll|select] 一般是不须要指定链接的处理方法(起始就是所使用的事件模型)的 建议让系统自动判断所要使用的方法 十二、worker_connections number; 指定每一个worker进程能够处理的最大并发链接的数量 默认是1024 1三、master_process on | off; 指定是否使用master-worker 默认是:on 9、和调试和定位bug相关配置 1四、damon off | on 指定nginx工做在前台仍是后台 默认是 on 1五、error_log file [level]; 指定错误日志的保存位置以及日志级别 总结:常常要修改的参数 worker_processes worker_connections worker_cpu_affinity worker_priority 10、和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{ } } 1六、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在进行域名匹配的时候,是有必定的规则,匹配次序以下 一、作精确匹配,server_name中的内容必须和用户请求的内容彻底同样 二、检查左侧的通配符 三、检查右侧的通配符 四、检查通配符 五、检查正则表达式 六、经过全部检查都失败,则访问default_server,若是没有指定default_server, 那么第一个server就是default_server 1七、listen 指定所监听的套接字 例子: listen 127.0.0.1:80; listen 172.0.0.1; listen 80; listen *:80; listen localhost:80; 1八、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 "<h1>test for web1</h1>">/vhost/web1/index.html [root@156 ~]# cat /vhost/web1/index.html [root@156 ~]# nginx -s reload http://192.168.70.156/ test for web1 1九、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做路径映射的时候,有多种方式 不带符号 = ~ ~* ^~ 一、不带符号,以下 location /img { root /vhost/web1; } http://www.baidu.com/img http://www.baidu.com/a.html 二、= 表示的是精确匹配 location = /img { root /vhost/web1; } 要求只有用户所请求的uri和location所指定的uri必须 彻底一致,不然匹配失败,以下 http://www.baidu.com/img <<匹配成功 http://www.baidu.com/img/index.html <<匹配失败 三、~表示对用户所请求的uri作正则表达式,检查过程当中区分大小写 location ~ .jgp$ { root /vhost/web1; } 四、~*表示对用户所请求的uri作正则表达式,检查过程当中不区分大小写 五、^~不对用户所请求的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"