Nginx网站服务
一、nginx—web服务器
1、概念
http就是apache,现在国内很少,nginx是开源的,是一款高性能,轻量级的web服务软件。稳定性高,而且版本迭代比较快(修复bug速度比较快,安全性高),主流22和24,消耗资源很低,http的请求并发连接,单台服务器可以支持30000~~50000个并发请求(系统资源全部分配给nginx)。单个节点的nginx一般支持20000个并发
2、nginx的功能介绍
2.1 静态文件服务:静态页面,可以直接提供静态文件服务,html、css、jsp。处理静态页面的响应速度很快,效率很高
2.2 代理:正向代理,反向代理。可以实现负载均衡,高可用和故障转移
2.3 动态内容处理,nginx并不能直接处理动态请求,可以通过中间件把动态请求转发给后端服务器(nginx 、php、mysql、tomcat)
2.4 支持https加密
2.5 可以实现重定向
2.6 虚拟主机,一个nginx可以配置多个域名和站点
2.7 nginx自带缓存机制
2.8 性能可扩展,处理能力可以随时调整
3、nginx应用场景
3.1 静态页面
3.2 转发动态请求
3.3 反向代理,负载均衡
3.4 缓存服务
./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 \ #支持prce库
--with-stream \ #支持4层代理的模块
--with-stream_ssl_module \ #支持对tcp连接的加密
--with-stream_realip_module #支持从代理协议中获取客户端的真实ip地址
配置文件目录 nginx.conf 主配置文件
工作目录
日志目录(访问日志和报错日志)
nginx的二进制启动脚本
4、nginx常用命令
nginx -t
:检测配置文件的语法是否正确
nginx -v
:显示nginx的版本
nginx -V
:显示版本和配置项
nginx -s
:s指的是信号
nginx -s stop
:关闭nginx服务
nginx -s reload
:重新加载nginx
如果更改了配置文件,使用nginx -s reload
命令则无需重启服务
rpm -q nginx
:查看是否安装nginx
make -j 4 && make install
:启动四个cpu进行处理
二、nginx配置文件
nginx.conf 配置文件的作用
1、全局模块
worker_process 1;
工作进程数,设置成服务器内核数的2倍(一般不超过8个,超过8个反而会降低性能,一般4个,1~2个最好)
处理进程的过程必然涉及配置文件和展示页面,也就是涉及打开文件的数量
linux默认打开的文件数就是1024个
vim /etc/security/limits.conf
#这个配置生效只能重启,这是系统初始化的一个环节
*表示任意
# 在<domain> <type> <item> <value>下添加
* soft nproc 65535
#能打开的进程最大数的软限制是65535,65535是最大
*表示任意 hard nproc 65535
#能打开的进程最大数的硬限制是65535,65535是最大
* soft nofile 65535
#进程打开文件数的最大值655355
* hard nofile 655355
#设置所有用户的打开文件数(nofile)的硬限制
2、nginx配置文件的含义
vim /usr/local/nginx/conf/nginx.conf
#user nobody
#默认程序用户就是nginx,这里可以保持注释无需修改
#
worker_processes 1;
#
pid /usr/local/nginx/run/nginx.pid;
#pid文件的位置
events {
worker_connections 10000;
}
#events模块,决定了nginx能够处理的连接数,连接数worker_processes的数值相乘
#http模块的转发和处理http请求,设置代理(正向代理,反向代理),缓存,定义日志格式,重定向配置
http {
include mime.types;
#文件扩展名与文件类型的映射,作用:nginx可以打开的文件和支持的文件类型
default_type application/octet-stream;
#默认支持的文件类型,.html.htm .jsp .js .php
#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,即访问日志格式,error.log也是一样的格式,这个可以改,但是一般没人动
#access_log logs/access.log main;
#默认的访问日志的存放路径,一般不动
sendfile on;
#支持文件发送或者下载
#tcp_nopush on;
#默认就是异步非阻塞模式功能
#keepalive_timeout 0;
keepalive_timeout 65;
#连接保持时间,单位是秒
#gzip on;
#gzip模块,设置是否开启页面压缩功能
#server模块式开启web服务的模块
server {
listen 80;
#nginx的默认监听端口
server_name localhost;
#配置站点的域名
#charset koi8-r;
#网页默认字符集(不支持中文)
#charset utf-8;
#access_log logs/host.access.log main;
#默认访问日志的地址
#网页匹配的工作目录的地址和支持打开页面的文件类型
location / {
root html;
# root表示的是nginx工作目录的家目录 /usr/local/nginx/html,是拼接模式,不需要绝对路径
#alias也是指匹配nginx的工作目录。但是需要绝对路径
index index.html index.htm;
# 如果需要添加别的类型的文件可在index.htm后添加index.jsp等;
}
location网页匹配的工作目录的地址和支持的打开页面的文件类型
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
3、修改nginx的工作的家目录
vim /usr/local/nginx/conf/nginx.conf
# 修改location模块
location /xy102 {
root /opt/test1;
index index.html index.htm index.jsp;
}
# 192.168.100.11/opt/xy102/index.html
wq!
nginx -t
mkdir -p /opt/test1/xy102
cd /opt/test1/xy102
echo "天天开心" > index.html
# 注意配置文件里面的字符集需要修改成中文,不然不显示
systemctl restart nginx
页面访问 192.168.100.11/xy102/index.html 访问结果为天天开心
cd /opt/test1
mkdir xy103
echo 456 > index.html
# 修改location下的家目录root为alias
location /xy103 {
alias /opt/test1/xy103;
#alias也是指匹配nginx的工作目录。需要绝对路径
index index.html index.htm index.jsp;
}
wq!
nginx -t
systemctl restart nginx
页面访问 192.168.100.11/xy103 访问结果为456
4、root、alias匹配工作目录
root的匹配模式是拼接,root的工作目录,访问的uri /xy102
location /xy102
/opt/test1/xy102/
alias 匹配nginx的工作目录,路径是绝对路径
location/xy102
alias /opt/test1/xy102/;
alias 只能写在http模块当中server模块的location模块里面
root可以写在server模块,也可以在http,也可以在location中
使用alias匹配工作目录不可以使用重定向功能
5、总结
worker_process 1;
:全局模块,指定进程数
events模块
决定了能够处理的连接数
stream
:四层代理模块
http
:转发和处理http请求,设置代理(正向代理、反向代理),缓存,定义日志格式,重定向配置
在http模块
当中,包含: server块,http里面可以有多个server模块
在server模块
当中,包含:location模块,server里面可以有多个location模块
二、实验
1、统计访问状态
vim nginx.conf
location /status {
stub_status on;
#打开状态统计的功能
access_log off;
#关闭status的访问日志
}
2、访问ip代表的意思
Active connections:1
# 当前活动的连接数
server accepts handled requests
# 已经处理的连接数
36 36 36
# 三个数字从左往右:已经处理的连接数,成功建立连接的次数,已经处理的请求数
Reading: 0 writing:1 Waiting:0
# 表示服务端正在从客户端读取请求的数据
# 表示服务端正在把响应数据发送给客户端
# 表示有连接处于空闲状态,等待新的请求
3、基于密码的授权进行访问控制
yum -y install httpd-tools
#httppasswd的工具,要先安装
htpasswd -c /usr/local/nginx/passwd.db chengqian
cd /usr/local/nginx
chown nginx passwd.db
chmod 400 passwd.db
cd conf/
vim nginx.conf
# 在location下添加密码验证
auth_basic "secret";
# 开启用户密码验证
auth_basic_user_file /usr/local/nginx/passwd.db
# 开启加密模式。使用指定的加密文件
wq!
4、基于客户端的访问控制——根据ip地址来进行控制
vim nginx.conf
# 在location下添加
deny 指定ip;
allow all;
# deny表示禁止指定ip访问nginx,allow表示允许
deny 指定网段;
allow all;
5、基于域名的nginx主机访问
vim nginx.conf
# 在server模块下,把server_name后面的localhost改为域名(如 www.xy102.com)
# 在配置站点的域名下增加访问日志
# 在location模块下,把root后面的工作目录修改为/var/www.html/xy102;
server_name www.xy102.com;
access_log logs/www.xy102.com.access.log;
root /var/www/html/xy102;
wq!
# nginx -t 查看语法是否有误
# 正确 systemctl restart nginx
# 命令行操作
mkdir -p /var/www/html/xy102
echo "world is beautiful" > /var/www/html/xy102/index.html
# 通过域名访问(www.xy102.com),由于没有注册dns,需要做一个本地映射
vim /etc/hosts
192.168.100.11 www.xy102.com
# 添加映射,即访问192.168.100.11相当于访问www.xy102.com
wq!
# nginx -t 查看语法是否有误
正确 curl 192.168.100.11
# 打印结果 world is beautiful
6、多个域名的nginx主机访问,在基于域名的nginx主机访问上添加新的域名
vim nginx.conf
# 在server结束的花括号后再添加一个server
server {
listen 80;
server_name www.halo.com;
charset utf-8;
access_log logs/www.halo.com.access.log;
location / {
root /var/www/html/halo;
index index.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
wq!
# nginx -t 查看语法是否有误
# 正确 systemctl restart nginx
# 命令行操作
mkdir /var/www/html/halo
echo "nihao" > /var/www/html/halo/index.html
# 做一个本地映射
vim /etc/hosts
192.168.100.11 www.xy102.com www.halo.com
wq!
curl www.halo.com
#打印结果 nihao
7、基于ip地址的nginx主机访问
命令行操作
ifconfig ens33:0 192.168.100.100/24
# 添加虚拟网卡ens33:0,我们用虚拟网卡来做此实验,此实验基于多域名访问nginx主机,ip访问需要修改端口
vim nginx.conf
server {
listen 192.168.100.11:80;
server_name www.xy102.com;
# 80端口前添加192.168.100.11:操作成功即上图
server {
listen 192.168.100.100:80;
server_name www.halo.com;
# 80端口前添加192.168.100.10:操作成功即上图
wq!
# nginx -t 查看语法是否有误
# 正确 systemctl restart nginx
页面访问
192.168.100.11
# 打印结果 world is beautiful
192.168.100.100
# 打印结果 nihao
8、基于端口的nginx的主机访问
# 实验基于ip地址访问nginx主机
vim nginx.conf
server {
listen 192.168.100.11:8080;
server_name www.xy102.com;
# 把80端口改为8080
server {
listen 192.168.100.100:8082;
server_name www.halo.com;
# 把80端口改为8082
# 只有端口没有被占用,就可以使用
wq!
# nginx -t 查看语法是否有误
# 正确 systemctl restart nginx
192.168.100.11:8080
# 打印结果 world is beautiful
192.168.100.100:8082
# 打印结果 nihao
cd ..
cd logs
ls
# www.xy102.com.access.log www.halo.com.access.log
cat www.xy102.com.access.log
cat www.halo.com.access.log
# 此时他们的访问日志以分开,可以分别查看他们的访问日志
7、多个配置文件
vim nginx.conf
# 在http模块下的include下添加下面命令
include /usr/local/nginx/conf.d/*.conf;
# 可以识别到conf.d下,只包含server模块的conf文件
命令行
[root@localhost nginx]# mkdir conf.d
[root@localhost nginx]# cd conf.d
[root@localhost conf.d]# ls
[root@localhost conf.d]# vim test1.conf
vim test1.conf
server {
listen 8888;
server_name localhost;
location /test1 {
root /opt/conf/;
index index.html;
}
}
server {
listen 8899;
server_name localhost;
location /test2 {
root /opt/conf/;
index index.html;
}
}
wq!
# nginx -t 查看语法是否有误
# 正确 systemctl restart nginx
命令行
[root@localhost opt]# mkdir -p conf/test1
[root@localhost opt]# mkdir -p conf/test2
[root@localhost opt]# ls
conf
[root@localhost opt]# cd conf/
test1 test2
[root@localhost conf]# echo "this is test1" > test1/index.html
[root@localhost conf]# echo "this is test2" > test2/index.html
[root@localhost conf]# cd /usr/local/nginx/conf.d/
[root@localhost conf.d]# netstat -antp | grep nginx
可以查看到刚刚的端口
页面访问
192.168.100.11:8888/test1
# 打印结果为this is test1
192.168.100.11:8899/test2
# 打印结果为this is test2
nginx的优化与防盗链:
查看版本号
1.隐藏版本号
查看版本号:以192.168.100.11:8899/test2为例
页面查看,f12后,f5可以查看到nginx的版本号,,以下为隐藏版本号操作
命令行查看,curl -I 192.168.100.11:8899/test2
vim nginx.conf
# 在http模块下添加
server_tokens off;
# 关闭版本号
wq!
# nginx -t 查看语法是否有误
# 正确 systemctl restart nginx
此时查看版本号,版本号已隐藏
### server_tokens on; 开启版本号
2.隐藏版本号的另外一种方法
cd /opt/nginx-1.22.0/src/core
vim nginx.h
修改下面两个命令
# define NGINX_VERSION "1.22.0"
# define NGINX_VER "nginx/" NGINX_VERDION
修改后
define NGINX_VERSION "wo zhen shuai"
define NGINX_VER "ni bie cai"
wq!
[root@localhost core]# cd /opt/nginx-1.22.0
[root@localhost nginx-1.22.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
[root@localhost nginx-1.22.0]# make -j 4 && make install
此时页面访问版本号变成 ni bie cai/wo zhen shuai
命令行访问 curl -I 192.168.100.11:8899/test2 同样如此
# 注意此ip地址是虚拟ip地址,不是永久,重新开启虚拟机需要再创建虚拟ip地址
注意:域名访问nginx和ip访问nginx主机的区别?
答:域名默认端口为80不需要修改,ip访问需要修改端口