Nginx
是一个高性能的HTTP和反向代理服务。是一款轻量级的Web服务器和反向代理服务器及电子邮件代理服务器,特色:占有内存少,并发能力强,javascript
epoll:
在Linux 2.6内核中提出的select和poll的加强版本
支持水平触发LT和边缘触发ET,最大的特色在于边缘触发,它只告诉进程哪些fd刚刚变为就需态,而且只会通知一次
使用“事件”的就绪通知方式,经过epoll_ctl注册fd,一旦该fd就绪,内核就会采用相似callback的回调机制来激
活该fd,epoll_wait即可以收到通知
优势:
没有最大并发链接的限制:能打开的FD的上限远大于1024(1G的内存能监听约10万个端口),具体查
看/proc/sys/fs/file-max,此值和系统内存大小相关
效率提高:非轮询的方式,不会随着FD数目的增长而效率降低;只有活跃可用的FD才会调用callback函数,即epoll
最大的优势就在于它只管理“活跃”的链接,而跟链接总数无关
内存拷贝,利用mmap(Memory Mapping)加速与内核空间的消息传递;即epoll使用mmap减小复制开销php
基础特性:
特性:
模块化设计,一、较好的扩展性 二、高可靠性 三、支持热部署:不停机更新配置文件,升级版本,更换日志文件
四、低内存消耗:10000个keep-alive链接模式下的非活动链接,仅需2.5M内存
基本功能:
一、静态资源的web服务器 二、http协议反向代理服务器 三、pop3/imap4协议反向代理服务器 四、FastCGI(LNMP),uWSGI(python)等协议 五、模块化(非DSO),如zip,SSL模
Nginx是多进程组织模型,并且是一个由Master主进程和Worker工做进程组css
Nginx的安装:
一是yum的版本比较旧,二是编译安装能够更方便自定义相关路径,三是使用源码编译能够自定义相关功能,更方便业务的上的使用,源码安装须要提早准备标准的编译器,GCC的全称是(GNU Compiler collection),其有GNU开发,并以GPL即LGPL许可,是自由的类UNIX即苹果电脑Mac OS X操做系统的标准编译器,由于GCC本来只能处理C语言,因此原名为GNU C语言编译器,后来获得快速发展,能够处理C++,Fortran,pascal,objective-C,java以及Ada等其余语言,此外还须要Automake工具,以完成自动建立Makefile的工做,Nginx的一些模块须要依赖第三方库,好比pcre(支持rewrite),zlib(支持gzip模块)和openssl(支持ssl模块)等。html
使用安装完成的二进制文件nginx:
[root@s1 ~]# nginx -h
nginx version: nginx/1.12.2
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]
Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit #显示版本和编译参数
-t : test configuration and exit #测试配置文件是否异常
-T : test configuration, dump it and exit #测试并打印
-q : suppress non-error messages during configuration testing #静默模式
-s signal : send signal to a master process: stop, quit, reopen, reload #发送信号
-p prefix : set prefix path (default: /usr/share/nginx/) #指定Nginx 目录
-c filename : set configuration file (default: /etc/nginx/nginx.conf) #配置文件路径
-g directives : set global directives out of configuration file #设置全局指令java
Nginx的启动脚本:
yum安装:官方网站:https://nginx.org/packages/centos/7/x86_64/RPMS/
wget https://nginx.org/packages/centos/7/x86_64/RPMS/nginx-1.14.2-1.el7_4.ngx.x86_64.rpm
yum install nginx-1.14.2-1.el7_4.ngx.x86_64.rpm
默认启动脚本:
[root@centos7 ~]#vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
[Install]
WantedBy=multi-user.targetnode
默认配置文件:/etc/nginx/nginx.conf
[root@centos7 ~]#grep -v "#" /etc/nginx/nginx.conf | grep -v "^$"
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
include /etc/nginx/conf.d/.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
servername ;
root /usr/share/nginx/html;
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}python
systemctl start nginx 便可登陆了。mysql
编译安装:
150:yum安装,200:编译安装
官方网站:https://nginx.org/en/download.html
[root@centos7 src]cd /usr/local/src 源码包通常都存放于此
[root@centos7 src]#wget https://nginx.org/download/nginx-1.14.2.tar.gz
[root@centos7 src]#tar xvf nginx-1.14.2.tar.gz
[root@centos7 src]#cd nginx-1.14.2/
[root@centos7 nginx-1.14.2]#yum install -y vim lrzsz tree screen psmisc lsof tcpdump wget ntpdate
gcc gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel
net-tools iotop bc zip unzip zlib-devel bash-completion nfs-utils automake libxml2
libxml2-devel libxslt libxslt-devel perl perl-ExtUtils-Embednginx
[root@centos7 nginx-1.14.2]#ll
total 732
drwxr-xr-x. 6 1001 1001 4096 Mar 15 11:07 auto
-rw-r--r--. 1 1001 1001 288742 Dec 4 22:52 CHANGES
-rw-r--r--. 1 1001 1001 440121 Dec 4 22:52 CHANGES.ru
drwxr-xr-x. 2 1001 1001 168 Mar 15 11:07 conf
-rwxr-xr-x. 1 1001 1001 2502 Dec 4 22:52 configure
drwxr-xr-x. 4 1001 1001 72 Mar 15 11:07 contrib
drwxr-xr-x. 2 1001 1001 40 Mar 15 11:07 html
-rw-r--r--. 1 1001 1001 1397 Dec 4 22:52 LICENSE
drwxr-xr-x. 2 1001 1001 21 Mar 15 11:07 man
-rw-r--r--. 1 1001 1001 49 Dec 4 22:52 README
drwxr-xr-x. 9 1001 1001 91 Mar 15 11:07 srcc++
[root@centos7 nginx-1.14.2]#./configure --prefix=/apps/nginx
[root@centos7 nginx-1.14.2]#make / make install
[root@centos7 nginx-1.14.2]#ll /apps/
total 0
drwxr-xr-x. 6 root root 54 Mar 15 11:20 nginx
[root@centos7 nginx-1.14.2]#ll /apps/nginx/
total 4
drwxr-xr-x. 2 root root 4096 Mar 15 11:20 conf 存放配置文件
drwxr-xr-x. 2 root root 40 Mar 15 11:20 html 存放静态页面的目录
drwxr-xr-x. 2 root root 6 Mar 15 11:20 logs 存放日志
drwxr-xr-x. 2 root root 19 Mar 15 11:20 sbin 存放可执行程序
[root@centos7 nginx-1.14.2]#./configure --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
[root@centos7 nginx-1.14.2]#/apps/nginx/sbin/nginx -V
nginx version: nginx/1.14.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/apps/nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
[root@centos7 nginx-1.14.2]#/apps/nginx/sbin/nginx
[root@centos7 nginx-1.14.2]#/apps/nginx/sbin/nginx -s stop
如今在150主机上:
scp /usr/lib/systemd/system/nginx.service 172.18.9.200:/usr/lib/systemd/system/nginx.service
200主机:
[root@200 nginx-1.14.2]#vim /usr/lib/systemd/system/nginx.service
[Service]
#PIDFile=/var/run/nginx.pid
ExecStart=/apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf 把配置文件路径更改
[root@200 nginx-1.14.2]#systemctl daemon-reload
[root@200 nginx-1.14.2]#systemctl start nginx
[root@200 nginx-1.14.2]#vim /apps/nginx/conf/nginx.conf
user nginx nginx;
worker_processes 2; 2个进程
worker_cpu_affinity 0001 0010;
[root@200 nginx-1.14.2]#/apps/nginx/sbin/nginx -s reload
[root@200 nginx-1.14.2]#ps -ef |grep nginx
root 34192 1 0 16:33 ? 00:00:00 nginx: master process /apps/nginx/sbin/nginx
nginx 34321 34192 0 16:43 ? 00:00:00 nginx: worker process
nginx 34322 34192 0 16:43 ? 00:00:00 nginx: worker process
root 34324 20781 0 16:43 pts/0 00:00:00 grep --color=auto nginx
[root@200 nginx-1.14.2]#cat /apps/nginx/html/ index.html
172.18.9.200
此时Nginx配置完成能够用了。
下午第一节:
Nginx的默认配置文件:
[root@200 nginx]#grep -v "#" /apps/nginx/conf/nginx.conf | grep -v "^$" 将空行等过滤掉
user nginx nginx;
worker_processes 2;
worker_cpu_affinity 0001 0010;
pid logs/nginx.pid;
events {
worker_connections 1024;
use epoll;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
[root@200 nginx]#cd /apps/nginx/
[root@200 nginx]#ll
total 4
drwx------. 2 nginx root 6 Mar 15 13:49 client_body_temp
drwxr-xr-x. 2 root root 4096 Mar 15 17:28 conf
drwx------. 2 nginx root 6 Mar 15 13:49 fastcgi_temp
drwxr-xr-x. 2 root root 40 Mar 15 16:50 html
drwxr-xr-x. 2 root root 58 Mar 15 17:13 logs
drwx------. 2 nginx root 6 Mar 15 13:49 proxy_temp
drwxr-xr-x. 2 root root 36 Mar 15 15:40 sbin
drwx------. 2 nginx root 6 Mar 15 13:49 scgi_temp
drwx------. 2 nginx root 6 Mar 15 13:49 uwsgi_temp
[root@200 nginx]#cd conf
[root@200 conf]#ll
total 68
-rw-r--r--. 1 root root 1077 Mar 15 13:47 fastcgi.conf
-rw-r--r--. 1 root root 1077 Mar 15 15:40 fastcgi.conf.default
-rw-r--r--. 1 root root 1007 Mar 15 13:47 fastcgi_params
-rw-r--r--. 1 root root 1007 Mar 15 15:40 fastcgi_params.default
-rw-r--r--. 1 root root 2837 Mar 15 15:40 koi-utf
-rw-r--r--. 1 root root 2223 Mar 15 15:40 koi-win
-rw-r--r--. 1 root root 5170 Mar 15 13:47 mime.types
-rw-r--r--. 1 root root 5170 Mar 15 15:40 mime.types.default
-rw-r--r--. 1 root root 2705 Mar 15 17:28 nginx.conf
-rw-r--r--. 1 root root 2656 Mar 15 15:40 nginx.conf.default
-rw-r--r--. 1 root root 636 Mar 15 13:47 scgi_params
-rw-r--r--. 1 root root 636 Mar 15 15:40 scgi_params.default
-rw-r--r--. 1 root root 664 Mar 15 13:47 uwsgi_params
-rw-r--r--. 1 root root 664 Mar 15 15:40 uwsgi_params.default
-rw-r--r--. 1 root root 3610 Mar 15 15:40 win-utf
[root@200 conf]#vim mime.types
types {
text/html html htm shtml; 当访问互联网时,网络会根据这些后缀来解析。
text/css css;
text/xml xml;
image/gif gif;
image/jpeg jpeg jpg;
application/javascript js;
application/atom+xml atom;
application/rss+xml rss;
text/mathml mml; text/plain txt; text/vnd.sun.j2me.app-descriptor jad; text/vnd.wap.wml wml; text/x-component htc; image/png png; image/svg+xml svg svgz; image/tiff tif tiff; image/vnd.wap.wbmp wbmp; image/webp webp; image/x-icon ico; image/x-jng jng; image/x-ms-bmp bmp; application/font-woff woff; application/java-archive jar war ear; application/json json; application/mac-binhex40 hqx;
[root@centos7 ~]#vim /apps/nginx/conf/nginx.conf
http {
server {
listen 172.18.9.200:80;
listen 8080;
}
[root@centos7 ~]#/apps/nginx/sbin/nginx -t
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
[root@centos7 ~]#/apps/nginx/sbin/nginx -s reload
[root@centos7 ~]#/apps/nginx/sbin/nginx -s stop 关停重启后,8080才会出现。
[root@centos7 ~]#/apps/nginx/sbin/nginx
[root@centos7 ~]#ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 :111 :
LISTEN 0 128 :8080 :
LISTEN 0 128 172.18.9.200:80
3.1:全局配置:
user nginx nginx; #启动Nginx工做进程的用户和组worker_processes [number | auto]; #启动Nginx工做进程的数量worker_cpu_affinity 00000001 00000010 00000100 00001000; #将Nginx工做进程绑定到指定的CPU核心,默认Nginx是不进行进程绑定的,绑定并非意味着当前nginx进程独占以一核心CPU,可是能够保证此进程不会运行在其余核心上,这就极大减小了nginx的工做进程在不一样的cpu核心上的来回跳转,减小了CPU对进程的资源分配与回收以及内存管理等,所以能够有效的提高nginx服务器的性能。[root@s2 ~]# ps axo pid,cmd,psr | grep nginx20061 nginx: master process /apps 020062 nginx: worker process 020063 nginx: worker process 120097 grep --color=auto nginx 0123456
第二题:Location作访问路径匹配访问不一样页面显示不一样的内容
1、[root@200 conf]#ll /apps/nginx/
total 4
drwx------. 2 nginx root 6 Mar 15 13:49 client_body_temp
drwxr-xr-x. 2 root root 4096 Mar 15 17:56 conf
drwx------. 2 nginx root 6 Mar 15 13:49 fastcgi_temp
drwxr-xr-x. 2 root root 40 Mar 15 16:50 html
drwxr-xr-x. 2 root root 58 Mar 15 17:45 logs
drwx------. 2 nginx root 6 Mar 15 13:49 proxy_temp
drwxr-xr-x. 2 root root 36 Mar 15 15:40 sbin
drwx------. 2 nginx root 6 Mar 15 13:49 scgi_temp
drwx------. 2 nginx root 6 Mar 15 13:49 uwsgi_temp
[root@200 conf]#ll /apps/nginx/html 存放访问页面的文件
total 8
-rw-r--r--. 1 root root 537 Mar 15 13:47 50x.html
-rw-r--r--. 1 root root 13 Mar 15 13:57 index.html
2、配置文件中的location部分:
[root@centos7 ~]#vim /apps/nginx/conf/nginx.conf
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
#redirect server error pages to the static page /50x.html #error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } #proxy the PHP scripts to Apache listening on 127.0.0.1:80 #location ~ \.php$ { #proxy_pass http://127.0.0.1; #} #pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 #location ~ \.php$ { #root html; #fastcgi_pass 127.0.0.1:9000; #fastcgi_index index.php; #fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; #include fastcgi_params; #}
[root@centos7 ~]#vim /apps/nginx/conf/nginx.conf 此文件中的events中添加2项,设置为on
events {
worker_connections 1024;
use epoll;
multi_accept on;
accept_mutex on; }
[root@centos7 ~]#/apps/nginx/sbin/nginx -t
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
[root@centos7 ~]#/apps/nginx/sbin/nginx -s reload
[root@centos7 ~]#ps -ef |grep nginx 显示以下:
root 35110 1 0 17:45 ? 00:00:00 nginx: master process /apps/nginx/sbin/nginx
nginx 35111 35110 0 17:45 ? 00:00:00 nginx: worker process
nginx 35112 35110 0 17:45 ? 00:00:00 nginx: worker process
root 35596 24183 0 18:20 pts/1 00:00:00 grep --color=auto nginx
3、在c盘的hosts中加入“172.18.9.200 www.magedu.net”
[root@centos7 ~]#vim /apps/nginx/conf/nginx.conf
将“include /apps/nginx/conf.d/*.conf;”加在文末HTTP文档中
[root@centos7 ~]#mkdir /apps/nginx/conf/conf.d
[root@centos7 ~]#cd /apps/nginx/conf/conf.d
[root@centos7 conf.d]#ll
total 0
[root@centos7 conf.d]#vim pc.conf 先建起一个,能够先用着。
server {
listen 80;
server_name www.magedu.net;
location / {
root /data/nginx/html/pc;
}
#location /about {
#root /data/nginx/html/pc;
#index index.html;
}
}
[root@200 ~]#cd /apps/nginx/conf/conf.d
[root@200 conf.d]#mkdir /data/nginx/html/pc -p
[root@200 conf.d]#vim /data/nginx/html/pc/index.html
pc web
去访问www.magedu.net
[root@200 conf.d]#cp pc.conf mobile.conf
[root@200 conf.d]#vim mobile.conf
server {
listen 80;
server_name mobile.magedu.net;
location / {
root /data/nginx/html/mobile; }
#location /about {
#root /data/nginx/html/pc;
#index index.html;
#}
}
[root@200 conf.d]#/apps/nginx/sbin/nginx -t
[root@200 conf.d]#/apps/nginx/sbin/nginx -s reload
[root@200 conf.d]#mkdir /data/nginx/html/mobile
[root@200 conf.d]#vim /data/nginx/html/mobile/index.html
mobile web
此时去访问mobile.magedu.net
[root@200 conf.d]#vim /apps/nginx/conf/conf.d/pc.conf
server {
listen 80;
server_name www.magedu.net;
location / {
root /data/nginx/html/pc;
}
location /about { 注释去掉
root /data/nginx/html/pc;
index index.html;
}
}
[root@200 conf.d]#mkdir /data/nginx/html/pc/about
[root@200 conf.d]#vim /data/nginx/html/pc/about/index.html
about page
4、Nginx的location的配置:
location的详细使用:
= #用于标准uri前,须要请求字串与uri精确匹配,若是匹配成功就中止向下匹配并当即处理请求。
~ #区分大小写
~ #不区分大写
!~ #区分大小写不匹配
!~ #不区分大小写不匹配
^~ #匹配以什么开头
$ #匹配以什么结尾
\ #转义字符。能够转. * ?等
server {
location ~ /1.jpg {
root /data/nginx/html/images; }
}
[root@centos7 conf.d]# /apps/nginx/sbin/nginx -t
[root@centos7 conf.d]# /apps/nginx/sbin/nginx -s reload
三、
举例2:^~匹配以什么开头
[root@200 conf.d]#cd /data/nginx/html/pc
[root@200 pc]#mkdir images images
[root@200 pc]#mkdir images images1
[root@200 pc]#vim images/index.html
images
[root@200 pc]#vim images1/index.html
images1
[root@centos7 conf.d]#vim /apps/nginx/conf/conf.d/pc.conf
server {
location ^~ /images {
root /data/nginx/html/pc;
index index.html;
}
location^~/images1{
root /data/nginx/html/pc;
index index.html;
}
}
在配置文件中加入:这种格式作匹配最经常使用
[root@200 ~]#vim /apps/nginx/conf/conf.d/pc.conf
location ~* .(gif|jpg|jpeg|bmp|png|tiff|tif|ico|wmf|js)$ {
root /data/nginx/images;
index index.html;
}
[root@200 ~]#/apps/nginx/sbin/nginx -t
[root@200 ~]#/apps/nginx/sbin/nginx -s reload
此时咱们便可去访问所对应的各类格式的文件了!!!!
访问控制基于模块ngx_http_access_module实现,能够经过匹配客户端源IP地址进行限制。
1、[root@200 ~]#yum install httpd_tools -y
[root@200 ~]#htpasswd -cbm /apps/nginx/conf/.htpassword user1 123gxy
Adding password for user user1
[root@200 ~]#htpasswd -bm /apps/nginx/conf/.htpassword user2 123gxy
Adding password for user user2
[root@200 ~]#tail /apps/nginx/conf/.htpassword
user1:$apr1$S0Lwa3Ja$RLr9H7ILXMCyHJpZ0F8cP.
user2:$apr1$FspSBOm/$qTGj/QI8YzwqhBsMDryHo.
2、[root@s2 ~]# vim /apps/nginx/conf/conf.d/pc.conf
location = /login/ {
root /data/nginx/html/;
index index.html;
auth_basic "login password";
auth_basic_user_file /apps/nginx/conf/.htpasswd;
}
3、location /about {
alias /data/nginx/html/;
index index.html;
deny 172.18.9.1;
allow 1722.18.9.0/24;
allow 127.9.0.0/16;
deny all; #先容许小部分,再拒绝大部分
}
访问网页:
编辑login的网页:
vim /apps/nginx/conf/nginx.conf/pc.conf
location /login {
index index.html;
#auth_basic "input password";
#auth_basic_user_file /apps/nginx/conf/ .htpassword;
root /data/nginx/html;
}
/apps/nginx/sbin/nginx -t
/apps/nginx/sbin/nginx -s reload
访问网页以下:
做为下载服务器配置:
一、mkdir /data/nginx/html/pc/download
二、vim /apps/nginx/conf/conf.d/pc.conf
location /download {
autoindex on; #自动索引功能
autoindex_exact_size on; #计算文件确切大小(单位bytes),off只显示大概大小(单位kb、mb、gb)
autoindex_localtime on; #显示本机时间而非GMT(格林威治)时间
root /data/nginx/html/pc;
}
三、[root@s2 pc]# cp /root/anaconda-ks.cfg /data/nginx/html/pc/download/
四、重启Nginx并访问测试下载页面:
worker_processes [number | auto]; #启动Nginx工做进程的数量
worker_cpu_affinity 01 10; #将Nginx工做进程绑定到指定的CPU核心,默认Nginx是不进行进程绑定的,绑定并非意味着当前nginx进程独占以一核心CPU,可是能够保证此进程不会运行在其余核心上,这就极大减小了nginx的工做进程在不一样的cpu核心上的来回跳转,减小了CPU对进程的资源分配与回收以及内存管理等,所以能够有效的提高nginx服务器的性能。
#错误日志记录配置,语法:error_log file [debug | info | notice | warn | error | crit |alert | emerg]
#error_log logs/error.log;
#error_log logs/error.log notice;
error_log /apps/nginx/logs/error.log error;
#pid文件保存路径
pid /apps/nginx/logs/nginx.pid;
worker_priority 0; #工做进程优先级,-20~19
worker_rlimit_nofile 65536; #这个数字包括Nginx的全部链接(例如与代理服务器的链接等),而不只仅是与客户端的链接,另外一个考虑因素是实际的并发链接数不能超过系统级别的最大打开文件数的限制.
daemon off; #前台运行Nginx服务用于测试、docker等环境。
master_process off|on; #是否开启Nginx的master-woker工做模式。
二、events {
worker_connections 65536;
use epoll; #使用epoll事件驱动,Nginx支持众多的事件驱动,好比select、poll、epoll,只能设置在events模块中设置。
accept_mutex on; #优化同一时刻只有一个请求而避免多个睡眠进程被唤醒的设置,on为防止被同时唤醒默认为off,所有唤醒的过程也成为"惊群",所以nginx刚安装完之后要进行适当的优化。
multi_accept on; Nginx服务器的每一个工做进程能够同时接受多个新的网络链接,可是须要在配置文件中配置,此指令默认为关闭,即默认为一个工做进程只能一次接受一个新的网络链接,打开后几个同时接受多个,
三、http {
include mime.types; #导入支持的文件类型
default_type application/octet-stream; #设置默认的类型,会提示下载不匹配的类型文件
#日志配置部分
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
#自定义优化参数
sendfile on; 指定是否使用sendfile系统调用来传输文件,sendfile系统调用在两个文件描述符之间直接传递数据(彻底在内核中操做),从而避免了数据在内核缓冲区和用户缓冲区之间的拷贝,操做效率很高,被称之为零拷贝,硬盘 >> kernel buffer (快速拷贝到kernelsocket buffer) >>协议栈。
#tcp_nopush on; #在开启了sendfile的状况下,合并请求后统一发送给客户端。
#tcp_nodelay off; #在开启了keepalived模式下的链接是否启用TCP_NODELAY选项,当为off时,延迟发送,合并多个请求后再发送,默认On时,不延迟发送,当即发送用户相应报文。
#keepalive_timeout 0;
keepalive_timeout 65 65; #设置会话保持时间
#gzip on; #开启文件压缩
listen 80; #设置监听地址和端口
server_name localhost; #设置server name,能够以空格隔开写多个并支持正则表达式,如
*.magedu.com www.magedu.* ~^www\d+.magedu.com$
#charset koi8-r; #设置编码格式,默认是俄语格式,能够改成utf-8
location / {
root html;
index index.html index.htm;
} #
error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
# error_page 500 502 503 504 /50x.html; #定义错误页面
location = /50x.html {
root html;
} #