nginx(engine x)

 nginx是什么

web服务器软件

       web软件(支持静态 http server)  负载均衡load balance解决多量并发时网站处理能力(中间件 reverse proxy反向代理服务器(有缓存cache)站在企业方)跟redisx像  用户先在proxy访问 有就直接用 没有就去web找到以后存到proxy中 下次用户直接在此用

performance 高性能 HTTP(超文本协议)

配置文件作用:传参

安装nginx:yum (配置文件所在的目录/etc/nginx/叫nginx.conf)和编译安装

[root@web-1 yum.repos.d]# yum install nginx  -y

启动nginx

[root@web-1 yum.repos.d]# systemctl  start nginx

查看进程

[root@web-1 yum.repos.d]# ps aux|grep nginx

cd  /usr/share/nginx/html/是nginx存放网站的目录

vim  index.html

[root@web-1 html]# cat luo.html

<html>

<head>

       <meta charset="UTF-8">

       <title>welcome to  luo's website</title>

</head>

<body>

       <p>欢迎来的罗的网站</p>

       <img src=luo.jpg width="400">

</body>

</html>

总的

       cat index.html

<!DOCTYPE html>

<html>

<head>

 <meta charset="UTF-8">

<title>Welcome to sanchuang</title>

<style>

html { color-scheme: light dark; }

body { width: 35em; margin: 0 auto;

font-family: Tahoma, Verdana, Arial, sans-serif; }

</style>

</head>

<body>

<h1>Welcome to nginx!</h1>

<p>If you see this page, the nginx web server is successfully installed and

working. Further configuration is required.</p>

<p>欢迎来到长沙</p>

<img src=feng.jpg width="400">

<p>For online documentation and support please refer to

<a href="http://nginx.org/">nginx.org</a>.<br/>

Commercial support is available at

<a href="http://nginx.com/">nginx.com</a>.</p>

<a href="luo.html">luobiao</a>

<a href="https://www.sanchuangedu.cn">sanchuang</a>

<p><em>Thank you for using nginx.</em></p>

</body>

</html>

编译安装nginx

kill -9 加进程号Pid

killall -9 进程名字

新安装一台web服务器,web-2

在ansible里添加进入主机清单

[root@ansible-prome ansible]# vim hosts

[web2]

192.168.205.140  ansible_ssh_user='root' ansible_ssh_pass='123456'

ssh  root@192.168.205.140  exit

ansible web2  -m script  -a "/etc/ansible/init_env_1.sh web-2 192.168.159140  192.168.159.2"

验证:使用ansible去执行初始化的操作不是特别方便,主要是脚本里有reboot命令,会重启服务器

使用ansible去安装node_exporter

将web-1服务器上的安装脚本和源码包文件,scp到ansible机器上

[root@web-1 ~]# scp install_node_exporter.sh root@192.168.205.134:/etc/ansible

scp node_exporter-1.4.0-rc.0.linux-amd64.tar.gz root@192.168.205.134:/etc/ansible

ansible web2 -m copy -a "src=node_exporter-1.4.0-rc.0.linux-amd64.tar.gz  dest=/root"

ansible web2 -m script -a "install_node_exporter.sh"

 在Prometheus server上添加 web2机器信息

[root@ansible-prome prometheus]# vim prometheus.yml

- job_name: "web-2"

    static_configs:

      - targets: ["192.168.205.135:9090"]

然后到网站访问

安装前的准备:

先停止yum安装的nginx

[root@web-1 nginx]# systemctl stop nginx

configure 是一个nginx官方提供的一个进行编译配置的脚本,主要目的是收集用户的指定配置,收集参数。

例如: 用户可以指定安装位置,启用哪些模块(功能),不启用哪些模块(功能)

[root@web-1 nginx]# cat install_nginx.sh 
set -e
#!/bin/bash
#创建存放nginx源码的目录
mkdir  -p  /nginx
#进入目录,开始下载nginx源码包
cd  /nginx
curl -O https://nginx.org/download/nginx-1.26.1.tar.gz
#解压源码包
tar  xf  nginx-1.26.1.tar.gz 
cd  nginx-1.26.1
#解决依赖的软件包
yum install  gcc   pcre-devel  openssl-devel -y
#配置工作
./configure  --prefix=/usr/local/nginx-1  --user=limingzhe  --group=limingzhe   --with-http_ssl_module  --with-http_v2_module  --with-threads   --with-stream   --with-http_stub_status_module
#启动2个进程去编译
make  -j  2
#安装编译,将前面编译好的二进制文件和配置文件复制到/usr/local/nginx-1目录下
make  install
#新建用户limingzhe
useradd  limingzhe  -s  /sbin/nologin
#修改PATH环境变量,增加nginx的安装目录,方便找到安装的nginx
echo &apos;PATH=/usr/local/nginx-1/sbin/:$PATH&apos;  >>/etc/profile
source  /etc/profile
#启动nginx
/usr/local/nginx-1/sbin/nginx
#firewalld and selinux
#stop firewall和设置下次开机不启动firewalld
service firewalld stop
systemctl disable firewalld
#临时停止selinux和永久停止selinux
setenforce 0
sed  -i &apos;/^SELINUX=/ s/enforcing/disabled/&apos; /etc/sysconfig/selinux 
 

