4. 配置nginx rewrite,要求如果访问不存在的任意网页都重定向到错误页面,错误页面内容自行定义。
1. 源码安装nginx,并提供服务脚本。
安装nginx所需的pere库
pere,peri兼容正则表达式
yum install -y pcre-devel
安装nginx
安装openssl-devel
yum install -y openssl-devel
检查并安装nginx基础依赖包pcre-devel、openssl-devel
rpm -qa | egrep 'pcre-devel|openssl-devel'
开始安装nginx
创建组和用户
groupadd -r -g 995 nginx
useradd -r -u 995 -g 995 -s /sbin/nologin -M nginx
下载nginx
wget http://nginx.org/download/nginx-1.18.0.tar.gz
解压
tar xf nginx-1.18.0.tar.gz -C /usr/local/src/
下载编译器
yum install gcc gcc-c++ make -y
进入目录
./configure --help
./configure --user=nginx --group=nginx \
--prefix=/usr/local/nginx \
--with-http_ssl_module \
--with-http_auth_request_module \
--with-http_gzip_static_module \
--with-http_gunzip_module \
--with-http_stub_status_module
编译
make
安装
make install
修改配置文件
vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/After=network-online.target remote-fs.targe nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
测试:systemctl enable --now nginx
查看端口netstat -lnupt | grep 8080
2. 配置基于域名的虚拟主机。
配置
[root@localhost ~]# nmcli con mod ens33 +ipv4.addresses 172.25.10.200/24
[root@localhost ~]# nmcli con up ens33
[root@localhost ~]# nmcli con reload
修改nginx配置文件nginx.conf
在server{}的后面加上以下命令
cd /usr/local/nginx/conf
vim nginx.conf
server{
listen 80;
server_name web.a.com;
location / {
root /www/web;
index index.html index.htm;
}
}
server{
listen 80;
server_name blog.b.com;
location / {
root /www/blog;
index index.html index.htm;
}
}
之后
/usr/local/nginx/sbin/nginx -t
//检查语法错误/usr/local/nginx/sbin/nginx -s reload
//重启服务器