Nginx

Administrator
Administrator
发布于 2025-03-04 / 60 阅读

官网: https://nginx.org/

nginx.conf 原文件

# 定义运行 Nginx 的用户,nobody 表示以低权限用户运行,增加安全性
#user  nobody;

# 定义工作进程的数量,通常设置为 CPU 核心数
worker_processes  1;

# 错误日志配置,可以指定日志级别(notice, info, warn, error, crit, alert, emerg)
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

# 指定 Nginx 主进程的 PID 文件路径
#pid        logs/nginx.pid;

# 事件块,配置连接处理相关的参数
events {
    # 定义每个工作进程的最大连接数
    worker_connections  1024;
}

# HTTP 块,配置 HTTP 服务器相关参数
http {
    # 包含 MIME 类型定义文件
    include       mime.types;
    # 默认的 MIME 类型
    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  logs/access.log  main;

    # 开启高效文件传输模式
    sendfile        on;
    # 防止网络阻塞,仅在 sendfile 开启时有效
    #tcp_nopush     on;

    # 客户端连接保持活动的超时时间,0 表示禁用
    #keepalive_timeout  0;
    keepalive_timeout  65;

    # 开启 gzip 压缩
    #gzip  on;

    # 定义一个 HTTP 服务器
    server {
        # 监听端口
        listen       80;
        # 服务器名称,可以是域名或 IP 地址
        server_name  localhost;

        # 字符集设置
        #charset koi8-r;

        # 访问日志配置
        #access_log  logs/host.access.log  main;

        # 定义 location 块,处理根路径请求
        location / {
            # 根目录,通常为 HTML 文件存放路径
            root   html;
            # 默认首页文件
            index  index.html index.htm;
        }

        # 自定义错误页面
        #error_page  404              /404.html;

        # 定义 50x 错误页面
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # 将 PHP 脚本代理到 Apache 服务器
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # 将 PHP 脚本传递给 FastCGI 服务器
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # 禁止访问 .htaccess 文件
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

    # 另一个虚拟主机配置,可以基于 IP、名称和端口进行配置
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

    # HTTPS 服务器配置
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    # SSL 证书和密钥文件路径
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    # SSL 会话缓存和超时时间
    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    # SSL 加密算法配置
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
}