nginx 编译参数

431 阅读1分钟

Nginx检查配置文件是否正确

nginx -tc /etc/nginx/nginx.conf

nginx -V 查看编译选项

编辑选项 作用
--prefix=/usr/share/nginx
--sbin-path=/usr/sbin/nginx
--modules-path=/usr/lib64/nginx/modules
--conf-path=/etc/nginx/nginx.conf
--error-log-path=/var/log/nginx/error.log
--http-log-path=/var/log/nginx/access.log
--pid-path=/var/run/nging.pid
--lock-path=/var/run/nginx.lock
安装的目录或路径
--http-client-body-temp-path=/var/lib/nginx/tmp/client_body
--http-proxy-temp-path=/var/lib/nginx/tmp/proxy
--http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi
--http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi
--http-scgi-temp-path=/var/lib/nginx/tmp/scgi
执行对应模块时,Nginx所保留的临时性文件
--user=nginx
--group=nginx
设定Nginx进程启动的用户和用户组
--with-cc-opt=parameters 设置额外的参数将被添加到CFLAGS变量

Nginx默认配置语法

user 设置nginx服务的系统使用用户
worker_processes 工作进程数,建议跟cpu核数一样
error_log nginx错误日志
pid nginx服务启动时候pid
events worker_connections 每个进程允许的最大连接数
use 工作进程数
http 配置

Nginx日志类型

包括: error.log access.log

log_format Context: http (配置在http对象中)

Nginx 变量

HTTP请求变量: arg_PARAMETER, http_HEADER, sent_http_HEADER 内置变量:Nginx内置的,参考官网文档 Logging to syslog 自定义变量:自己定义

example:

log_format compression '$remote_addr - $remote_user [$time_local] '
                       '"$request" $status $bytes_sent '
                       '"$http_referer" "$http_user_agent" "$gzip_ratio"';

access_log /spool/logs/nginx-access.log compression buffer=32k;

Nginx配置参数

编译参数 作用
http_stub_status_modules Nginx客户端状态

nginx.org/en/docs/htt…

Syntax: stub_status;

Default: —

Context: server, location

配置实例

浏览器访问: http://localhost/mystatus

Active connections: 1

server accepts handled requests 13 13 34

Reading: 0 Writing: 1 Waiting: 0

  • 参数释义

Active connections:当前活动客户端连接的数量,包括Waiting连接。

accepts:接受的客户端连接总数。

handled:处理的连接总数。通常情况下该值等于accepts的值,除非达到某个资源限制(例如, worker_connections限制)。

requests:客户端请求的总数。

Reading:nginx正在读取请求头的当前连接数。

Writing:nginx将响应写回客户端的当前连接数。

Waiting:当前等待请求的空闲客户端连接数。

--with-http-random-index-moudule 目录中随机一个主页

Syntax: random_index on | off;

Default: random_index off;

Context: location

location /random {
	random_index on; # 配置打开
	root /var/opt/ ; # 随机页面目录
}
编译参数 作用
--with-http_sub_module HTTP内容替换
  • sub_filter 替换响应内容
Syntax:	sub_filter string replacement;
Default:	—
Context:	http, server, location
  • 允许在替换期间保留原始响应的“上次修改”头字段,以方便响应缓存。默认情况下,在处理过程中修改响应的内容时会删除头字段。
Syntax:	sub_filter_last_modified on | off;
Default:	sub_filter_last_modified off;
Context:	http, server, location
This directive appeared in version 1.5.1.
  • 指定只替换第一个还是全局替换
Syntax:	sub_filter_once on | off;
Default: sub_filter_once on;
Context:	http, server, location

示例

location / {
    sub_filter '<a href="http://127.0.0.1:8080/'  '<a href="https://$host/';
    sub_filter '<img src="http://127.0.0.1:8080/' '<img src="https://$host/';
    sub_filter_once on;
}