参考资料

  1. 如何查看nginx主配置文件路径方式
  2. PHP-FPM 性能优化
  3. Nginx镜像模块:ngx_http_mirror_modu详细说明以及案例le
  4. Nginx基于FastCGI实现负载均衡详细说明以及案例
  5. 是否有自动化的安全检测nginx工具?
  6. nginx的用法
  7. Nginx Logrotate:日志归档配置详细说明以及案例
  8. FastCGI(Fast Common Gateway Interface,快速通用网关接口)详细说明以及案例

nginx -g详细说明以及案例

nginx -g 是用于在启动 Nginx 时传递全局指令的命令。这些指令会覆盖 Nginx 配置文件中的默认设置。

语法

nginx -g "directive;"

常用指令

  1. daemon off;
    让 Nginx 在前台运行,而不是作为守护进程。

    nginx -g "daemon off;"
  2. master_process off;
    禁用主进程,仅使用工作进程运行。

    nginx -g "master_process off;"
  3. worker_processes auto;
    自动设置工作进程数。

    nginx -g "worker_processes auto;"
  4. error_log /path/to/error.log debug;
    设置错误日志路径和日志级别。

    nginx -g "error_log /var/log/nginx/error.log debug;"
  5. pid /path/to/nginx.pid;
    指定 Nginx 主进程的 PID 文件路径。

    nginx -g "pid /var/run/nginx.pid;"

案例

  1. 在前台运行 Nginx 并设置日志级别为 debug

    nginx -g "daemon off; error_log /var/log/nginx/error.log debug;"
  2. 禁用主进程并自动设置工作进程数

    nginx -g "master_process off; worker_processes auto;"
  3. 指定 PID 文件路径并设置日志级别为 notice

    nginx -g "pid /var/run/nginx.pid; error_log /var/log/nginx/error.log notice;"

通过这些指令,可以在启动 Nginx 时灵活调整配置,而无需修改配置文件。