1、 源码安装nginx,并提供服务脚本。
下载解压软件
wget -c http://nginx.org/download/nginx-1.20.0.tar.gz
tar xf nginx-1.20.0.tar.gz -C /usr/local/src/
cd /usr/local/src/nginx-1.20.0/
创建用户
useradd -r -s /sbin/nologin -M nginx
安装环境和依赖
yum install gcc gcc-c c++ make -y
yum install pcre-devel -y
yum install openssl-devel -y
yum install perl-ExtUtils-Embed
./configure --prefix=/usr/local/nginx
--user=nginx --group=nginx
--with-threads
--with-http_ssl_module
--with-http_sub_module
--with-http_gzip_static_module
--with-http_auth_request_module
--with-http_stub_status_module
--with-http_perl_module
--with-stream
--with-pcre
make
make install
配置环境变量或软连接
ln -sv /usr/local/nginx/sbin/nginx /usr/sbin/nginx
配置服务文件
vim /usr/lib/systemd/system/nginx.service
systemctl daemon-reload
systemctl start nginx
验证
2、 配置基于域名的虚拟主机。
配置子配置目录
mkdir conf.d
vim nginx.conf
systemctl restart nginx
修改静态页面
cd /usr/local/nginx/html/
mv index.html{,.bak}
vim index.html
配置虚拟主机
cd conf.d/
[root@localhost nginx]# vim vhost.conf
systemctl restart nginx
mkdir /data/web{1,2} -p
echo "web1 test" >/data/web1/index.html
echo "web2 test" >/data/web2/index.html
给测试机配hosts
3、 配置nginx基于用户和地址的访问控制。
基于地址
vim conf.d/vhost.conf
systemctl restart nginx
主机130
主机131
二、基于用户访问
vim conf.d/vhost.conf
systemctl restart nginx
安装认证工具
yum install httpd-tools
htpasswd -c -m /data/web1/webpass tom
4、 配置nginx rewrite,要求如果访问不存在的任意网页都重定向到错误页面,错误页面内容自行定义
配置错误页面
echo "web not exist" >/data/web1/err.html
vim conf.d/vhost.conf
测试