第三方模块是对nginx的功能扩展,第三方模块需要在编译nginx的时候使用参数--add-module=PATH指定扩展模块的源码包路径给Nginx扩展添加echo功能,echo模块
[root@app src]# cd /root/nginx/nginx-1.17.8
[root@app src]# yum install git -y
[root@app src]# git clone https://github.com/openresty/echo-nginx-module.git #把echo模块从github上克隆下来
[root@app src]# nginx -s stop #扩展nginx的功能需要从新编译,编译前必须停止服务;如果服务不停止,则无法用新生成的nginx二级制程序替代原有程序
[root@app nginx-1.16.1]#./configure --prefix=/usr/local/nginx --with-http_ssl_module --add-module=/克隆下来的路径/echo-nginx-module-master
#添加模块源码路径
[root@app nginx-1.16.1]# make -j 4 && make install
[root@app nginx-1.16.1]# nginx
location /echo {
echo "hello world";
default_type text/html;
}
当访问www.xxx.com/echo时,将会在浏览器上打印"hello world"
如果不加default_type text/html;,访问www.xxx.com/echo时,将会把它当做文件下载下来,因为echo默认不是mime里所支持的格式;加上这项是告诉浏览器用html的格式去解析它
Nginx常用内置变量
$remote_addr;
#存放了客户端的地址,注意是客户端的公网IP,也就是一家人访问一个网站,则会显示为路由器的公网IP。
$args;
#变量中存放了URL中的指令,例如http://www.magedu.net/main/index.do?id=20190221&partner=search中的id=20190221&partner=search;需要输入指令 时,要添加?进行隔开,两个指令中间需要用&进行隔开,args变量里面存放了id=20190221&partner=search
$document_root;
#所请求资源的location下面root所指定的路径,如/apps/nginx/html。
$document_uri;
#保存了当前请求中不包含指令的URI,注意是不包含请求的指令,比如http://www.magedu.net/main/index.do?id=20190221&partner=search会被定义为/main/index.do
$host;
#存放了请求的域名。
$http_user_agent;
#客户端浏览器的详细信息。
$http_cookie;
#客户端的cookie信息。用户登录后才可以看到cookie信息。
$remote_port;
#客户端请求Nginx服务器时随机打开的端口,这是每个客户端自己的端口。
$remote_user;
#已经经过Auth Basic Module验证的用户名。
$request_body_file;
#做反向代理时发给后端服务器的本地资源的名称。做反向代理时必须开启。
$request_method;
#请求资源的方式,GET/PUT/DELETE等。
$request_filename;
#当前请求的资源文件的路径名称,由root或alias指令与URI请求生成的文件绝对路径,如/apps/nginx/html/main/index.html
$request_uri;
#包含请求参数的原始URI,不包含主机名,如:/main/index.do?id=20190221&partner=search,打印整个uri。
$scheme;
#请求的协议,如ftp,https,http等。
$server_protocol;
#保存了客户端请求资源使用的协议的版本,如HTTP/1.0,HTTP/1.1,HTTP/2.0等。
$server_addr;
#保存了服务器的IP地址。
$server_name;
#请求的服务器的主机名,server_name。
$server_port;
#请求的服务器的端口号。
启动Nginx出现Failed to start nginx.service:unit not found
vi /etc/init.d/nginx
把下方赋值进去
#!/bin/sh
# nginx - this script starts and stops the nginx daemin
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /usr/local/nginx/conf/nginx.conf
# pidfile: /usr/local/nginx/logs/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
lockfile=/var/lock/subsys/nginx
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
接下来就依次操作以下命令:
cd /etc/init.d
chmod 755 /etc/init.d/nginx
chkconfig --add nginx
开启Nginx:
service nginx start