Nginx
http就是apache,现在国内很少了
nginx是开源的,是一款高性能,轻量级的web服务软件
不仅仅如此,
稳定性高,版本迭代比较快速(修复bug速度比较快,安全性快)
消耗系统资源很低,尤其是http的请求,并发连接.单台服务器可以支持30000-50000个并发请求(系统资源全部都分配给nginx)
单个节点的nginx一般支持20000个并发
(根据实际使用情况,可以使用分布式,以及其他程序也需要占用资源)
nginx的功能
1.静态页面(静态文件服务)
可以直接提提供静态文件服务,html css jsp 处理静态页面的响应速度很快,效率很高
2.代理功能:正向代理和反向代理
可以实现负载均衡,高可用和故障转移
3.动态内容处理.nginx并不能直接处理动态请求,可以通过中间键,把动态请求转发给后端服务器
4.支持https加密
5.可以实现重定向
6.虚拟主机,一个nginx可以配置多个域名和站点
7.nginx自带缓存机制
8.性能可扩展,处理能力可以随时调整
nginx的应用场景
1.静态页面
2.转发动态请求
3.反向代理,负载均衡
4.缓存服务
编译安装nginx
./configure --prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_ssl_module \ 支持https的加密功能ss/tls
--with-http_v2_module \ 支持http2.0协议
--with-http_realip_module \ 支持nginx获取客户端的真实ip地址
--with-http_stub_status_module \ 支持nginx获取访问状态信息的功能
--with-http_gzip_static_module \ 支持页面压缩文本
--with-pcre \ 支持pcre库
--with-stream \ 支持4层代理的模块
--with-stream_ssl_module \ 支持对tcp连接的加密
--with-stream_realip_module 支持从代理协议中获取客户端的真实ip地址make -j 4 && make install 启动四个cpu进行安装cd nginx/
配置文件目录
工作目录
日志目录 访问日志,报错日志
nginx的二进制启动脚本nginx.conf是主配置文件cd html
nginx常用命令
nginx -t 检测配置文件的语法是否正确
nginx -v 显示nginx的版本
nginx -V 显示版本和配置项
nginx -s 信号
stop关闭nginx
reload重新加载nginx,nginx -s reload无需重启服务
nginx配置文件
cd /usr/local/nginx/conf/
vim nginx.conf
1.全局模块
worker process 1
是nginx主进程的线程,其数字设置成服务器内核数的2倍,一般不超过8个,超过8个反而会降低性能,大多设置4个
处理进程的过程,必然涉及配置文件和展示页面,也就是涉及打开文件的数量
linux默认打开的文件数就是1024
#user nobody;
#设置成2
worker_processes 2;#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;#pid文件的文件
pid /usr/local/nginx/run/nginx.pid;#这里是events模块,它的作用,决定了nginx能够处理的连接数,和worker_processes的数值相乘
events {#1*2等于2万个,足够了,再多还有分布式呢worker_connections 10000;
}#转发和处理http请求,还有设置代理(正向代理和反向代理),缓存,定义日志格式,重定向配置
http {#文件扩展名与文件类型的映射表,nginx能够打开的文件和支持的文件类型include mime.types;#默认支持的文件类型.html .htm 可以自定义.jsp .phpdefault_type application/octet-stream;#日志格式,默认的日志格式,记录了access.log,访问日志的格式,嗯...报错也是这个格式#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;#默认就是异步非阻塞模式#tcp_nopush on;#keepalive_timeout 0;#连接保持的时间,单位是秒keepalive_timeout 65;#gzip 设置是否开启页面压缩,没啥用,前端才考虑#gzip on;#server在http模块里,开起web服务的模块server {#设置nginx的默认监听端口listen 80;#配置站点的域名server_name localhost;#网页的默认字符集#charset koi8-r;#默认的访问日志地址#access_log logs/host.access.log main;#网页匹配的工作目录的地址和支持的打开页面的文件类型#配置家目录下所有index文件#nginx工作目录的家目录就是/usr/local/nginx/location / {#root相当于拼接#/usr/local/nginx/htmlroot html;#alias是绝对路径#能打开的文件名称都是index或者index.htm或者index.htmlindex 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;#}# deny access to .htaccess files, if Apache's document root# concurs with nginx's one##location ~ /\.ht {# deny all;#}}
配置文件总结
1.全局模块
work_processes 1 指定进程数
events 模块决定了能够处理的连接数
stream 四层代理模块,只能写在全局模块里
2 .http模块
在http模块当中包含
server模块,可以有多个server模块
在server模块当中包含
location模块,可以有多个location模块
面试题
root匹配和alias匹配之间的区别
root的匹配模式是拼接
root的工作目录,访问的url /xy102
location /xy102
/opt/test1/
/opt/test1/xy102/
root可以写在server模块,也可以写在http,也可以写在location中
location /xy102
alias /opt/test1/xy102/
alias 只能卸载http模块当中server模块的location模块里
alias匹配工作目录,不能够使用重定向功能
#linux打开文件最大数的设置
vim /etc/security/limits.conf#能打开的进程最大数的软限制就是65535
* soft nproc 65535* hard nproc 65535* soft nofile 65535* hard nofile 65535
实验
location /status {
#打开状态统计的功能
stub_status on;
#关闭status访问日志
access_log off;
}
Active Connections: 1 当前活动的连接数
server accepts handled requests: 表示已经处理的连接数
三个数字,从左往右,已经处理的连接数,成功的建立连接的次数,已经处理的请求数
Reading: 0 Writing :1 Waiting:0 表示服务端正在从客户端读取请求的数据
writing 表示服务端正在把响应数据发送给客户端
waiting 表示有连接处于空闲状态,等待新的请求
基于密码的授权进行访问控制
httppasswd的工具
yum -y install httpd-tools
htpasswd -c /usr/local/nginx/passwd.db dn
123
123
chown nginx passwd.db
chmod 400 passwd.dbvim nginx.conf
location / {root html;index index.html index.htm;auth_basic "secret";auth_basic_user_file /usr/local/nginx/passwd.db;}
:wqnginx -t
systemctl restart nginx
基于客户端的访问控制
vim nginx.conf
location / {#添加一个控制规则/网段deny 192.168.118.20;allow all;
}
:wq
基于域名的nginx主机
vim nginx.conf
server {listen 80;server_name www.xy102.com;charset utf-8access_log logs/www.xy102.com.access.log;location / {root /var/www/html/xy102;index index.html;}location /xy102 {root /var/www/html;index index.html;}
}
:wqmkdir -p /var/www/html/xy102
ehcho "我们都爱学习" > /var/www/html/xy102/index.html
vim /etc/hosts
192.168.118.10 www.xy102.com
curl www.xy102.com
多个域名
vim nginx.conf
server {listen 80;server_name www.dnzs.com;charset utf-8;access_log logs/www.dnzs.com.access.log;location / {root /vat/www/html/dnzs;index index.html;}error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}
}
:wqmkdir -p /var/www/html/dnzs/
echo "我们也都爱学习" >
/var/www/html/dnzs/index.html
vim /etc/hosts
192.168.118.10 www.xy102.com www.dnzs.com
:wqcurl www.dnzs.com
基于ip地址的访问
ifconfig ens33:0 192.168.118.100/24vim nginx.conf
server {listen 192.168.118.10:80;server_name www.xy102.com;charset utf-8access_log logs/www.xy102.com.access.log;location / {root /var/www/html/xy102;index index.html;}location /xy102 {root /var/www/html;index index.html;}
}
server {listen 192.168.118.100:80;server_name www.dnzs.com;charset utf-8;access_log logs/www.dnzs.com.access.log;location / {root /vat/www/html/dnzs;index index.html;}error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}
}
:wqcurl 192.168.118.10
curl 192.168.118.100
基于端口实现多个虚拟主机
#改端口就行了
vim nginx.conf
server {listen 192.168.118.10:80;server_name www.xy102.com;charset utf-8access_log logs/www.xy102.com.access.log;location / {root /var/www/html/xy102;index index.html;}location /xy102 {root /var/www/html;index index.html;}
}
server {listen 192.168.118.10:8080;server_name www.dnzs.com;charset utf-8;access_log logs/www.dnzs.com.access.log;location / {root /vat/www/html/dnzs;index index.html;}error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}
}
:wq
curl 192.168.118.10:80
curl 192.168.118.10:8080
以多个配置文件的方式
vim nginx.conf
http {include mime.types;#可以识别到conf.d下,只包含server模块的conf文件include /usr/local/nginx/conf.d/*.conf;:wq
}
mkdir -p /usr/local/nginx/conf.d/
cd conf.d/
vim test1.conf
server {listen 8081;server_name localhost;location /test1 {root /opt/conf/;index index.html;}
}
server {listen 8082;server_name localhost;location /test2 {root /opt/conf/;index index.html;}
}
:wqnginx -t
cd /opt
mkdir -p conf/test1 && mkdir -p conf/test2cd conf/
echo "this is test1" > test1/index.html&&echo "this is test2" > test2/index.html