nginx安装
Nginx版本
Openresty: lua语言二次开发版本
Tengine: C和C++二次开发版本
Centos 7安装nginx1.19.6
wget https://mirrors.huaweicloud.com/nginx/nginx-1.19.6.tar.gz
tar -zxvf nginx-1.19.6.tar.gz
cd nginx-1.19.6/
yum -y install gcc gcc-c++ autoconf automake make
yum install pcre-devel -y
yum install zlib-devel -y
./configure --prefix=/usr/local/nginx
cd nginx-1.19.6/
make
make install
安装完成后启动:cd /usr/local/nginx/sbin/; ./nginx
关闭防火墙:
systemctl stop firewalld.service
禁止防火墙开启启动:
systemctl disable firewalld.service
一般不禁止防火墙,我们可以在防火墙上放开我们使用的端口:
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --reload
Nginx配置为系统该服务
cd /usr/lib/systemd/system
vi ngxin.service,加入如下内容:
[Unit]
Description=Nginx - web server
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
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecQuit=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
然后重新加载systemctl服务: systemctl daemon-reload;
就可以使用如下命令操作nginx(需要先将已经启动的nginx进程kill):
systemctl start nginx.service
systemctl status nginx.service
systemctl stop nginx.service
Nginx使用
1. location路径匹配问题
nginx配置文件中loction路径匹配默认使用的是最大前缀匹配原则,如下的配置,/test /testxxxx,都可以匹配
location /test {
......
}
要精确配置的话,改为如下的就可以了:
location = /test {
......
}
2. 热部署
- 关闭缓存需要重启才能生效;lua_code_cache off需要配置在http -> server节点下;
- 只对content_by_lua_file生效;
3. 部署文件列表服务器和用户名认证
其中认证使用到了httpd-tools组件,需要先安装:yum install httpd-tools -y,然后执行:
htpasswd -c htpasswd root123123,会提示如数密码以及确认密码,完成后就会在当前面目录生成htpasswd文件,里面内容就是用户名和加密后的密码
server {
listen 2222;
server_name 10.0.0.25;
location /reqheader {
default_type text/html; charset utf-8;
content_by_lua_file /root/tools/test.lua;
}
location /list {
root /root;
autoindex on; # 开启后在/root/list下的文件以及目录都可以在页面展示
auth_basic "Pls username and pwd";
auth_basic_user_file /usr/local/openresty/nginx/conf/htpasswd;
}
}
配置好后,访问list路径就会提示输入用户名密码:
输入用户名密码后,就可以看到文件服务器列表: