安装配置nginx
song

更新软件源,安装Nginx

1
2
yum install epel-release
yum install nginx

检查安装版本、安装路径

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@racknerd-25d802 software]# nginx -v
nginx version: nginx/1.20.1

[root@racknerd-25d802 software]# nginx -V
nginx version: nginx/1.20.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.1.1k FIPS 25 Mar 2021
TLS SNI support enabled
configure arguments: --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 --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 --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-compat --with-debug --with-file-aio --with-google_perftools_module --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_degradation_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_mp4_module --with-http_perl_module=dynamic --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-http_xslt_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-stream_ssl_preread_module --with-threads --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E'

[root@racknerd-25d802 software]# whereis nginx
nginx: /usr/sbin/nginx /usr/lib64/nginx /etc/nginx /usr/share/nginx /usr/share/man/man8/nginx.8.gz /usr/share/man/man3/nginx.3pm.gz

[root@racknerd-25d802 software]# which nginx
/usr/sbin/nginx

默认情况下,Nginx的安装路径为/usr/share/nginx,Nginx的配置文件路径为/etc/nginx/nginx.conf。

nginx帮助手册命令以及常用命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 查看nginx版本号
nginx -v

# 查看nginx版本号以及其他配置参数,包含安装、配置文件路径以及内置模块等信息
nginx -V

# 查看nginx帮助文档
nginx -h

# 在Linux系统中,可以使用man命令查看nginx详细帮助文档
man nginx

# 检测nginx配置文件语法是否正确
nginx -t

# 检测nginx配置文件语法是否正确,并把nginx配置文件信息输出到屏幕
nginx -T

# 设置nginx使用的配置文件(默认使用:/usr/local/etc/nginx/nginx.conf)
nginx -c nginx_file.conf

# 向nginx主进程发送信号,stop, quit, reopen, reload
nginx -s stop # 停止Nginx进程


nginx -s quit # 快速停止Nginx进程,但允许完成已经接受的连接请求

nginx -s reopen # 重新打开日志文件

# 与stop不同,reload不会完全停止Nginx进程,而是平滑地重新加载配置文件,并在新的工作进程中对已有的连接进行服务,可在不停止服务的情况下实现动态更新配置
nginx -s reload

区别:

stop和quit都是停止Nginx进程,但quit允许之前接受的连接继续完成,而stop则会强制终止所有连接。 reload会重载Nginx的配置文件,并启动新的worker进程,新的worker进程会用新的配置重新跑,旧的worker进程会继续处理旧有的连接,新配置生效后,新的连接会从新的worker进程处理,而reopen是重新打开日志文件,在日志文件较大的时候,方便日志切割。

设置开启启动

1
systemctl enable nginx

Nginx常用的全局指令

  1. user:指定Nginx所使用的系统用户和用户组,默认为nobody;
  2. worker_processes:指定Nginx工作进程的数量,默认为1;
  3. worker_cpu_affinity:指定每个worker进程绑定的CPU核心;
  4. worker_rlimit_nofile:指定每个worker进程允许打开的最大文件数;
  5. pid:指定Nginx进程号存放的位置,默认为/var/run/nginx.pid;
  6. access_log:指定Nginx访问日志存放的位置和格式;
  7. error_log:指定Nginx错误日志存放的位置和格式,常用的格式有debug,info,notice,warn,error和crit;
  8. events:指定Nginx事件模块的配置,常见的配置包括worker_connections(最大连接数)和use,epoll和kqueue,以及其他一些事件模块配置;
  9. http:指定Nginx HTTP模块的配置,包括server_names_hash_max_size(域名哈希表的最大尺寸)、server_names_hash_bucket_size(每个哈希桶的大小)、client_max_body_size(前端客户端传输的最大请求体大小)等等;
  10. server:指定Nginx所有server块的配置,包括listen(服务器监听端口)、server_name(服务器域名)、access_log(访问日志路径和格式)等等。
  11. keepalive_timeout:指定HTTP keep-alive连接超时时间。
  12. multi_accept:一个请求是否能够被多个 worker 处理。
  13. gzip:是否启用gzip压缩传输;
  14. ssl:开启SSL支持;
  15. use:添加 nginx 模块;

nginx常用的内置变量

  1. $request_method:所使用的 HTTP 请求方法,如 GET、POST、PUT、DELETE、HEAD 等。
  2. $host: 请求的主机名。不显示端口
  3. $http_host: 请求的主机名。端口存在则显示
  4. $proxy_host: 请求的主机名。默认80端口不显示 其他显示
  5. $http_referer: 请求的 HTTP referer 头部字段。
  6. $http_user_agent: 请求的 User-Agent 头部字段。
  7. $remote_addr: 客户端的 IP 地址。
  8. $request_body: 请求主体的内容。
  9. $request_uri: 请求的 URI 包括参数部分。
  10. $args: 请求中的参数部分,同$query_string。
  11. $scheme: 请求使用的协议,比如 http 或 https。
  12. $server_name: Nginx 配置中当前虚拟主机的名称。
  13. $server_port: Nginx 监听的端口号。

通过使用这些内置变量,可以方便地在Nginx配置中进行动态参数配置、请求头部字段获取等操作。同时,还可以像使用任何其他Nginx变量一样,将它们与各种指令(如if、set、map)结合使用。

以下是Nginx中常用的一些内置变量及其说明:

  1. $request_method:所使用的 HTTP 请求方法,如 GET、POST、PUT、DELETE、HEAD 等。示例:

    1
    2
    3
    if ($request_method = POST) {
    return 405;
    }
  2. $host:请求头中的主机名,通常用于虚拟主机配置。示例:

1
2
3
4
5
6
7
8
server {
listen 80;
server_name example.com;

location / {
proxy_pass http://backend;
}
}
由 Hexo 驱动 & 主题 Keep