nginx

nginx 简介

Nginx 是一个高性能的http和反向代理服务器,特点是占有内存少,并发能力强,事实上ngixn的并发能力,确实是同类型的网页服务器中表现最好的,中国大陆使用nginx网站用户有:百度,京东,新浪,网易,腾讯,淘宝等

nginx 作为web服务器

Nginx可以作为静态页面的web服务器,同时还支持CGI的动态语言,比如perl,php等,但是不支持java,java程序只能通过tomcat配合完成,Ngix转为性能优化而开发的,性能是其最重要的考量,实现上非常注重效率,能经受高负载的考验,有报告表明能支持高达50000个并发连接

正向代理

需要在客户端配置代理服务器进行指定网站访问
 

反向代理

暴露的是代理服务器地址,隐藏了真实服务器 IP 地址。

负载均衡

单个服务器解决不了,我们增加服务器的数量,然后将请求分发到各个服务器上,将原先的请求单个服务器上的情况,改为将请求发到多个服务器上,将负载均衡分发到不同的服务器,也就是我们所说的负载均衡

动静分离

为了加快网站的解析速度,可以把动态页面和静态页面,由不同的服务器来解析,加快解析速度,降低原来单个服务器的压力。

Linux防火墙

1.firewalld的基本使用
启动: systemctl start firewalld
查状态:systemctl status firewalld
停止: systemctl disable firewalld
禁用: systemctl stop firewalld
在开机时启用一个服务:systemctl enable firewalld.service
在开机时禁用一个服务:systemctl disable firewalld.service
查看服务是否开机启动:systemctl is-enabled firewalld.service
查看已启动的服务列表:systemctl list-unit-files|grep enabled
查看启动失败的服务列表:systemctl --failed
firewall-cmd --list-all
2.配置firewalld-cmd
查看版本: firewall-cmd --version
查看帮助: firewall-cmd --help
显示状态: firewall-cmd --state
查看所有打开的端口: firewall-cmd --zone=public --list-ports
更新防火墙规则: firewall-cmd --reload
查看区域信息: firewall-cmd --get-active-zones
查看指定接口所属区域: firewall-cmd --get-zone-of-interface=eth0
拒绝所有包:firewall-cmd --panic-on
取消拒绝状态: firewall-cmd --panic-off
查看是否拒绝: firewall-cmd --query-panic
3.那怎么开启一个端口呢
添加 firewall-cmd --zone=public --add-port=9000/tcp --permanent
firewall-cmd --zone=public --add-service=http --permanent
firewall-cmd --reload # 重新载入,更新防火墙规则
firewall-cmd --zone= public --query-port=80/tcp #查看
firewall-cmd --zone= public --remove-port=80/tcp --permanent # 删除

nginx常用命令

首先进入nginx的目录
1.查看版本号
./nginx -v
2.开启nginx
./nginx
3.关闭nginx
./nginx -s stop
4.重新加载nginx
./nginx -s reload

nginx反向代理

在nginx中的,nginx.config 配置文件中配置
server {
listen 80; #监听端口号
server_name localhost; #访问的地址
 
#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;
}

nginx负载均衡

再配置文件中配置
#添加服务器地址
upstream myserver{
server 106.15.73.222:8080;
server 106.15.73.222:8081;
}
 
server {
listen 80;
server_name localhost;
 
#charset koi8-r;
 
#access_log logs/host.access.log main;
 
location / {
root html;
#转发地址
proxy_pass http://myserver;
index index.html index.htm;
}
分配的方式
1 、轮询(默认)
每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器 down 掉,能自动剔除。
 
2 weight
weight 代表权,重默认为 1,权重越高被分配的客户端越多
指定轮询几率,weight 和访问比率成正比,用于后端服务器性能不均的情况。
 
upstream server_pool {
server 192.168.5.21 weight = 10 ;
server 192.168.5.22 weight = 10 ;
}
 
3 、ip_hash
每个请求按访问 ip 的 hash 结果分配,这样每个访客固定访问一个后端服务器,可以解决 session 的问题。
 
upstream server_pool {
ip_hash ;
server 192.168.5.21:80 ;
server 192.168.5.22:80 ;
}
 
4 、fair (第三方)
按后端服务器的响应时间来分配请求,响应时间短的优先分配。
 
upstream server_pool {
server 192.168.5.21:80 ;
server 192.168.5.22:80 ;
fair ;
}

nginx动静分离

再配置文件中修改
server {
listen 80;
server_name localhost;
 
#charset koi8-r;
 
#access_log logs/host.access.log main;
 
location /html/ {
root /data/;
index index.html index.htm;
}
 
location /image/ {
root /data/;
autoindex on;
}
再相关文件目录中,创建文件夹和相关静态资源
 
 
posted @ 2020-03-19 10:45  黑大帅。  阅读(78)  评论(0)    收藏  举报