Nginx 学习

Nginx

主要功能:

  • Web 服务器:Nginx 可以作为Web 服务器,接收客户端的 HTTP 请求,根据配置文件中的规则返回相应的网页内容
  • 反向代理:Nginx 可以作为反向代理服务器,位于后端服务器(如应用服务器、数据库服务器等)之前,接收来自客户端的请求,然后将请求转发给后端服务器处理。
  • 负载均衡:Nginx 具备强大的负载均衡功能,能够将客户端的请求均匀地分配到多个后端服务器上,避免单个服务器负载过高。
  • HTTP 缓存:Nginx 可以对 HTTP 请求和响应进行缓存,减少后端服务器的压力,提高网站的响应速度。

一 .安装

1. YUM 安装

yum 源:

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
$ yum -y install nginx
$ systemctl enable nginx
$ systemctl start nginx
$ firewall-cmd --permanent --zone=public --add-port=80/tcp
$ firewall-cmd --reload

2. docker 安装

docker run --name mynginx -p 80:80 -d nginx

3. Ansible 安装

  • 安装 Ansible NGINX collection
    ansible-galaxy collection install nginxinc.nginx_core
  • playbook:
---
- hosts: all
  collections:
    - nginxinc.nginx_core
  tasks:
    - name: Install NGINX
      include_role:
        name: nginx
    - name: Configure NGINX
      ansible.builtin.include_role:
        name: nginx_config
      vars:
        nginx_config_http_template_enable: true
        nginx_config_http_template:
          - template_file: http/default.conf.j2
            deployment_location: /etc/nginx/conf.d/default.conf
            config:
              servers:
                - core:
                    listen:
                      - port: 80
                    server_name: localhost
                  log:
                    access:
                      - path: /var/log/nginx/access.log
                        format: main
                  sub_filter:
                    sub_filters:
                      - string: server_hostname
                        replacement: $hostname
                    once: false
                  locations:
                    - location: /
                      core:
                        root: /usr/share/nginx/html
                        index: index.html

 nginx_config_html_demo_template_enable: true
 nginx_config_html_demo_template:
   - template_file: www/index.html.j2
     deployment_location: /usr/share/nginx/html/index.html
     web_server_name: Ansible NGINX collection

  • nginx.conf:
user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/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  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

l

二 . 负载均衡

Nginx 负载均衡通过 upstream 模块来定义一组后端服务器,并使用 proxy_pass 指令将请求转发到这些服务器

http {
    upstream backend_servers {
        server 192.168.1.100:80;
        server 192.168.1.101:80;
    }

    server {
        listen 80;
        server_name your_domain.com;

        location / {
            proxy_pass http://backend_servers;
        }
    }
}

负载均衡算法

  • 轮询(默认):每个请求按顺序逐一分配到不同的后端服务器。
  • 加权轮询:根据服务器的性能或权重,按比例分配请求。权重越高,分配到的请求越多。
upstream backend_servers {
    server 192.168.1.100:80 weight=3;
    server 192.168.1.101:80 weight=1;
}
  • IP 哈希:根据客户端的 IP 地址计算哈希值,将相同 IP 地址的请求始终转发到同一台后端服务器。适用于需要保持会话粘性的场景。
upstream backend_servers {
    ip_hash;
    server 192.168.1.100:80;
    server 192.168.1.101:80;
}

三 . 内容缓存

缓存能够存储响应结果,以供未来再次使用,进而加速内容的提供。通过从缓存中提供内容,NGINX 可卸载上游(upstream)服务器的高开销、重复性工作,从而减少其负载。
缓存可以提高性能并减少负载,这意味着您可以用更少的资源更快地提供服务。缓存还能减少提供资源所需的时间和带宽。

     http {
         proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m use_temp_path=off;
 
         server {
             listen 80;
             server_name localhost;
 
             location /static/ {
                 proxy_cache my_cache;
                 proxy_cache_valid 200 302 60m;
                 proxy_cache_valid 404 10m;
                 proxy_pass http://backend_servers;
             }
         }
     }

在这个配置中,proxy_cache_path指令定义了缓存的存储路径、层级结构、缓存区名称和大小等参数。location块中的proxy_cache指令指定使用的缓存区名称(这里是my_cache),proxy_cache_valid指令设置了不同 HTTP 状态码的缓存有效期。对于状态码为 200 和 302 的响应,缓存有效期为 60 分钟;对于状态码为 404 的响应,缓存有效期为 10 分钟。proxy_pass指令将请求转发到后端服务器。

四. 安全配置

访问配置

Nginx 可以通过配置来限制对特定资源的访问。可以基于 IP 地址、用户身份验证等方式进行访问控制。例如,只允许特定 IP 地址段的客户端访问服务器:

     server {
         listen 80;
         server_name localhost;
 
         location /admin/ {
             allow 192.168.1.0/24;
             deny all;
             proxy_pass http://backend_servers;
         }
     }

location /admin/块中,allow指令允许192.168.1.0/24网段的 IP 地址访问,deny all拒绝其他所有 IP 地址的访问。然后将请求转发到后端服务器。

https配置

listen 443 ssl指定服务器监听 443 端口并启用 SSL。ssl_certificate和ssl_certificate_key指令分别指定了 SSL 证书和私钥的路径。

     server {
         listen 443 ssl;
         server_name localhost;
 
         ssl_certificate /etc/nginx/ssl/certificate.crt;
         ssl_certificate_key /etc/nginx/ssl/private.key;
 
         location / {
             root /usr/share/nginx/html;
             index index.html index.htm;
         }
     }

五. API网关

 http {  
    # 定义一个或多个upstream块,用于配置后端服务器组  
    upstream backend_servers {  
        server backend1.example.com;  
        server backend2.example.com;  
    }  
  
    server {  
        listen 80; # 监听80端口  
        server_name api.example.com; # 设置你的API网关域名  
  
        # 配置SSL(如果需要HTTPS)  
        # ssl_certificate /path/to/cert.pem;  
        # ssl_certificate_key /path/to/cert.key;  
  
        # 定义不同的location块,用于匹配不同的API路径  
        location /api/v1/video/ {  
            proxy_pass http://video_service; # 假设你有一个视频服务的upstream  
            proxy_http_version 1.1;  
            proxy_set_header Host $host;  
            proxy_set_header X-Real-IP $remote_addr;  
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  
        }  
  
        location /api/v1/homework/ {  
            proxy_pass http://homework_service; # 假设你有一个作业服务的upstream  
            # 其他代理设置...  
        }  
  
        # 可以继续添加其他location块以匹配更多API路径  
  
        # 其他server块配置...  
    }  
  
    # 其他http块配置...  
  
    # 定义其他upstream块,如video_service、homework_service等  
    upstream video_service {  
        server video1.example.com:8080;  
        server video2.example.com:8080;  
    }  
  
    upstream homework_service {  
        server homework1.example.com:8080;  
        server homework2.example.com:8080;  
    }  
  
    # ... 其他upstream定义  
}

参考: Nginx配置最全详解(万字图文总结)

posted @ 2024-12-26 15:32  渔樵江渚  阅读(33)  评论(0)    收藏  举报