1. 源码安装nginx,并提供服务脚本。
源码获取途径:http://nginx.org/download/
1、安装pere库,兼容正则表达式
[root@centos7 ~]# yum install pcre-devel -y
2、安装nginx的依赖包[root@centos7 ~]# yum install openssl-devel -y
3、创建用户和组
[root@centos7 ~]# groupadd -r -g 995 nginx
[root@centos7 ~]# useradd -r -u 995 -g 995 -s /sbin/nologin -M nginx4、将下载好的压缩文件解压到指定路径
[root@centos7 ~]# tar xf nginx-1.18.0.tar.gz -C /usr/local/src/
5、源码安装需要编译,安装gcc gcc-c++ make
[root@centos7 nginx-1.18.0]# yum install gcc gcc-c++ make -y
6、进入文件解压好的目录
[root@centos7 ~]# cd /usr/local/src/
[root@centos7 src]# ll
total 0
drwxr-xr-x 8 1001 1001 158 Apr 21 2020 nginx-1.18.0
[root@centos7 src]# cd nginx-1.18.0/
7、安装nginx,需要什么安装什么直到出现汇总信息[root@centos7 nginx-1.18.0]#./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-http_stub_status_module \
> --with-http_ssl_module
8、开始编译
[root@centos7 nginx-1.18.0]# make
9、编写配置文件[root@centos7 nginx-1.18.0]# cat /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
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
10、重启nginx服务[root@centos7 nginx-1.18.0]# systemctl daemon-reload
[root@centos7 nginx-1.18.0]# systemctl enable --now nginx
11、查看端口[root@centos7 nginx-1.18.0]# netstat -lnupt |grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 22008/nginx: master
2. 配置基于域名的虚拟主机。
1、添加一个IP地址并启用
[root@centos7 ~]# nmcli con mod ens33 +ipv4.addresses 192.168.164.139/24
[root@centos7 ~]# nmcli con up ens33
[root@centos7 ~]# nmcli con reload2、切换目录并创建站点目录
[root@centos7 ~]# cd /usr/local/nginx/html
[root@centos7 html]# for name in bbs news
> do
> mkdir $name
> done
[root@centos7 html]# ll
total 8
-rw-r--r-- 1 root root 494 Oct 16 23:26 50x.html
drwxr-xr-x 2 root root 6 Oct 16 23:53 bbs
-rw-r--r-- 1 root root 612 Oct 16 23:26 index.html
drwxr-xr-x 2 root root 6 Oct 16 23:53 news3、创建主页文件 ---将"$name test page."内容写入相对应的文件
[root@centos7 html]# for name in bbs news; do echo "$name test page." > $name/index.html; done
4、修改配置文件,一个server是一个虚拟主机
[root@centos7 html]# cat /etc/nginx/conf.d/vhost.conf
server {
listen 192.168.164.138:80;
server_name localhost;
location / {
root /usr/share/nginx/html/bbs/;
index index.html index.htm;
}
}
server {
listen 192.168.164.139:80;
server_name localhost;
location / {
root /usr/share/nginx/html/news/;
index index.html index.htm;
}
}5、重启服务
[root@centos7 html]# systemctl restart nginx
6、测试[root@centos7 html]# curl 192.168.164.138
bbs test page.
[root@centos7 html]# curl 192.168.164.139
news test page.
3. 配置nginx基于端口的访问控制。
1、修改配置文件,不加IP表示本地IP地址。(IP地址可写可不写)--- 修改端口号
[root@centos7 conf.d]# cat vhost.conf
server {
listen 192.168.164.138:80;
server_name localhost;
location / {
root /usr/share/nginx/html/bbs/;
index index.html index.htm;
}
}
server {
listen 192.168.164.138:81;
server_name localhost;
location / {
root /usr/share/nginx/html/news/;
index index.html index.htm;
}
}2、检查配置文件的语法是否正确
[root@centos7 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful3、重启nginx服务
[root@centos7 conf.d]# systemctl restart nginx
4、查看并过滤端口号
[root@centos7 conf.d]# netstat -lnupt |grep 8
tcp 0 0 192.168.164.138:80 0.0.0.0:* LISTEN 26595/nginx: master
tcp 0 0 192.168.164.138:81 0.0.0.0:* LISTEN 26595/nginx: master5、测试
[root@centos7 conf.d]# curl 192.168.164.138:80
bbs test page.
[root@centos7 conf.d]# curl 192.168.164.138:81
news test page.
[root@centos7 conf.d]#
配置nginx基于hosts解析的访问控制。
1、修改配置文件 -- 将server_name改为对应的域名
[root@centos7 conf.d]# cat vhost.conf
server {
listen 192.168.164.138:80;
server_name bbs.jojo.org;
location / {
root /usr/share/nginx/html/bbs/;
index index.html index.htm;
}
}
server {
listen 192.168.164.138:80;
server_name news.jojo.org;
location / {
root /usr/share/nginx/html/news/;
index index.html index.htm;
}
}2、重启服务
[root@centos7 conf.d]# systemctl restart nginx
3、编辑etc/hosts中的IP与域名的映射
[root@centos7 conf.d]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.164.138 bbs.jojo.org news.jojo.org4、测试
[root@centos7 conf.d]# curl bbs.jojo.org
bbs test page.
[root@centos7 conf.d]# curl news.jojo.org
news test page.
4. 配置nginx rewrite,要求如果访问不存在的任意网页都重定向到错误页面,错误页面内容自行定义。
1、创建test1目录
mkdir -p /usr/share/nginx/html/test1/
2、定义错误页面
echo "this is error" > /usr/share/nginx/html/test1/err.html
3、修改配置文件
[root@centos7 test1]# cat /etc/nginx/conf.d/vhost.conf
server {
listen 192.168.164.138:80;
server_name www.test1.com;
location / {
root /usr/share/nginx/html/bbs/;
index index.html index.htm;
if (!-f $request_filename) {
rewrite /.* err.html permanent;}}
}
4、重启服务
[root@centos7 test1]# systemctl restart nginx