nginx
-
nginx 基本概念
nginx 是一个高性能的http和反向代理服务器,特点是占用内存少,并发稿。
-
nginx作用
正向代理:在客户端配置代理服务配置,然后访问网络
反向代理:客户端–> 服务器–> (0~n)个服务器
负载均衡:将单个服务器请求分发到多个服务器上,并由多个服务器对请求进行平摊处理
动静分离:为了加快网站的解析速度,可以把动态页面和静态页面由不同的服务器来解析,加快解析速度,降低原来单个服务器的压力。
nginx安装
yum -y install gcc pcre-devel zlib-devel openssl openssl-devel安装依赖
然后用安装包安装
make未找到命令:
yum -y install gcc automake autoconf libtool make
nginx常用命令
使用nginx操作命令前提条件:必须进入nginx的目录
/usr/local/nginx/sbin
1. 查看nginx版本号 ./nginx -v
2. 启动nginx ./nginx
3. 关闭nginx ./nginx -s stop
4. 重新加载nginx ./nginx -s reload
nginx 配置文件
usr/local/nginx/conf/nginx.conf
nginx配置文件有三部分:
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
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;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
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;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
第一部分:全局快
worker_processes 1; nginx服务器并发处理服务器关键配置,值越大可支持的并发量越大
第二部分 events块:
events块涉及的指令主要影响nginx服务器与用户的网络连接
worker_connections 1024;
第三部分:http块
nginx 服务器配置最频繁的部分,
包括http全局块和server块
http块有引入设置
server块又分为:
servler块
location块
实例
nginx 反向代理
-
实现效果:
打开浏览器,在浏览器输入www.123.com,跳转到linxu系统的tomcat主界面
-
具体实现:
安装jkd tomcat
启动tomcat 在bin文件夹下
./startup.sh查看日志,在logs下运行
tail -f catalina.outtomcat默认端口8080
1.查看开放的端口号
firewall-cmd --list-all
设置开放的端口号
firewall-cmd --add-service=http–permanent
firewall-cmd --add-port=8080/tcp --permanent
重启防火墙
firewall-cmd --reload
- 访问过程:
windows浏览器 —> nginx -->tomcat
tomcat–>nginx --> w浏览器
server { listen 80; server_name 192.168.85.132; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; proxy_pass http://127.0.0.1:8080 index index.html index.htm; }首先,当我们访问192.168.85.132; 80端口时,根据配置:
proxy_pass http://127.0.0.1:8080 会帮我们转发到本地8080端口
-
实例2
使用nginx反向代理,根据访问的路径跳转到不同的端口服务
nginx监听端口为9001
访问 http://192.168.85.132:9001/edu/ 跳转到127.0.0.1:8080
访问 http://192.168.85.132:9001/vod/ 跳转到127.0.0.1:8081
准备两个tomcat服务器
启动tomcat服务
./startup.sh
关闭
./shutdown.sh
ps -ef | grep tomcat
查询服务端口号
kill -9 进程id 杀死进程
在nginx配置文件中配置反向代理:
server { listen 9001 ; # listen somename:8080; server_name 192.168.85.132; location ~/edu/ { proxy_pass http://127.0.0.1:8080; # index index.html index.htm; } location ~/vod/{ proxy_pass http://127.0.0.1:8081; } }nginx 负载均衡:
实现效果:
浏览器地址输入:http://192.168.85.132/edu/a.html,负载均衡效果
http{ upstream myserver { server 192.168.85.132:8080; server 192.168.85.132:8081; } server { listen 80; server_name 192.168.85.132; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; #proxy_pass http://127.0.0.1:8080; proxy_pass http://myserver; index index.html index.htm; } } # #server { #listen 80; #server_name 192.168.85.132; # ##charset koi8-r; # ##access_log logs/host.access.log main; # #location / { #root html; #proxy_pass http://127.0.0.1:8080; #index index.html index.htm; #}upstream myserver {
server 192.168.85.132:8080;
server 192.168.85.132:8081;
}添加服务器列表;
proxy_pass http://myserver;
添加服务器代理
负载均衡分配策略:
轮询(默认) 每个请求按照时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,则自动剔除。
weight: weight代表权重,默认为1,权重越高被分配的客户端越多。
upstream myserver {
server 192.168.85.132:8080 weight=5;
server 192.168.85.132:8081 weight=10;
}则8081 比8080获得的客户端请求多。
ip_hash:
upstream myserver {
ip_hash;
server 192.168.85.132:8080 weight=5;
server 192.168.85.132:8081 weight=10;
}每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题;
fair(第三方):
按后端服务器的响应时间来分配请求,响应时间短的优先分配;
upstream myserver {
server 192.168.85.132:8080 weight=5;
server 192.168.85.132:8081 weight=10; fair;
}
nginx 动静分离
将动态servlet /jsp 与 静态css,html,jpg等进行分开存放在不同的服务器上,使用nginx处理静态页面,使用tomcat处理动态页面。
可以分为两种,一种是将静态文件放在单独的服务器上。第二种是放在一起,但是使用nginx进行分开处理。
通过nginx访问静态资源
在nginx配置文件中进行配置:
server {
listen 80;
server_name 192.168.85.132;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
#proxy_pass http://127.0.0.1:8080;
proxy_pass http://myserver;
index index.html index.htm;
}
location /www/ {
root /data/;
}
location /image/{
root /data/;
autoindex on;
}
location /www/ {
root /data/;
}
location /image/{
root /data/;
autoindex on; # 表示列出当前文件夹中的内容
}
通过以上配置,就可以通过nginx服务器进行静态资源访问;
http://192.168.85.132/www/index.html
nginx 高可用集群
如果nginx挂了,请求依然可以发送,返回。
高可用配置:
配置两台nginx服务器,主服务器,备份服务器。
当主服务器挂了,则切换至备份服务器;
切换需要用到keepalived软件;相当于路由。用来检测当前服务器是否还活着。
虚拟IP:
准备工作:
两台nginx服务器
并安装nginx与keepalived;
yum install keepalived -y
安装后再etc目录下keepalived/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.85.132
smtp_connect_timeout 30
router_id LVS_DEVEL # 定义个什么
vrrp_skip_check_adv_addr
#vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_script chk_http_port{
script "/usr/local/src/nginx_check.sh"
interval 2 # 检测脚本执行间隔
weight 2
}
vrrp_instance VI_1 {
#state MASTER
state BACKUP # 备份服务器上将MASTER改为BACKUP
interface ens33 //网卡
virtual_router_id 51 # 主、备机的virtual_rooter_id 必须相同
priority 100 # 主、备机取不同的优先级,主机值较大,备份机较小
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.200.16 // VRRP H虚拟地址
192.168.200.17
192.168.200.18
}
}
-
修改/etc/keepalived.conf配置文件
-
添加检测脚本
#!/bin/bash A=`ps -C nginx -no-header |wc -1` if [ $A -eq 0 ];then /usr/local/nginx/sbin/nginx sleep 2 if [ `ps -C nginx --no-header |wc -1` -eq 0 ];then killall keepalived fi fisystemctl start keepalived.service 启动keepalived服务
ps -ef | grep keepalived 查看keepalived进程状态
所以是将两个服务器绑定在一个虚拟IP上,然后访问这个虚拟ip,再去nginx服务器进行访问。
使用ip a命令查看网卡
inet 192.168.85.140/32 scope global ens33
valid_lft forever preferred_lft forever
出现这行代表 虚拟ip已经绑定
在/etc/hosts/中 添加本机ip地址映射
127.0.0.1 LVS_DEVEL
遇到的错误:
vip不能ping通,数据发送不过去,一直在加载,原因是开启了vrrp_strict
vrrp_strict:严格遵守VRRP协议。下列情况将会阻止启动Keepalived:1. 没有VIP地址。2. 单播邻居。3. 在VRRP版本2中有IPv6地址。这里需要注释掉
nginx原理
-
mastre进程 和worker进程
master是领导worker进程的管理员
-
client -->Master–>worker
-->worker -->tomcat
…
-
一个Master和多个worker
-
设置多少个worker合适?
worker数和服务器的cpu数相等最为合适。
设置worker数量:
worker_processes 4
wokrer绑定cpu 4worker 绑定4 cpu
worker_cpu_affinity 0001 0010 0100 1000
worker_connection:这个值表示每个worker进程所能建立连接的最大值。
发送一个请求,占用了worker几个连接数?
要么是2个要么是4个
副服务器一直可以访问,主服务器访问不到????、
-
2万+

被折叠的 条评论
为什么被折叠?



