安装nginx,及其相关配置

1. 源码安装nginx,并提供服务脚本。

2. 配置基于域名的虚拟主机。

3. 配置nginx基于用户和地址的访问控制。

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 //重启服务器 

3. 配置nginx基于用户和地址的访问控制。
4. 配置nginx rewrite,要求如果访问不存在的任意网页都重定向到错误页面,错误页面内容自行定义。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值