Nginx的编译安装
#!/bin/bash
#解决软件的依赖关系,需要安装的软件包
yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel gcc gcc-c++ autoconf automake make
#useradd ouzhe
id ouzhe|| useradd ouzhe
#download nginx
mkdir -p /nginx
cd /nginx
curl -O http://nginx.org/download/nginx-1.19.6.tar.gz
#解压 下载的nginx的源码包
tar xf nginx-1.19.6.tar.gz
cd nginx-1.19.6
#生成编译前配置工作-->Makefile
./configure --prefix=/usr/local/nginx1 --user=ouzhe --group=ouzhe
--with-threads --with-http_ssl_module --with-http_realip_module
--with-http_v2_module --with-file-aio --with-http_stub_status_module --with-stream
#编译
make -j 2
#编译安装--》将编译好的二进制程序安装指定目录/usr/local/nginx1
make install
#永久修改
echo "PATH=$PATH:/usr/local/nginx1/sbin" >> /root/.bashrc
source /root/.bashrc
#关闭防火墙
systemctl stop firewalld
setenforce 0
#启动nginx
nginx
解释
–user=ouzhe指定启动nginx的进程的用户
–group=ouzhe指定启动的组
–with-threads 启用线程池的使用
–with-file-aio 支持 在FreeBSD和Linux上使用 异步文件I / O(AIO)。
–with-http_ssl_module 启用构建将HTTPS协议支持添加 到HTTP服务器的模块的功能。默认情况下未构建此模块。需要OpenSSL库来构建和运行此模块。
–with-http_realip_module 启用构建ngx_http_realip_module 模块的功能,该 模块将客户端地址更改为在指定的标头字段中发送的地址。默认情况下未构建此模块。
–with-http_v2_module
支持构建提供对HTTP / 2支持的模块 。默认情况下未构建此模块。
–with-http_stub_status_module
支持构建ngx_http_stub_status_module 模块,该 模块提供对基本状态信息的访问。默认情况下未构建此模块。
–with-stream
–with-stream=dynamic
支持构建 用于通用TCP / UDP代理和负载平衡的 流模块。默认情况下未构建此模块。 --》四层负载均衡的支持
Nginx基本命令
nginx -s signal
Where signal may be one of the following:
stop — fast shutdown
quit — graceful shutdown
reload — reloading the configuration file
reopen — reopening the log files
Nginx.conf 文件
[root@sc-nginx conf]# cat nginx.conf
#user nobody; 默认使用nobody用户去启动nginx
worker_processes 2; 工作进程的数量,建议工作进程的数量和cpu核心一致
#error_log logs/error.log; 错误日志
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid; nginx master的进程号pid
events {
worker_connections 1024; 并发数量,同时可以允许多少人同时访问nginx --》同时1024个人访问
}
#需要根据实际的cpu,内存,带宽,磁盘IO能力来进行压力测试
#1个cpu核心--》4G的内存
#worker_processes * worker_connections = 2 *1024 = 2048
http { --》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;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65; 65秒后nginx会主动断开连接
#gzip on; 启用压缩功能 --》加快传输的速度的
server { --》提供web服务的配置 --》虚拟主机--》网站
listen 80; 监听80端口
server_name localhost; 网站服务的域名
#charset koi8-r;
#access_log logs/host.access.log main; 访问日志的路径和格式
location / { --》提供某个路由的配置 --》/ 访问网站的根目录
root html; html是存放网页的根目录 --》/usr/local/nginx1/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;
}
}
}
一个server其实就是虚拟主机—》对应一个网站
server{
listen 80;
server_name www.a.com;
access_log logs/a.com.access.log main;
location / {
root html/a.com;
index index.html index.htm;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html/a.com;
}
}
server{
listen 80;
server_name www.b.com;
access_log logs/b.com.access.log main;
location / {
root html/b.com;
index index.html index.htm;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html/b.com;
}
}
Directives —>ngx_http_core_module --》功能实现
指令 模块
下载模块 autoindex on
server{
listen 80;
server_name www.b.com;
access_log logs/b.com.access.log main;
location / {
root html/b.com;
index index.html index.htm;
autoindex on; #开启下载
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html/b.com;
}
}
网页的重定向:rewrite
dns域名解析
www.360buy.com -->www.jd.com
www.jd.com --》113.220.184.3
rewrite ^/(.*) http://www.ouzhe.cn/$1 permanent;
/feng
http://www.b.com/feng -->http://www.ouzhe.cn/feng
http://www.b.com/lixinhai -->http://www.ouzhe.cn/lixinhai
server{
listen 80;
server_name www.b.com;
access_log logs/b.com.access.log main;
location / {
root html/b.com;
index index.html index.htm;
autoindex on;
**rewrite ^/(.*) http://www.ouzhe.cn/$1 permanent;**
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html/b.com;
}
server{
listen 80;
server_name www.d.com;
location / {
**proxy_pass http://www.qq.com;**
}
}
https服务器的搭建
0.去申请证书,并且下载下来
1.证书需要上传到自己的linux服务器里
[root@sc-nginx conf]# pwd
/usr/local/nginx1/conf -->存放在conf目录下
[root@nginx conf]# unzip 5151775_www.sanchuangedu.cn_nginx.zip 解压
Archive: 5151775_www.sanchuangedu.cn_nginx.zip
Aliyun Certificate Download
inflating: 5151775_www.sanchuangedu.cn.pem
inflating: 5151775_www.sanchuangedu.cn.key
[root@nginx conf]# ls
5151775_www.sanchuangedu.cn.key fastcgi.conf htpasswd mime.types.default scgi_params win-utf
5151775_www.sanchuangedu.cn_nginx.zip fastcgi.conf.default koi-utf nginx.conf scgi_params.default
5151775_www.sanchuangedu.cn.pem fastcgi_params koi-win nginx.conf.bak uwsgi_params
b.com.htpasswd fastcgi_params.default mime.types nginx.conf.default uwsgi_params.default
2.修改nginx.conf配置文件,使用证书
server{
listen 443 ssl;
server_name www.ouzhe.com;
access_log logs/ouzhe_ssl.access.log ;
ssl_certificate 5151775_www.sanchuangedu.cn.pem;
ssl_certificate_key 5151775_www.sanchuangedu.cn.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
root html/ouzhe.com;
index index.html index.htm;
autoindex on;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
[root@nginx conf]# ulimit -a
Nginx的负载均衡
主要使用 ngx_http_upstream_module 模块
upstream web_pools {
#ip_hash; #启用哈希 ,启用时weight和backup无用
server 192.168.174.145:80 weight=10; #权重
#server 192.168.174.146:80 weight=10;
server 192.168.174.146:80 weight=10 backup; #热备配置
}
server {
..........
location / {
root html;
index index.html index.htm;
#proxy_pass web_pools;
proxy_pass http://web_pools;
}
}
根据不同的url做转发:
location /login{
proxy_pass http://192.168.0.161;
}
location /music{
proxy_pass http://192.168.0.163;
}
location /download{
proxy_pass http://192.168.0.163;
}
使用代理时在后端server获取更多的信息
在代理server上添加
location / {
root html;
index index.html index.htm;
proxy_pass http://web_pools;
proxy_set_header Host $host; #http头部携带
proxy_set_header X-Forwarded-For $remote_addr; #头部携带IP
}
基于文件目录或扩展名做跳转
Nginx重试机制
proxy_next_upstream
nginx日志变量
https://www.jianshu.com/p/1dc559c2eef8?from=timeline&isappinstalled=0
高可用:HA 高可用性 (High Availability)
什么是keepalivel?
Keepalived是用C语言编写的路由软件。该项目的主要目标是为Linux系统和基于Linux的基础结构提供负载均衡和高可用性的简单而强大的功能。 负载平衡框架依赖于提供第4层负载平衡的著名且广泛使用的Linux虚拟服务器(IPVS)内核模块。Keepalived实现了一组检查器,以根据其运行状况动态,自适应地维护和管理负载平衡的服务器池。另一方面,VRRP可实现高可用性 协议。VRRP是路由器故障转移的基础砖。此外,Keepalived还实现了一组VRRP有限状态机的挂钩,从而提供了低级和高速协议交互。为了提供最快的网络故障检测,Keepalived实施BFD协议。VRRP状态转换可以考虑BFD提示来驱动快速状态转换。Keepalived框架可以独立使用,也可以一起使用以提供弹性基础架构。
[root@LB-1 keepalived]# vim keepalived.conf
vrrp_instance VI_1 { #vrrp实例名字VI_1 --》相当于启用一个vrrp进程去工作
state MASTER #指定服务器从当的角色为MASTER
interface ens33 #在ens33接口上监听vrrp报文
virtual_router_id 99 #虚拟路由组的id 为99 此值有个范围0~255
priority 110 #优先级
advert_int 1 #发宣告报文的间隔时间为1秒
authentication { #认证
auth_type PASS #密码认证
auth_pass 1111 #密码为1111
}
virtual_ipaddress { #定义vip
192.168.0.180
}
}
backup服务器上的配置
[root@LB-2 keepalived]# cat keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL_02
vrrp_skip_check_adv_addr
#vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 99
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.0.180
}
}