20230122=nginx学习笔记

134 阅读4分钟

安装

安装依赖

pcre(重定向支持)和openssl(https支持,如果不需要https可以不安装。)

yum install -y pcre-devel 
yum -y install gcc make gcc-c++ wget
yum -y install openssl openssl-devel 

下载解压

wget http://nginx.org/download/nginx-1.9.2.tar.gz
tar zxf nginx-1.9.2.tar.gz

编译安装

[root@ecs-node02 nginx-1.9.2]# ./configure 
checking for OS
 + Linux 6.1.6-1.el8.elrepo.x86_64 x86_64
checking for C compiler ... found
 + using GNU C compiler
 + gcc version: 8.5.0 20210514 (Red Hat 8.5.0-4) (GCC) 
...
creating objs/Makefile

Configuration summary
  + using system PCRE library
  + OpenSSL library is not used
  + md5: using system crypto library
  + sha1: using system crypto library
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"
[root@ecs-node02 nginx-1.9.2]# make
make -f objs/Makefile
make[1]: Entering directory '/root/nginx-1.9.2'
...
make[1]: Leaving directory '/root/nginx-1.9.2'

遇到错误1: src/core/ngx_murmurhash.c: In function ‘ngx_murmur_hash2’: src/core/ngx_murmurhash.c:37:11: error: this statement may fall through [-Werror=implicit-fallthrough=] h ^= data[2] << 16; ~~^~~~~~~~~~~~~~~~ src/core/ngx_murmurhash.c:38:5: note: here case 2: ^~~~ src/core/ngx_murmurhash.c:39:11: error: this statement may fall through [-Werror=implicit-fallthrough=] h ^= data[1] << 8; ~~^~~~~~~~~~~~~~~ src/core/ngx_murmurhash.c:40:5: note: here case 1: ^~~~

解决:进入到nginx-1.9.2目录下(即解压的目录),找到当前目录下找到objs文件夹,并进入,打开文件Makefile,找到有一下内容的这行:

CFLAGS = -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g 1

把这行内容中的 “-Werror”去掉

-Werror: gcc将所有的警告当成错误进行处理

遇到报错2: src/os/unix/ngx_user.c:36:7: error: ‘struct crypt_data’ has no member named ‘current_salt’

解决:注解掉36行

[root@ecs-node02 nginx-1.9.2]# make install make -f objs/Makefile install ... make[1]: Leaving directory '/root/nginx-1.9.2'

测试

[root@ecs-node02 sbin]# ./nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

设置全局nginx命令

[root@ecs-node02 sbin]# vi ~/.bash_profile

修改成以下:

PATH=$PATH:$HOME/bin:/usr/local/nginx/sbin/
export PATH
[root@ecs-node02 sbin]# source ~/.bash_profile

配置立即生效

开机自启动

开机自启动方法一:

编辑 vi /lib/systemd/system/nginx.service 文件,没有创建一个 touch nginx.service 然后将如下内容根据具体情况进行修改后,添加到nginx.service文件中:

[Unit]
Description=nginx
After=network.target remote-fs.target nss-lookup.target

[Service]

Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStartPost=/bin/sleep 0.1
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $(/bin/cat /usr/local/nginx/logs/nginx.pidpid)
ExecStop=/bin/kill -s QUIT $(/bin/cat /usr/local/nginx/logs/nginx.pidpid)
PrivateTmp=true

[Install]
WantedBy=multi-user.target
  • [Unit]:服务的说明
  • Description:描述服务
  • After:描述服务类别
  • [Service]服务运行参数的设置
  • Type=forking是后台运行的形式
  • ExecStart为服务的具体运行命令
  • ExecReload为重启命令
  • ExecStop为停止命令
  • PrivateTmp=True表示给服务分配独立的临时空间

注意:[Service]的启动、重启、停止命令全部要求使用绝对路径。

[Install]运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3

保存退出。

设置开机启动,使配置生效:

# 启动nginx服务
systemctl start nginx.service
# 停止开机自启动
systemctl disable nginx.service
# 查看服务当前状态
systemctl status nginx.service
# 查看所有已启动的服务
systemctl list-units --type=service
# 重新启动服务
systemctl restart nginx.service
# 设置开机自启动
systemctl enable nginx.service
# 输出下面内容表示成功了
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
systemctl is-enabled servicename.service # 查询服务是否开机启动
systemctl enable *.service # 开机运行服务
systemctl disable *.service # 取消开机运行
systemctl start *.service # 启动服务
systemctl stop *.service # 停止服务
systemctl restart *.service # 重启服务
systemctl reload *.service # 重新加载服务配置文件
systemctl status *.service # 查询服务运行状态
systemctl --failed # 显示启动失败的服务

注:*代表某个服务的名字,如http的服务名为httpd

开机自启动方法二:

vi /etc/rc.local

# 在 rc.local 文件中,添加下面这条命令
/usr/local/nginx/sbin/nginx start

如果开机后发现自启动脚本没有执行,你要去确认一下rc.local这个文件的访问权限是否是可执行的,因为rc.local默认是不可执行的。修改rc.local访问权限,增加可执行权限:

# /etc/rc.local是/etc/rc.d/rc.local的软连接,
chmod +x /etc/rc.d/rc.local

全局变量

变量说明变量说明
$args这个变量等于请求行中的参数,同$query_string$remote_port客户端的端口。
$content_length请求头中的Content-length字段。$remote_user已经经过Auth Basic Module验证的用户名。
$content_type请求头中的Content-Type字段。$request_filename当前请求的文件路径,由root或alias指令与URI请求生成。
$document_root当前请求在root指令中指定的值。$schemeHTTP方法(如http,https)。
$host请求主机头字段,否则为服务器名称。$server_protocol请求使用的协议,通常是HTTP/1.0或HTTP/1.1。
$http_user_agent客户端agent信息$server_addr服务器地址,在完成一次系统调用后可以确定这个值。
$http_cookie客户端cookie信息$server_name服务器名称。
$limit_rate这个变量可以限制连接速率。$server_port请求到达服务器的端口号。
$request_method客户端请求的动作,通常为GET或POST。$request_uri包含请求参数的原始URI,不包含主机名,如:/foo/bar.php?arg=baz。
$remote_addr客户端的IP地址。$uri不带请求参数的当前URI,$uri不包含主机名,如/foo/bar.html。
$document_uri与$uri相同。--