Nginx是一个高性能的HTTP和反向代理服务器,特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好。
Nginx专为性能优化而开发,性能是其最重要的考量,实现上非常注重效率,能经受高负载的考验,有报告表明能支持高达50,000个并发连接数。
HTTP的反向代理服务器
负载均衡
单个服务器解决不了,我们增加服务器的数量,然后将请求分发到各个服务器上,将原先请求集中到单个服务器上的情况改为将请求分发到多个服务器上,将负载分发到不同的服务器,也就是我们所说的负载均衡。
动静分离(动态静态资源分离)
为了加快网站的解析速度,可以把动态页面和静态页面由不同的服务器来解析,加快解析速度,降低原来单个服务器的压力。
这里记录的是使用yum命令,如果手动安装,需要先安装prre.tar.gz 、openssl.tar.gz、 zlib.tar.gz等依赖,再安装nginx.tar.gz,这里不再赘述。
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/7/@basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
执行yum list | grep nginx查看稳定版的源
然后选择稳定版运行yum install nginx安装(出现询问输入y)
输入nginx -v或whereis nginx验证是否安装成功
Mac OS的安装
通过工具brew安装
brew install nginx
Windows不推荐安装
## 全局块:从配置文件开始到events块之间的内容,主要设置一些影响整体运行的配置指令
# 运行用户,默认是nginx
user nginx;
# nginx进程数,一般默认为何cpu核数一样
worker_processes auto;
# 全局错误日志路径
error_log /var/log/nginx/error.log notice;
# 进程pid路径
pid /var/run/nginx.pid;
## events块:涉及的指令主要影响Nginx服务器与用户的网络连接
events {
# 最大连接数
worker_connections 1024;
}
## http块,包括http全局块和server块,Nginx服务器配置中最频繁的部分
#设置http服务器
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
# 开启高效传输模式
sendfile on;
#tcp_nopush on;
# 长链接超时时间,单位秒
keepalive_timeout 65;
# 传输的时候是否需要压缩
#gzip on;
# 加载其他配置文件
include /etc/nginx/conf.d/*.conf;
}
default.conf
/etc/nginx/conf.d/default.conf
## server块,包括全局server块和location块,这块和虚拟主机有密切关系
server {
# 端口号
listen 80;
#服务器名称
server_name localhost;
#access_log /var/log/nginx/host.access.log main;
# 默认情况下/代表的路径
location / {
# 默认转发的路径
root /usr/share/nginx/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 /usr/share/nginx/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;
#}
}
所以只要在/usr/share/nginx/html路径下的文件都可以访问打开。
通过文件上传工具,如SecureFX上传测试项目到/usr/share/nginx/web目录下
修改文件/etc/nginx/conf.d/default.conf的location
然后重启nginx浏览器访问
可以打开/var/log/nginx/access.log查看日志
实现效果
准备工作
具体配置
实现效果
使用nginx反向代理,根据访问的路径跳转到不同端口的服务器中
nginx监听端口为9001
访问 http://127.0.0.1:9001/edu/ 直接跳转到127.0.0.1:8080
访问 http://127.0.0.1:9001/vod/ 直接跳转到127.0.0.1:8081
准备工作
具体配置
什么是动静分离
通过 location 指定不同的后缀名实现不同的请求转发。通过 expires 参数设置,可以使浏览器缓存过期时间,减少与服务器之前的请求和流量。具体 Expires 定义:是给一个资源设定一个过期时间,也就是说无需去服务端验证,直接通过浏览器自身确认是否过期即可,所以不会产生额外的流量。此种方法非常适合不经常变动的资源。(如果经常更新的文件,不建议使用 Expires 来缓存),我这里设置 3d,表示在这 3 天之内访问这个 URL,发送一个请求,比对服务器该文件最后更新时间没有变化,则不会从服务器抓取,返回状态码 304,如果有修改,则直接从服务器重新下载,返回状态码 200。
准备工作
具体配置
# 全局配置
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.1.129
smtp_connect_timeout 30
router_id LVS_DEVEL # 访问到主机
}
# 脚本配置
vrrp_script chk_http_port {
script "/usr/local/src/nginx_check.sh"
interval 2 #检测脚本执行的间隔
weight 2 # 权重
}
# 虚拟IP配置
vrrp_instance VI_1 {
state BACKUP # 备份服务器将 MASTER 改为 BACKUP
interface ens33 # 网卡
virtual_router_id 51 # 主、备机的 virtual_router_id 必须相同
priority 90 # 主、 备机取不同的优先级,主机值较大,备机值较小
advert_int 1 # 每隔多长时间发送心跳,检测服务器是否活着
authentication { # 权限校验方式 PASS为密码
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.120 # VRRP H虚拟地址
}
}
#!/bin/bash
A=`ps -C nginx -no-header |wc -l`
if [ $A -eq 0 ]; then
/usr/sbin/nginx
sleep 2
if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
killall keepalived
fi
fi
一个 master 和多个 worker 的好处
设置多少个worker合适
worker 数和服务器的 cpu 数相等是最为事宜的。
连接数 worker_connection