美文网首页
Nginx 基础

Nginx 基础

作者: Tinyspot | 来源:发表于2022-10-03 16:54 被阅读0次

1. 概念

HTTP 服务器 --> 代理服务器 --> 应用服务器
HTTP 服务器主要用来访问静态资源
应用服务器处理业务逻辑,可以生成动态的内容

1.1 Apache vs Nginx

  • Apache 一个进程处理一个请求,阻塞式的
  • Nginx 一个进程处理多个请求,非阻塞式的
    • 轻量级,占用更少的内存
    • 静态处理性能比 Apache 高
    • 异步非阻塞
    • 高度模块化设计

2. Nginx 功能

  • 高性能的HTTP和反向代理服务器
  • 高并发、高性能
  • 扩展性好
  • 异步非阻塞的事件驱动模型

2.1 正向代理

  • 客户端需要配置目标服务器信息
  • 正向代理是对客户端的伪装,隐藏了客户端的IP、头部或者其他信息,服务器得到的是伪装过的客户端信息
  • 应用场景
    • 正向代理的主要场景是客户端
    • 通过正向代理的方式,客户端的HTTP请求可以转发到之前与客户端网络不通的其他不同的目标服务器

2.2 反向代理

  • 客户端不感知目标服务器,对客户端而言,访问代理服务器就是访问真实服务器
  • 反向代理(如Nginx)是对目标服务器的伪装,隐藏了目标服务器的IP、头部或者其他信息,客户端得到的是伪装过的目标服务器信息。
  • 运用场景
    • 反向代理的主要场景是服务端。服务提供方可以通过反向代理服务器轻松实现目标服务器的动态切换,实现多目标服务器的负载均衡等
  • 优势
    • 隐藏真实服务器
    • 便于横向扩充后端动态服务
    • 动静分离,提升系统健壮性

2.3 负载均衡

  • 将请求分发到多个服务器

2.4 动静分离

  • 把动态页面和静态页面由不同的服务器来解析,加快解析速度
  • 静态资源(jpg/css/js/html),动态资源(动态数据)

2.5 缓存加速

2.6 扩展

淘宝 Tengine

3. Nginx 安装

3.1 Windows 环境

  • 官网下载压缩包
  • 用命令行启动 nginx (start nginx),启动时会一闪而过是正常的
  • 指定配置文件启动 start nginx -c conf/nginx.conf
  • 查看任务管理器有 nginx.exe 或 tasklist /fi "imagename eq nginx.exe"
  • 浏览器访问 http://127.0.0.1:80,出现 Welcome to nginx!
  • 退出 nginx -s quit

3.2 Linux 环境

3.3 文件目录

  • conf 配置文件
  • logs 启动日志

3.4 Nginx 常用命令

  • nginx -h 帮助命令
  • nginx -s reload 重新加载配置文件
  • nginx -s reopen 重启 Nginx
  • nginx -s stop 强制停止 Nginx
  • nginx -s quit 退出 Nginx
  • nginx -t 检查配置文件是否有语法错误

4. nginx.conf

# user  nobody;
# group nobody;

# worker 子进程
# worker_processes  auto;
worker_processes  1;

events {
    worker_connections  1024; # 每个worker进程可处理的并发连接数
}

http {
    # log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    # access_log  logs/access.log  main;
    keepalive_timeout  65; # 长连接超时时间

    server {
        listen       80;            # 监听端口
        server_name  localhost;     # 监听地址
        location / {                # 请求 url 匹配
            root   html;           # 根目录
            index  index.html index.htm; # 默认页
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

4.1 组成模块

  • main 模块
  • events 模块
  • http 模块
directive-command.png
Syntax: events { ... }
Default:    —
Context:    main

4.2 配置文件

  • 默认端口 listen 80;
    • 查看 80 端口是否被占用 netstat -ano | findstr 0.0.0.0:80netstat -ano | findstr "80"
  • 每个指令必须以分号结束
  • # 表示注释
  • 单位简写
    • K, k 千字节, M, m 兆字节
    • ms, s, m, h, d, w, M 月, y 年

4.3 location路由规则

pattern desc 优先级(降序)
location = /uri 完全匹配
location ^~ /uri 前缀匹配,^~ 表示在正则之前
location ~ pattern 正则匹配,~ 表示区分大小写
location ~* pattern 正则匹配,~* 表示不大小写
location /uri 前缀匹配,但是在正则匹配之后
location / 通用匹配,匹配任何未匹配到其它location的请求

4.4 请求行修改指令

HTTP 请求

GET /nginx/query?page=1&count=7 HTTP/1.1
Host: localhost
Connection: keep-alive
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9

page=1&count=7
Syntax: proxy_method method;
Default:    —
Context:    http, server, location

Syntax: proxy_http_version 1.0 | 1.1;
Default:    
proxy_http_version 1.0;
Context:    http, server, location

Syntax: proxy_set_header field value;
Default:    
proxy_set_header Host $proxy_host;
proxy_set_header Connection close;
Context:    http, server, location

4.5 自定义config

指定 config 配置

start nginx -c conf/nginx-redirect.conf
nginx -s reload
http {
    server {
        listen       80;
        server_name  localhost;
        location / {
            proxy_pass http://localhost:8020/nginx/query;
            proxy_method POST;
            proxy_set_header Host $host;
            # 获取客户端的 ip 放入变量X-Real-IP
            proxy_set_header X-Real-IP $remote_addr;
            # 获取所有转发请求的 ip 信息列表
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }
}
@RestController
@RequestMapping("/nginx")
public class NginxController {
    @RequestMapping("/query")
    public String query(HttpServletRequest request) {
        System.out.println(request.getMethod());
        System.out.println(request.getRemoteAddr());
        // 获取自定义的变量
        System.out.println(request.getHeader("X-Real-IP"));
        System.out.println(request.getHeader("X-Forwarded-For"));
        return "success";
    }
}

5. Nginx 实现虚拟主机

  • 三类虚拟主机
    • 基于多 IP 的虚拟主机
    • 基于多端口的虚拟主机
    • 基于域名的虚拟主机

VPS

相关文章

网友评论

      本文标题:Nginx 基础

      本文链接:https://www.haomeiwen.com/subject/pfcdartx.html