Nginx安装与使用: Nginx之编译安装的nginx加入systemctl、Nginx命令

本文详细介绍了如何在Centos7上通过编译方式安装Nginx,包括安装依赖、配置、编译安装步骤,以及如何将Nginx加入systemctl管理,并讲解了nginx.conf文件结构、静态资源部署、反向代理和负载均衡的配置方法。

Nginx

介绍

​ Nginx是一款轻量级的Web服务器/反向代理服务器以及电子邮件代理服务器。

​ 特点:

  • 占内存少

  • 并发能力强

    官网:http://nginx.org/en/download.html

编译方式安装Nginx(Centos7)

安装依赖

yum -y install gcc pcre-devel zlib-devel openssl openssl-devel

下载

wget http://nginx.org/download/nginx-1.16.1.tar.gz

在这里插入图片描述

解压到当前目录就行

tar -zxvf nginx-1.16.1.tar.gz

创建nginx目录

mkdir -p  /usr/local/nginx

检查环境

[root@localhost nginx-1.16.1]# ./configure --prefix=/usr/local/nginx

安装

[root@localhost nginx-1.16.1]# make && make install

配置环境变量(这样任意路径下就可以使用nginx命令了)

[root@localhost conf]# vim /etc/profile
PATH=/usr/local/nginx/sbin:$JAVA_HOME/bin:$PATH
[root@localhost conf]# nginx -s reload

END

Nginx之编译安装的nginx加入systemctl

先删除nginx的进程

[root@localhost sbin]# ps -ef | grep nginx
root       4634      1  0 16:49 ?        00:00:00 nginx: master process ./nginx
nobody     4635   4634  0 16:49 ?        00:00:00 nginx: worker process
root       4668   1892  0 16:56 pts/0    00:00:00 grep --color=auto nginx
[root@localhost sbin]# kill -9 4634 4635
[root@localhost sbin]# ps -ef | grep nginx
root       4670   1892  0 16:57 pts/0    00:00:00 grep --color=auto nginx

编辑 nginx.service文件

[root@localhost sbin]# vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
 
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true
 
[Install]
WantedBy=multi-user.target

配置生效&&nginx重新启动&&nginx开机自启

[root@localhost sbin]# systemctl daemon-reload
[root@localhost sbin]# systemctl start nginx
[root@localhost sbin]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.

测试(关闭防火墙或者开放80端口)

在这里插入图片描述
在这里插入图片描述

访问
在这里插入图片描述

nginx目录结构

在这里插入图片描述

Nginx命令

检查文件正确性

在这里插入图片描述

启动nginx

在这里插入图片描述

或者

[root@localhost sbin]# systemctl start nginx
重新加载配置文件
[root@localhost conf]# vim nginx.conf
[root@localhost conf]# /usr/local/nginx/sbin/nginx -s reload

或者

[root@localhost sbin]# systemctl restart nginx

nginx配置文件结构(nginx.conf)

  • 全局快 : 和Nginx运行相关的全局配置

  • Events块:和网络连接相关的配置

  • Http块 : 代理、缓存、日志记录、虚拟主机配置

    • http全局块
    • server块
      • Server全局块
      • location块
      • 在这里插入图片描述

Nginx具体应用

部署静态资源

​ nginx可以作为静态web服务器来部署静态资源。(html页面、css文件、js文件、图片、视频)

​ 只需要将静态资源部署到nginx的html目录中即可

反向代理

  • ​ 正向代理:是一个位于客户端和原始服务器之间的服务器,为了从哪个原始服务器取得内容,客户端向代理发送一个请求并指定目标,然后代理向原始服务器提交请求并获得内容返回给客户端。

    • 用于在防火墙内的局域网客户端提供访问Internet的途径
    • 正向代理一般是在客户端设置代理服务器,通过代理服务器转发请求,最终访问到目标服务器
  • 反向代理:代理服务器也是位于客户端与目标服务器之技安,但是对于用户而言,反向代理服务器就相当于目标服务器,用户可以直接访问反向代理服务器获取资源。

反向代理用法
前置条件

两台服务器,一台作为代理服务器,一台作为web服务器

代理服务器配置nginx web服务器部署一个应用

代理服务器:192.168.100.10

web服务器:10.2.168.193

web

在这里插入图片描述

修改 nginx.conf文件

    server {
        listen 82;
        server_name localhost;
        location / {
                proxy_pass http://10.2.168.193:8080;
        }
        }

重启nginx

测试

在这里插入图片描述

END

负载均衡

​ 将用户请求根据对应的负载均衡算法分发到应用集群中的一台服务器进行处理。避免了单点故障。

前提条件: 开启两台web服务器,一台代理服务器

编辑nginx.conf

    upstream targetserver{
        server 10.2.168.193:8080;
        server 10.2.168.193:8081;

        }
    server {
        listen 8080;
        server_name localhost;
        location / {
                proxy_pass http://targetserver;
        }
        }

重启nginx

验证

在这里插入图片描述

在这里插入图片描述

负载均衡策略

在这里插入图片描述

upstream targetserver{
    server 10.2.168.193:8080 weight=10;
    server 10.2.168.193:8081 weight=5;

    }
server {
    listen 8080;
    server_name localhost;
    location / {
            proxy_pass http://targetserver;
    }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值