前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >后端开发人员必备的一份NGINX学习清单

后端开发人员必备的一份NGINX学习清单

原创
作者头像
Mandy的名字被占用了
发布2024-06-29 01:41:04
730
发布2024-06-29 01:41:04
举报

大家好,我是小麦。今天分享一篇关于NGINX常用的配置清单。

作为一名服务端研发工程师,接触服务器的时间也比较多。在项目对外提供服务,我们一般会使用NGINX来提供对外的服务,因此NGINX的操作也非常多,这就需要我们对NGINX比较熟悉,因此特意整理了一份相对完善的清单。

首先还是对NGINX做一个简单的介绍,分别从是什么?有什么优势?以及使用的业务场景来聊聊NGINX。

什么是NGINX?

NGINX是一个高性能的HTTP和反向代理服务器,同时也是一个IMAP/POP3代理服务器。它最初由Igor Sysoev开发,旨在解决C10K问题(即在单台服务器上同时处理1万个客户端连接)。如今,NGINX已经发展成为广泛使用的Web服务器、负载均衡器和反向代理服务器。

NGINX的优势

  1. 高性能:NGINX以事件驱动架构为基础,能够高效地处理大量并发连接,适用于高流量网站。
  2. 低资源消耗: NGINX使用较少的内存和CPU资源,能够高效地管理资源,适合在资源有限的环境中运行。
  3. 稳定性:NGINX在长时间高负载下表现出色,能够保持稳定运行,减少宕机和性能下降的风险。
  4. 灵活性和可扩展性:通过模块化架构,NGINX可以根据需要进行扩展和定制,以满足特定应用场景的需求。
  5. 反向代理和负载均衡:NGINX内置强大的反向代理和负载均衡功能,可以分发流量到多个后端服务器,提高应用的可用性和性能。
  6. 支持多种协议: 除了HTTP/HTTPS,NGINX还支持IMAP、POP3等邮件协议,以及HTTP/2、WebSocket等现代协议。
  7. 静态文件处理: NGINX在处理静态文件(如HTML、CSS、JavaScript和图像)方面非常高效,适合用于静态内容服务。

常用的业务场景

1、Web服务器

作为HTTP和HTTPS服务器,提供静态和动态内容服务。用于托管网站和Web应用程序。

2、反向代理

代理客户端请求,将请求转发到一个或多个后端服务器。用于隐藏后端服务器的真实地址,提供负载均衡和缓存功能。

3、负载均衡器:将流量分发到多个后端服务器,以提高应用程序的可用性和响应速度。支持多种负载均衡策略(如轮询、IP哈希、最少连接等)。

4、内容缓存:缓存后端服务器的响应,减少后端服务器的负载,提高响应速度。适用于静态内容和动态内容的缓存。

5、 SSL/TLS终端:处理SSL/TLS加密和解密,保护数据传输的安全性。用于HTTPS网站和Web应用程序的安全通信。

6、API网关:管理和保护API请求,提供身份验证、速率限制、日志记录和监控等功能。适用于微服务架构和分布式系统。

7、 邮件代理服务器: 代理IMAP和POP3邮件请求,提高邮件服务的性能和安全性。

示例配置