最后还要先  ./nginx

nginx  -s stop  停止nginx服务

nginx  -s reload 重启nginx服务

编译安装的好处?

  1. 可以获得指定的版本
  2. 可以定制功能 --》巨大的优势了 --》可以根据自己公司的业务特点定制功能

阿里云、腾讯基于nginx进行了二次开发,搞出了自己的nginx服务器,阿里搞出来 tnginx(taobao nginx)

nginx主配置文件讲解

cd /usr/local/nginx-1/conf

cat nginx.conf

#user  nobody;  #nginx woker进程启动的时候会使用那个用户去启动

worker_processes  2;   指定2个woker进程,一般情况下与cpu核心的个数一致

#error_log  logs/error.log;   #错误日志

#pid        logs/nginx.pid;   #master进程的pid号

events {

    worker_connections  2048;  #定义一个worker进程可以同时并发连接多少个请求     总的请求数=woker进程数*2048

}

#  配套修改一个内核对一个进程允许打开的文件数 为100000  

#ulimit -n 100000

#log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '

    #                  '$status $body_bytes_sent "$http_referer" '

#                  '"$http_user_agent" "$http_x_forwarded_for"';

上面三行打开注释

  #一个server对应nginx进程提供的网站服务,一个server对应一个网站  --》虚拟主机 对应一个网站

    server {

        listen       80;  监听的端口

        server_name  www.scweb.com;   网站的域名

#access_log  logs/host.access.log  main;   #访问日志

        #定义路由--》根路径

        location / {

            root   html;   #网页根目录 在html文件夹   在/usr/local/nginx-1/html在nginx安装的路径下

            index  index.html index.htm;    #定义网站的首页

        }

Master进程是管理进程 --》父进程

Worker 进程是工作进程 --》子进程

编译安装的nginx启动nginx

[root@web1 logs]# nginx   #启动

[root@web1 ~]# nginx  -s reload  #重启

[root@web1 ~]# nginx  -s stop  #关闭

[root@web1 ~]# nginx  -t   #测试nginx.conf文件是否有语法错误

yum安装的nginx

  • 在 CentOS/RHEL 系统中,你可以使用 systemd 服务管理器来启动 Nginx:
systemctl start nginx
systemctl stop nginx
systemctl restart nginx
systemctl status nginx查看 Nginx 的运行状态:

完整的可以使用的nginx.conf配置文件

[root@web1 conf]# cat nginx.conf|egrep -v "^$|^#| +#"  什么意思

yum  install  nginx

              配置配置/etc/nginx

              可执行的nginx二进制程序 /usr/sbin/nginx

              网站目录: /usr/share/nginx/html

编译安装的nginx

              配置文件:  /usr/local/nginx-1/conf

              可执行的nginx二进制程序  /usr/local/nginx-1/sbin

              网站目录:  /usr/local/nginx-1/html

如何知道自己的cpu的信息?

       1.使用top  --》1   知道cpu有多少个核心

       2.cat  /proc/cpuinfo

       3.lscpu

因为我们是编译安装的nginx,默认安装的位置不在PATH变量定义的文件夹里,所以我们需要去修改PATH变量,让linux系统能找到执行程序nginx这个二进制文件

yum安装的软件,默认会把执行的文件存放到PATH变量的目录下,一般在/usr/bin 或者/usr/sbin

临时修改PATH变量

PATH=/usr/local/nginx-1/sbin:$PATH

永久修改PATH变量

echo  'PATH=/usr/local/nginx-1/sbin:$PATH'    >>/etc/profile

在当前终端里执行脚本/etc/profile 加载PATH环境变量

source   /etc/profile(全局环境变量配置文件。每当用户登录系统时,这个脚本会被自动执行,用于设置一系列的环境变量,修改 /etc/profile 后,新的设置不会立即在已打开的 shell 会话中生效,除非你重新登录或手动使用 source 命令来重新加载这个文件。)

nginx开机启动的问题

1./etc/rc.local文件,linux在启动的最后阶段会执行这个脚本里的命令 用于在系统启动完成前运行一些脚本或命令

       /usr/local/nginx-1/sbin/nginx

       [root@web-2 nginx-1]# vim /etc/rc.local

       #开启自动启动编译安装的nginx

       /usr/local/nginx-1/sbin/nginx

[root@web-2 nginx-1]# chmod  +x /etc/rc.d/rc.local

[root@web-2 system]# systemctl daemon-reload(当修改了服务的配置文件(通常是 .service .target 文件 执行这句话让让 Systemd 重新解析和只加载配置 再使用 systemctl startsystemctl restart systemctl reload 等命令来启动或重新启动服务,以使新的配置生效。 )

[root@web-2 system]# systemctl stop nginx

[root@web-2 system]# ps aux|grep nginx

2.创建一个 nginx.service文件,让systemctl进行管理

nginx.conf配置文件

[root@web-2 ~]# cd /usr/lib/systemd/system

[root@web-2 system]# vim nginx.service

[Unit]

Description=nginx - high performance web server

[Service]

Type=forking

PIDFile=/usr/local/nginx-1/logs/nginx.pid

ExecStart=/usr/local/nginx-1/sbin/nginx -c /usr/local/nginx-1/conf/nginx.conf

ExecReload=/bin/sh -c "/bin/kill -s HUP $(/bin/cat $PIDFile)"

ExecStop=/bin/sh -c "/bin/kill -s TERM $(/bin/cat $PIDFile)"

[Install]

WantedBy=multi-user.target

cd nginx-1/html vim index.html 修改网站首页

修改windows机器里的hosts文件,添加域名解析记录对应的ip和域名

C:\Windows\System32\drivers\etc\hosts

192.168.159.140   www.scweb.com

在浏览器里输入Parked Page for: scweb.com

使用浏览器去访问

[root@web-2 html]# curl  192.168.205.135

curl  www.scweb.com

故障排除

方法:

  1. 查看错误日志
    1. 如果虚拟主机没有配置单独的错误日志,可以看nginx总的错误日志 error.log
    2. 可以单独配置错误日志
  1. 看报错信息 tail -f feng.com.error.log

隐藏nginx的版本

http {  在这里面

    include       mime.types;

    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '

                      '$status $body_bytes_sent "$http_referer" '

                      '"$http_user_agent" "$http_x_forwarded_for"';

    server_tokens off;加一句

然后重启

如何知道nginx所在的web服务器的负载(web主机的负载)?Cpu、内存、磁盘IO、网络带宽

如何知道nginx目前有多少人访问(nginx的负载)?不同的虚拟主机,每个虚拟主机有多少人访问?整个nginx进程由多少人访问?

为什么要开启nginx状态统计功能?

答: 因为运维人员需要了解nginx服务是否忙还是不忙(负载情况)

如何知道一台安装了nginx的web服务器是否繁忙?

方法:

  1. 看服务器负载   (cpu、内存、磁盘IO、网络带宽)
    1. top命令
    2. glances

[root@web2 nginx]# yum  install  glances -y

[root@web2 nginx]# glances

  1. 看nginx的基本的状态统计
  2. 在web1上查看已经安装的nginx是否支持状态统计功能

    [root@web1 ~]# nginx  -V

    nginx version: nginx/1.24.0

    configure arguments: --prefix=/usr/local/wangguang --user=wangguang --group=wangguang --with-http_ssl_module --with-threads --with-http_v2_module --with-http_stub_status_module --with-stream --with-http_gunzip_module表示我们在编译安装的时候打开过状态统计功能  必须写进去才能看到效果

    [root@web1 conf]# vim nginx.conf

    #www.sc.com这个网站的配置

          server {

            listen       80;

            server_name  www.sc.com;

            access_log  logs/www.access.log  main;

            error_log  logs/www.error.log;

         

            #基本的状态统计功能

            location = /sc_status {

                    stub_status;

            }

    location /download {提供下载服务功能

     autoindex  on;}

  3. 提供以下状态信息:

    Active connections: 1  #最重要的参数,可以看出nginx服务器是否繁忙

    Active connections当前活动的客户端连接数,包括Waiting连接数。

    Accepts  接受的客户端连接总数

    handled已处理的连接总数。通常,参数值与accepts 除非达到某些资源限制(例如, worker_connections限制)相同。

    Requests   客户端请求总数。65秒内连接是建立的 不会断开  keepalive_timeout 65;(tcp三次握手建立连接后 避免频繁建立连接)  请求可以在原来已有的连接基础上进行,不需要再次连接

    Reading  nginx正在读取请求报文的当前连接数。 --》处理请求报文

    Writing   nginx正在将响应写回到客户端的当前连接数。  --》处理响应报文

    Waiting  当前等待请求的空闲客户端连接数  --》占着茅坑不拉屎的人的数量--》已经建立连接的人,nginx服务器等待它们再次发起请求

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值