NGINX服务器运维

前言

概括
Nginx是一款开源高并发高性能反向代理服务器,模块化架构使得它的扩展性非常好,能够实现正向代理、反向代理、负载均衡、缓存、动静分离等。通过keepalived来实现Nginx在高并发情况下的高可用(主备)。

正向代理、反向代理
正向代理隐藏真实客户端,反向代理隐藏真实服务端。

  • 正向代理:客户端手机通过vpn访问google浏览器。
  • 反向代理:打电话至10086总机,转至人工服务。

安装环境

Linux命令行、Docker安装NGINX1.19.9 查看

# 配置文件(主配置文件)
/etc/nginx/nginx.conf
# 子配置文件(主配置文件会默认将子配置文件内容全部引入)
/etc/nginx/conf.d/

# 日志文件
/var/log/nginx

# 静态文件
/usr/share/nginx/html/


# 开启启动
systemctl enable nginx
# 关闭开机启动
systemctl disable nginx
# 启动
systemctl start nginx
# 停止
systemctl stop nginx
# 重启
systemctl restart nginx
# 重新加载
systemctl reload nginx
# 运行状态
systemctl status nginx
# 杀死进程
ps-ef|grep nginx & kill -9 pid

配置详解

nginx.conf

# 全局配置
user  nginx; 								    							# 运行用户
worker_processes  1; 						    					# nginx进程数,一般设置与cpu核数一样
error_log   /var/log/nginx/error.log warn;  	# 错误日志存放
pid         /var/run/nginx.pid; 							# nginx服务启动时候pid存放

# 配置影响Nginx服务器与用户的网络连接
events {
		# use epoll; nginx轮询方式 select、poll、kqueue、epoll、/dev/poll、eventport、推荐不配置让nginx自动选择
    worker_connections  1024; 				# 子进程能够处理的最大并发连接数
    accept_mutex on; # 负载均衡互斥锁 默认off关闭
}

# 配置代理.缓存.日志定义等绝大多数功能和第三方模块的配置
http {
    # 日志
    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;   # 减少网络报文段的数量
    tcp_nodelay         on;
    keepalive_timeout   65;   # 保持连接的时间,也叫超时时间,单位秒
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;      # 文件扩展名与类型映射表
    default_type        application/octet-stream;   # 默认文件类型

    # include /etc/nginx/conf.d/*.conf;   # 加载子配置项

    server {
        listen       80;
      	# server_name www.snowl.socpe snowl.socpe; 域名匹配
        server_name  localhost;

        error_page 500 502 503 504 /50x.html;
        error_page 400 404 error.html;

        location / {
            root /usr/share/nginx/html; # 网站根目录
            index index.html index.htm; # 默认首页文件
            allow 183.129.134.242; # allow 允许访问的ip地址 可以all
            # deny 进制访问的ip地址 可以all
        }

        # rabbitmq控制界面
        location /rabbitmq/ {
           proxy_pass http://localhost:15672/;
        }

        # nacos控制界面
        location /nacos/ {
            proxy_pass http://localhost:8848;
        }
    }
}

路径匹配规则

location
  • = 精确匹配
  • ~ 正则匹配,区分大小写
  • ~* 正则匹配,不区分大小写
  • ^~ 匹配到即停止搜索

示例

  • ~ \.(jpeg|jpg|png|svg)$ 以…结尾,区分大小写
  • ^~ /api/ 以api为前缀请求,匹配到就停止搜索
  • = /match_all/ 精确匹配,等号也可以忽略

location中反斜杠区别

location /test {
	...
}

location /test/ {
	...
}
  • /test :当收到请求时,nginx首先会查找是否有test目录
    • 如果,则会去test目录中查找index.html文件
    • 如果没有,则会找是否有test文件
  • /test/:当收到请求时,nginx首先会查找是否有test目录
    • 如果,则会去test目录中查找index.html文件
    • 如果没有,也不会去找test文件,而是抛出404页面
proxy_pass
proxy_pass http://192.168.100.33:8081 
proxy_pass http://192.168.100.33:8081/
  • / 意味着nginx不会修改用户的url、而是直接透传给上游应用服务器
  • 不带/意味着nginx会修改用户的url、修改方法是将location后的url从用户的url中剔除,然后透传给上游应用服务器

**/**

location /bbs/{
  proxy_pass http://127.0.0.1:8080;
}
  • 用户请求: /bbs/api/test
  • 到达nginx服务器url: /bbs/api/test
  • 达到上游应用服务器url: **/bbs/api/test

不带**/**

location /bbs/{
  proxy_pass http://127.0.0.1:8080/;
}
  • 用户请求: /bbs/api/test
  • 到达nginx服务器url: /bbs/api/test
  • 达到上游应用服务器url: /api/test

反向代理

server {
  listen 80;
  server_name snowl.space;

  location / {
    	proxy_pass http://polling_strategy
  }
}
轮询策略
upstream polling_strategy {
  server snowl.space:8080; # 应用服务器1
  server snowl.space:8081; # 应用服务器2
}
权重策略
upstream polling_strategy {
  server snowl.space:8080  [option];  # 应用服务器1
  server snowl.space:8081  [option]; # 应用服务器2
}

- weight=number 			# 权重值
- max_conns=number 			# 上游服务器最大的并发连接数
- fail_timeout=time 		# 服务器不可用的判定时间
- max_fails=number 			# 服务器不可用的检查次数
- backup 					# 备份服务器,仅当其他服务器都不可用时才会启用
- down 					   # 标记服务器长期不可用,离线维护
hash算法

**hash $request_uri** 表示使用reqeust_uri变量作为hashkey值,只要访问的uri保持不变,就会一直分发给同一个服务器

upstream polling_strategy {
	hash $request_uri;
  server snowl.space:8080; # 应用服务器1
  server snowl.space:8081; # 应用服务器2
}
ip_hash策略

ip_hash 表示使用ip地址变量作为hashkey值,只要访问的ip不变,就会一直分发给同一个服务器

upstream polling_strategy {
	ip_hash;
  server snowl.space:8080; # 应用服务器1
  server snowl.space:8081; # 应用服务器2
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值