参考资料

  1. 如何防止勒索病毒攻击?
  2. nginx配置
  3. Nginx Keepalived配置详细说明以及案例
  4. Nginx HTTP代理服务器详细说明以及案例
  5. 如何查看nginx主配置文件路径方式
  6. 如何设置nginx监控与响应?
  7. WSGI(Web Server Gateway Interface,Web 服务网关接口)详细说明以及案例
  8. nginx配置文件详解

Nginx基本配置文件结构示例:

  1. 主配置文件(nginx.conf)基本结构:

# 全局块
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# events块
events {
    worker_connections 1024;
    use epoll;
}

# http块
http {
    # http全局配置
    include /etc/nginx/mime.types;
    default_type application/octet-stream;
    access_log /var/log/nginx/access.log;
    sendfile on;
    keepalive_timeout 65;

    # server块(虚拟主机配置)
    server {
        listen 80;
        server_name example.com;
        root /var/www/html;

        location / {
            index index.html;
        }

        location /api {
            proxy_pass http://backend;
        }
    }

    # 可包含其他配置文件
    include /etc/nginx/conf.d/*.conf;
}
  1. 关键配置部分说明:

  • 全局块:配置影响nginx全局的指令

  • events块:配置影响nginx服务器与用户的网络连接

  • http块:可以嵌套多个server块,配置代理、缓存、日志等

  • server块:配置虚拟主机的相关参数

  • location块:配置请求的路由,处理特定URI

  1. 典型server配置示例:

server {
    listen 443 ssl;
    server_name www.example.com;
    
    ssl_certificate /etc/nginx/ssl/example.crt;
    ssl_certificate_key /etc/nginx/ssl/example.key;
    
    location /static/ {
        alias /var/www/static/;
        expires 30d;
    }
    
    location / {
        proxy_set_header Host $host;
        proxy_pass http://127.0.0.1:8000;
    }
}