nginx

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 nginx

4、将下载好的压缩文件解压到指定路径

[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 reload

2、切换目录并创建站点目录

[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 news

3、创建主页文件  ---将"$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 successful

3、重启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: master

5、测试

[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.org

4、测试

[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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值