安装
安装依赖
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指令中指定的值。 | $scheme | HTTP方法(如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相同。 | - | - |