栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

nginx源码安装(负载均衡集群)——web集群

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

nginx源码安装(负载均衡集群)——web集群

web集群
  • 1. 源码安装nginx,并提供服务脚本。
  • 2. 配置基于域名的虚拟主机。
  • 3. 配置nginx基于用户和地址的访问控制。
    • 基于用户
    • 基于地址
  • 4. 配置nginx rewrite,要求如果访问不存在的任意网页都重定向到错误页面,错误页面内容自行定义。

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

以tengine为例

1.复制链接并下载:

wget -c http://tengine.taobao.org/download/tengine-2.3.0.tar.gz

2.安装编译环境:gcc、gcc-c++、make

yum install gcc gcc-c++.x86_64 make -y

3.解压缩到:/usr/local/src/

tar -xf tengine-2.3.0.tar.gz -C /usr/local/src/
cd /usr/local/src/tengine-2.3.0/

4.创建用户(模仿rpm包安装的nginx用户)

useradd -s /sbin/nologin -u 994 -g 994 -c "Nginx web server"  nginx

5.尝试安装:可能缺少东西如下
–prefix:安装在哪、–user:指定用户、–group:指定用户组、–with:启用什么服务、:强制换行

./configure --user=nginx --group=nginx 
 --prefix=/usr/local/nginx 
 --with-http_stub_status_module 
 --with-http_ssl_module



注意:缺库、头文件都要装开发包如下

yum install pcre-devel -y
yum install openssl-devel -y

补全后不再报错,进行下面的操作

[root@localhost ~]# make
[root@localhost ~]# make install
  1. 启动服务(进程)
/usr/local/nginx/sbin/nginx -t   #检查语法是否有错

/usr/local/nginx/sbin/nginx     #启动服务
 netstat -lnupt | grep 80     #查看是否启动成功

7.创建服务脚本
vim /usr/lib/systemd/system/nginx.service

[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running `nginx -t` from the cmdline.
# https://bugzilla.redhat.com/show_bug.cgi?id=1268621
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

8.验证路径是否正确

ll /usr/local/nginx/conf/nginx.conf
ll /usr/local/nginx/logs/nginx.pid

9.杀掉之前的进程,验证服务脚本配置是否正确

killall -9 nginx

10.重新加载配置、启动服务并验证是否成功

systemctl daemon-reload
systemctl start nginx
 netstat -lnupt | grep 80

yum whatprovides */killall #查询指令来自哪个包

rpm包安装

补充:wget指令,-c 是断续下载 -O是下载并以不同的文件名保存,并默认会以最后一个符合'/'的后面的字符来命名

1.下载扩展源

wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

2.安装软件包:nginx

[root@localhost ~]# yum install nginx -y

3.启动服务并验证是否成功

[root@localhost ~]# systemctl start nginx

服务启动成功

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

这里使用模块化配置(若不只用则在主配置文件编辑:/usr/local/nginx/conf/nginx.conf)

1.在主配置文件添加下面的语句

2.创建目录

mkdir /usr/local/nginx/conf.d

3.创建并编辑配置文件

vim /usr/local/nginx/conf.d/vhost.conf
	server {
        listen       80;
        server_name  bbs.test.com;
        location / {
            root   html/bbs;
            index  index.html index.htm;
        }
    }

    server {
        listen       80;
        server_name  blog.test.com;
        location / {
            root   html/blog;
            index  index.html index.htm;
        }
    }

4.添加域名解析信息

vim /etc/vhost
	192.168.87.140  blog.test.com  bbs.test.com

5.启动服务并验证是否成功

systemctl restart nginx

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

/usr/local/nginx/conf.d/vhost.conf
1.在配置文件中添加语句,如下图

server {
    listen    192.168.150.120:80;
    server_name  www.b.com;
    access_log /data/web2/access.log combined;

    location / {
      autoindex on;
      root  /data/web2;
      index  index.html index.htm;
      auth_basic  "Restricted";
      auth_basic_user_file /data/web2/webpass;
    }

主要语句如下:

2.创建账号和密码(htpasswd指令需要用到httpd-tools包)
1.安装httpd-tools

2.创建账号和密码

3.重启测试

systemctl restart nginx

访问成功:

基于地址

#提示:从上到下的顺序,类似iptables。匹配到了便跳出。
/usr/local/nginx/conf.d/vhost.conf
1.在配置文件中添加语句,如下图

server {
    listen    192.168.87.140:80;
    server_name  bbs.test.com;
    access_log /usr/local/nginx/logs/access.log combined;

    location / {
      autoindex on;
      root  html/bbs;
      index  index.html index.htm;
      deny 192.168.87.141;
      allow 192.168.87.0/24;
      deny all;
    }
   }

主要语句如下:

2.重启测试

systemctl restart nginx

下图是主机141,注意:需要添加域名信息

测试得到,当主机双IP的时,依然可以访问
添加ip:192.168.87.233后

4. 配置nginx rewrite,要求如果访问不存在的任意网页都重定向到错误页面,错误页面内容自行定义。

/usr/local/nginx/conf.d/vhost.conf
1.在配置文件中添加语句,如下图

location / {
      root  html;
      index  index.html index.htm;

      if (!-f $request_filename) {
        rewrite /.* /err.html permanent;   # (permanent表示永久的重定向到这个页面)
}


2.创建错误页面

echo "没有这个页面" > /usr/local/nginx/html/blog/err.html

3.重启测试

systemctl restart nginx

注意:添加上述语句后,测试时要加-L,否则不显示页面内容

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/962281.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号