1. 基本Web服务器配置
代码语言:shell
复制
server {
    listen 80;
    server_name example.com;

    location / {
        root /var/www/html;
        index index.html index.htm;
    }
}
2. 反向代理配置
代码语言:shell
复制
server {
    listen 80;
    server_name api.example.com;

    location / {
        proxy_pass http://backend_server;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}
3. 负载均衡配置
代码语言:shell
复制
http {
    upstream backend {
        server backend1.example.com;
        server backend2.example.com;
    }

    server {
        listen 80;
        server_name example.com;

        location / {
            proxy_pass http://backend;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }
}
4. 静态内容缓存配置
代码语言:shell
复制
server {
    listen 80;
    server_name cache.example.com;

    location / {
        proxy_pass http://backend_server;
        proxy_cache my_cache;
        proxy_cache_valid 200 302 10m;
        proxy_cache_valid 404 1m;
    }
}

proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=my_cache:10m inactive=60m;

上述内容便是我对NGINX的大致总结,下面就来分享一下具体的只知识内容。

基本配置

  1. user: 指定运行 NGINX 服务的用户和用户组。user www-data;
  2. worker_processes: 设置工作进程的数量,通常设置为CPU核心数。worker_processes auto;error_log /var/log/nginx/error.log warn;pid /var/run/nginx.pid;性能优化events { worker_connections 1024; }keepalive_timeout 65;sendfile on;tcp_nopush on;tcp_nodelay on;HTTP 配置http { include /etc/nginx/mime.types; include /etc/nginx/conf.d/*.conf; }
  3. error_log: 指定错误日志文件的位置和日志级别。
  4. pid: 指定存储 NGINX 进程 ID 的文件。
  5. worker_connections: 设置每个工作进程的最大连接数。
  6. keepalive_timeout: 设置服务器与客户端连接的保持时间。
  7. sendfile: 启用高效的文件传输。
  8. tcp_nopush: 提高网络包的传输效率。
  9. tcp_nodelay: 提高小包数据的传输效率。
  10. include: 包含其他配置文件。
  11. default_type: 设置默认的 MIME 类型。default_type application/octet-stream;
  12. log_format: 定义日志格式。log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"';
  13. access_log: 指定访问日志文件的位置和格式。access_log /var/log/nginx/access.log main;
  14. send_timeout: 设置发送响应的超时时间。send_timeout 60s;

反向代理配置

  1. proxy_pass: 设置反向代理的地址。location / { proxy_pass http://localhost:8080; }
  2. proxy_set_header: 设置传递给后端服务器的请求头。proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme;
  3. proxy_connect_timeout: 设置连接后端服务器的超时时间。proxy_connect_timeout 60s;
  4. proxy_send_timeout: 设置向后端服务器发送请求的超时时间。proxy_send_timeout 60s;
  5. proxy_read_timeout: proxy_read_timeout 60s;
    • 设置读取后端服务器响应的超时时间。

SSL/TLS 配置

  1. ssl_certificate: 指定SSL证书文件。ssl_certificate /etc/nginx/ssl/nginx.crt;
  2. ssl_certificate_key: 指定SSL证书密钥文件。ssl_certificate_key /etc/nginx/ssl/nginx.key;
  3. ssl_protocols: 指定支持的SSL/TLS协议。ssl_protocols TLSv1.2 TLSv1.3;
  4. ssl_ciphers: 指定支持的加密算法。ssl_ciphers HIGH:!aNULL:!MD5;
  5. ssl_prefer_server_ciphers: 启用服务器首选加密算法。ssl_prefer_server_ciphers on;

虚拟主机配置

  1. server: 定义一个虚拟主机配置。server { listen 80; server_name example.com; location / { root /var/www/html; index index.html index.htm; } }
  2. listen: 指定服务器监听的端口和协议。listen 443 ssl;server_name example.com;root /var/www/html;index index.html index.htm;安全配置1、limit_req_zone: 设置速率限制区域。http { limit_req_zone $binary_remote_addr zone=mylimit:10m rate=1r/s; }2、 limit_req: 应用速率限制规则。server { location /api/ { limit_req zone=mylimit burst=5 nodelay; } }
  3. server_name: 指定虚拟主机的域名。
  4. root: 设置网站的根目录。
  5. index: 设置默认的主页文件。
  6. deny: 拒绝特定IP地址的访问。location / { deny 192.168.1.1; allow all; }
  7. allow: 允许特定IP地址的访问。location / { allow 192.168.1.0/24; deny all; }
  8. auth_basic: 启用基本HTTP认证。location /secure/ { auth_basic "Restricted Area"; auth_basic_user_file /etc/nginx/.htpasswd; }

其他有用的配置

  1. rewrite: 配置URL重写规则。location /old-path/ { rewrite ^/old-path/(.*)$ /new-path/$1 permanent; }
  2. return: 配置返回特定的HTTP状态码和页面。location / { return 404; }
  3. try_files: 尝试按照顺序找到文件。location / { try_files $uri $uri/ =404; }
  4. gzip: 启用Gzip压缩。gzip on; gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
  5. expires: 设置缓存控制头。location / { expires 30d; }

完整的 NGINX 配置示例

代码语言:shell
复制
user www-data;
worker_processes auto;
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;
    error_log /var/log/nginx/error.log warn;

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;

    server {
        listen 80;
        server_name example.com;
        root /var/www/html;
        index index.html index.htm;

        location / {
            try_files $uri $uri/ =404;
        }

        location /api/ {
            limit_req zone=mylimit burst=5 nodelay;
            proxy_pass http://localhost:8080;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 什么是NGINX?
  • NGINX的优势
  • 常用的业务场景
  • 示例配置
    • 1. 基本Web服务器配置
      • 2. 反向代理配置
        • 3. 负载均衡配置
          • 4. 静态内容缓存配置
          • 基本配置
          • 反向代理配置
          • SSL/TLS 配置
          • 虚拟主机配置
          • 其他有用的配置
          • 完整的 NGINX 配置示例
          相关产品与服务
          负载均衡
          负载均衡(Cloud Load Balancer,CLB)提供安全快捷的流量分发服务,访问流量经由 CLB 可以自动分配到云中的多台后端服务器上,扩展系统的服务能力并消除单点故障。负载均衡支持亿级连接和千万级并发,可轻松应对大流量访问,满足业务需求。
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档