1. 源码安装nginx,并提供服务脚本。
#下载nginx
[root@localhost ~]# wget https://nginx.org/download/nginx-1.20.1.tar.gz
[root@localhost ~]# ls
anaconda-ks.cfg apache-tomcat-8.5.20.tar.gz nginx-1.20.1.tar.gz#创建Nginx系统用户及Nginx用户组
[root@localhost ~]# useradd nginx -c "Nginx web servber" -d /var/lib/nginx -s /sbin/nologin -r[root@localhost ~]# id nginx
uid=997(nginx) gid=995(nginx) groups=995(nginx)
[root@localhost ~]# grep nginx /etc/passwd
nginx:x:997:995:Nginx web servber:/var/lib/nginx:/sbin/nologin#将nginx压缩包解压至指定目录
[root@localhost ~]# tar -xf nginx-1.20.1.tar.gz -C /usr/local/src#进入nginx文件目录
[root@localhost ~]# cd /usr/local/src/nginx-1.20.1#配置
[root@localhost nginx-1.20.1]# ./configure \
> --prefix=/usr/local/nginx \
> --sbin-path=/usr/sbin/nginx \
> --modules-path=/usr/lib64/nginx/modules \
> --conf-path=/etc/nginx/nginx.conf \
> --error-log-path=/var/log/nginx/error.log \
> --http-log-path=/var/log/nginx/access.log \
> --pid-path=/run/nginx.pid \
> --lock-path=/run/lock/subsys/nginx \
> --user=nginx --group=nginx
checking for OS
+ Linux 3.10.0-1160.71.1.el7.x86_64 x86_64
checking for C compiler ... not found./configure: error: C compiler cc is not found
#根据报错安装依赖包
[root@localhost nginx-1.20.1]# yum install -y gcc gcc-c++
#再次配置查看
[root@localhost nginx-1.20.1]# ./configure --prefix=/usr/local/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx
checking for OS./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.#根据报错安装依赖包
[root@localhost nginx-1.20.1]# yum install -y pcre-devel
#再次配置查看
[root@localhost nginx-1.20.1]# ./configure --prefix=/usr/local/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx
checking for OS
./configure: error: the HTTP gzip module requires the zlib library.
You can either disable the module by using --without-http_gzip_module
option, or install the zlib library into the system, or build the zlib library
statically from the source with nginx by using --with-zlib=<path> option.#根据报错安装依赖包
[root@localhost nginx-1.20.1]# yum install -y zlib-devel
#无报错后进行编译安装
[root@localhost nginx-1.20.1]# make
[root@localhost nginx-1.20.1]# make install#配置服务脚本
[root@localhost nginx-1.20.1]# vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/usr/sbin/nginx -s reload
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true[Install]
WantedBy=multi-user.target#重新加载配置服务
[root@localhost nginx-1.20.1]# systemctl daemon-reload
#启动nginx
[root@localhost nginx-1.20.1]# systemctl start nginx#查看nginx状态
[root@localhost nginx-1.20.1]# systemctl status nginx
#浏览器输入IP地址查看
2. 配置基于域名的虚拟主机。
#配置文件加入虚拟主机
[root@localhost nginx-1.20.1]# vim /etc/nginx/nginx.conf
server{
listen 80; #监听端口
server_name abc.okok.com; #自定义域名
location / {
root /www/web; #站点目录路径
index index.html index.htm;
}
}#创建站点目录
[root@localhost nginx-1.20.1]# mkdir -p /www/web#配置站点首页文件内容
[root@localhost nginx-1.20.1]# echo "welcom to abc.okok.com">>/www/web/index.html#检查拼写
[root@localhost nginx-1.20.1]# nginx -t#重启nginx服务
[root@localhost nginx-1.20.1]# systemctl restart nginx#配置hosts文件 将域名和IP写入
[root@localhost nginx-1.20.1]# echo "192.168.6.128 abc.okok.com" >> /etc/hosts#访问测试
[root@localhost nginx-1.20.1]# curl abc.okok.com
welcom to abc.okok.com
3. 配置nginx基于用户和地址的访问控制。
[root@localhost nginx-1.20.1]# yum install -y httpd-tools
[root@localhost nginx-1.20.1]# htpasswd -c /usr/local/nginx/passwd user1
New password:
Re-type new password:
Adding password for user user1
[root@localhost nginx-1.20.1]# vim /etc/nginx/nginx.conf
server{
listen 192.168.6.128:80;
server_name abc.okok.com;
location / {
root /www/web;
index index.html index.htm;
deny 192.168.1.213;
allow all;
auth_basic "Restricted";
auth_basic_user_file /www/web/passwd;
}
}
linux系统配置hosts文件:vim /etc/hosts
window系统配置hosts文件:C:\Windows\System32\drivers/hosts
访问测试
4. 配置nginx rewrite,要求如果访问不存在的任意网页都重定向到错误页面,错误页面内容自行定义。
server {
listen 80;
server_name abc.okok.com;
location / {
root /www/web;
index index.html index.htm;
if (!-f $request_filename) {
rewrite /*.err.html permanent;}
}
}