当前位置首页 > Nginx知识

nginx学习

阅读次数:280 次  来源:admin  发布时间:
nginx web通信服务软件 基于http协议通信,tcp协议连接

反向代理,并发

软件的启动与关闭:
systemctl start nginx 启动软件
systemctl restart nginx 重启软件
systemctl stop nginx 关闭软件

查看软件状态
systemctl status nginx

重新加载软件
systemctl reload nginx

关闭防火墙
systemctl stop firewalld
systemctl disable firewalld 开机也不启动防火墙

安装
yum -y install epel-release 安装扩展包源

yum -y install nginx

写配置文件
打开配置文件:vim /etc/nginx/nginx.conf
user 用户
worker_processes 工作进程,默认是auto
error_log 错误日志路径
pid pid文件路径

include nginx模块相关的配置文件

events {      代表事件
worker_connections 1024; 工作连接数目,代表一个进程中开启多少个线程
}

http {
#日志格式
log_format main  .......

access_log /var/log/nginx/access.log main  记录访问日志 格式按照上面日志格式来写
include 包含的配置文件
server {  服务******
listen 80 default_server; 监听的端口
root /user/share/nginx/html web服务的根目录;在该目录下建立index.html文件

location / {   # / 代表统一资源定位符 url地址  uri 代表统一文件标识符
写配置文件,没有文件,读外部全局的配置
root /var/www/html;  #参照上部写配置文件,注意结尾的分号
index index.html index.htm index.* 指定默认资源
}
error_log 
}
}


正反向代理,负载均衡

nginx 做的就是反向代理,把自己暴露给请求用户,然后交给身后的web服务器去处理请求。

正向代理:
知道自己想要访问的地址,访问明确!不管中间怎么处理都会走到这个明确的地址
反向代理:
不明确具体请求,只要要求告诉代理,代理去做处理,然后把信息返回。

有三种负载均衡的调用方法
1、轮询 从头到尾轮一遍 默认方式

在http中配置
http { #http配置的位置

# 配置信息
upstream myapp1 {  创建组,自定义组名
server srv1.example.com;  web服务器的IP地址
server srv2.example.com;
server srv3.example.com;
}

server {
listen 80;
# 配置轮询监听的location
location / {
proxy_pass http://myapp1; # 指定反向代理是自己的组名
}
}
}

通过访问代理IP就能间接的访问web服务器的页面

也可以对某台web服务器通过权重,进行加权操作。在server 定义的一个服务器后边加上一个 weight = 数字;以表示多大的权重。

upstream myapp1 {  创建组,自定义组名
server srv1.example.com;  web服务器的IP地址
server srv2.example.com wight=3; 加权访问
server srv3.example.com;
}

2、最小链接调度  哪台连接处理的少就交给谁处理

在http中配置,在upstream 配置访问组中加上一条属性 least_conn 即可。
upstream myapp1 {
least_conn;
server srv1.example.com;
server srv2.example.com;
server srv3.example.com;
}

server {
listen 80;
# 配置监听的location
location / {
proxy_pass http://myapp1; # 指定反向代理是自己的组名
}
}

3、ip-hash 基于ip地址进行判断,常用于http请求回话保持操作

在http中配置,在upstream 配置访问组中加上一条属性 ip-hash即可。
http { #http配置的位置

# 配置信息
upstream myapp1 {  创建组,自定义组名
ip-hash
server srv1.example.com;  web服务器的IP地址
server srv2.example.com;
server srv3.example.com;
}

server {
listen 80;
# 配置监听的location
location / {
proxy_pass http://myapp1; # 指定反向代理是自己的组名
}
}
}
上一篇:ubuntu软件安装
下一篇:(转发)DebianGNU/Linux11.1安装