1.nginx的概述

、nginx是一个开源的、支持高性能、高并发的WWW服务和代理服务软件
、是由俄罗斯人Igor Sysoev开发的,具有高并发、占用系统资源少等特性
、官网:http://nginx.org

#特点

、支持高并发:能支持几万并发连接
、资源消耗少:在3万并发连接下,开启10个nginx进程消耗的内存不到200MB
、开源做HTTP反向代理及加速缓存,即负载均衡
、具备Squid等专业缓存软件等的缓存功能
、支持异步网络I/O时间模型epoll(Linux2.+ 内核)

#扩展:异步网络和同步网络

#异步网络:将数据发送到缓冲区就返回,发送成功的消息是通过事件通知的
#同步网络:收发数据,等到数据真正发送出去或者接收到,才返回

#nginx的企业应用

、作为Web服务软件
、反向代理或负载均衡
、前端业务数据缓存服务
可通过proxy_cache模块进行缓存

#nginx的应用场景

、使用nginx运行HTML、JS、CSS、小图片等静态数据
、nginx结合FastCGI运行PHP等动态程序
、nginx结合Tomcat/Resin等支持Java动态程序

#nginx软件使用排名

#查看地址:https://w3techs.com/technologies/overview/web_server/all

2.nginx的安装

.编译安装
.rpm安装

1.rpm安装

[root@ctos2 ~]# wget -q http://mirrors.ustc.edu.cn/fedora/epel/6/x86_64/epel-release-6-8.noarch.rpm
[root@ctos2 ~]# rpm -ivh epel-release-6-8.noarch.rpm
[root@ctos2 ~]# yum install nginx -y #安装
[root@ctos2 ~]# rpm -qa nginx #查看软件是否安装
nginx-1.16.1-1.el7.x86_64

2.编译安装

[root@ctos3 ~]# yum install  gcc  pcre pcre-devel wget openssl  openssl-devel.x86_64  -y   #安装相关依赖包
[root@ctos3 ~]# useradd nginx -s /sbin/nologin -M [root@ctos3 ~]# mkdir -p /home/demo/tools/
[root@ctos3 ~]# cd /home/demo/tools/
[root@ctos3 tools]# wget http://nginx.org/download/nginx-1.16.0.tar.gz
[root@ctos3 tools]# tar xf nginx-1.16..tar.gz
[root@ctos3 tools]# cd nginx-1.16./
[root@ctos3 nginx-1.16.]# ./configure --user=nginx --group=nginx --prefix=/application/nginx --with-http_stub_status_module --with-http_ssl_module
[root@ctos3 nginx-1.16.]# make -j && make instal #安装 [root@ctos3 nginx]# /application/nginx/sbin/nginx -t #查看语法有误错误
nginx: the configuration file /application/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx/conf/nginx.conf test is successful [root@ctos3 nginx]# /application/nginx/sbin/nginx #启动服务
[root@ctos3 nginx]# ss -untpl | grep #查看服务是否启动 [root@ctos3 ~]# /application/nginx/sbin/nginx -V #安装完后查看版本
nginx version: nginx/1.16.
built by gcc 4.8. (Red Hat 4.8.-) (GCC)
built with OpenSSL 1.0.2k-fips Jan
TLS SNI support enabled
configure arguments: --user=nginx --group=nginx --prefix=/application/nginx --with-http_stub_status_module --with-http_ssl_module

#参数介绍

.yum install openssl-devel  #是为了支持SSL
.可以使用./configure --help查看相关参数的帮助
. --prefix=PATH #设置安装路径
. --user=USER #进程用户权限
. --group=GROUP #进程用户组权限
. --with-http-stub_status_module #激活状态信息
. --with-http_ssl_module #激活ssl功能
. /application/nginx/sbin/nginx -t #语法检查
. /application/nginx/sbin/nginx #启动服务
. /application/nginx/sbin/nginx -s reload #重启

#查看配置编译后的配置文件信息

[root@ctos3 ~]# tree /application/nginx/
/application/nginx/
├── client_body_temp
├── conf #配置文件目录
│   ├── fastcgi.conf
│   ├── fastcgi.conf.default
│   ├── fastcgi_params
│   ├── fastcgi_params.default
│   ├── koi-utf
│   ├── koi-win
│   ├── mime.types
│   ├── mime.types.default
│   ├── nginx.conf #主配置文件
│   ├── nginx.conf.default
│   ├── scgi_params
│   ├── scgi_params.default
│   ├── uwsgi_params
│   ├── uwsgi_params.default
│   └── win-utf
├── fastcgi_temp
├── html
│   ├── 50x.html
│   └── index.html
├── logs
│   ├── access.log
│   ├── error.log
│   └── nginx.pid
├── proxy_temp
├── sbin
│   └── nginx
├── scgi_temp
└── uwsgi_temp

#提示:

#1.temp结尾的文件是临时文件

#2.default结尾的文件是备份文件

3.nginx的常用模块

4.nginx的虚拟主机

、虚拟主机就是一个独立的站点,这个站点对应独立的域名、或者使IP或端口,也有独立的程序和资源目录
、由一定的格式标签段标记,Apache使用<VirtualHost></VirtualHost>,nginx使用server{} 来标签一个虚拟主机,也支持多个虚拟主机
、虚拟主机的官网配置文档:http://Nginx.org/en/docs/http/request_processing.html

#虚拟主机的类型

.基于域名的虚拟主机
.基于端口的虚拟主机
.基于IP的虚拟主机

#配置不同类型的虚拟主机

#1.配置基于域名的虚拟主机

[root@ctos3 ~]# cd /application/nginx/conf/
[root@ctos3 conf]# grep -Ev '^$|#' nginx.conf.default > nginx.conf
[root@ctos3 conf]# cat nginx.conf
http {
server {
listen ;
server_name www.guoke.com;
location / {
root html;
index index.html index.htm;
}
}
server {
listen ;
server_name bbs.guoke.com;
location / {
root html/bbs;
index index.html index.htm;
} }
}

#2.配置基于端口的虚拟主机

只需将端口改成不同的就可以了

#3.配置基于IP的虚拟主机

本地有多个IP,然后listen监听改成是地址,server_name也相应的修改一下

#总结配置虚拟主机的步骤

、增加一个完整的server标签段,要放再http里面
、更改server_name及root根目 录
、创建index.html文件
、检查语法然后重启服务
、访问

5.nginx的反向代理

反向代理:接收用户请求代替用户去后端访问

#反向代理的重要参数

#例子:为10.1.1.1做域名www.guoke.com的代理,当访问www.guoke.com就会访问到10.1.1.1服务器上,也可以写upstream服务器池

Server {
Listen ;
Server_name www.guoke.com;
Location / {
Proxy_pass http://10.1.1.1;
Proxy_set_header Host $host;
}
}

6.nginx的负载均衡

可以对用户的访问请求进行调度处理,对用户的访问请求进行压力分担

#关键参数upstream

#upstream的相关参数说明

server 10.10.1.1: weight= max_fails= fail_timeout= backup;

#配置例子:为www.guoke.com域名做负载均衡调度

http  {
upstream server{
server 192.168.226.146:;
server 192.168.226.147:;
}
server {
listen ;
server_name www.guoke.com; #access_log logs/host.access.log main; location / {
root html;
index index.html index.htm;
proxy_pass http://server;
}
}

#附加负载均衡有关的面试题

nginx有哪几种调度算法,这几种区别是什么
常用的有3种调度算法(轮询,ip hash,权重) 轮询是默认的,每个请求按时间顺序逐一分配都不同的后端服务,如果后端某台服务器死机就会自动剔除故障系统,让用户访问不受影响 权重:权重的值越大,访问的概率就越高      iphash:请求按访问的IP的哈希结果分配,使来自同一个IP的访客固定访问一台后端服务器,可以解决会话问题

7.nginx的其他相关功能

7.1.别名

别名就是为虚拟主机设置除了主域名以外的一个或多个域名名字

配置方法
在原有的域名上添加server_name www.baidu.com baidu.com 应用场景
多数企业网站希望访问www.baidu.com和baidu.com,所浏览的事一个页面

7.2.状态信息功能

Nginx status介绍
模块为ngx_http_stub_status_module,主要是记录nginx的基本访问状态信息,如果想要添加,在编译的时候就加入http_stub_status_module
配置:在location / {
stub_status on
}

7.3.错误日志

 #常见的日志级别:[debug|info|notice|warn|error|crit|alert|emerg],级别越高,记录的信息越少

#error_log的默认值为
#default: error_log logs/error.log error;
#参考资料:http://nginx.org/en/docs/ngx_core_module.html#error_log #配置
error_log logs/error.log;

7.4.访问日志

#nginx软件会把用户访问网站的日志信息记录到指定的日志文件里,给网站提供者参考
#官网地址:http://nginx.org/en/docs/http/ngx_http_log_module.html
#默认参数配置
#access_log logs/access.log main;

7.5.日志轮询切割

默认情况下nginx会把所有的访问日志生成到一个指定日志文件access.log中,但是如果时间长了日志文件会很大,不利于分析和处理,所以又必要对日志按天或按小时进行切割

#切割脚本
[root@ctos3 script]# pwd
/script
[root@ctos3 script]# cat cut_ng_log.sh
#!/bin/bash Dateformat=`date +%Y%m%d` #定义时间格式
Basedir="application/nginx" #目录名
Nginxlogdir="$Basedir/logs" #日志目录
Logname="access_www" #日志的名字 [ -d $Nginxlogdir ] && cd $Nginxlogdir
exit
[ -f ${Logname}.log ]
exit
/bin/mv ${Logname}.log ${Dateformat}_${Logname}.log #放访问的日志更名,加上时间
$Basedir/sbin/nginx -s reload #重启服务 #然后将脚本放进定时任务里面,每天的00:00执行
[root@ctos3 ~]# cat /etc/crontab
* * * /bin/sh /script/cut_ng_log.sh > /dev/null

7.6.location

location指令的作用是根据用户请求的URI来执行不同的应用
语法:location [=|~|~*|^~] uri{...}

7.7.rewrite

Nginx rewrite的主要功能是实现URL地址重写
指令语法:rewrite regex replacement[flag];
#例子:
server {
listen ;
server_name guoke.com;
rewrite ^/ (*.)http://www.guoke.com/$1 permanent;
#参数介绍
  rewrite为固定关键字
  regex匹配正则表达式
  $1:取前面regex部分括号里的内容
  permanent:301永久跳转

7.8.访问认证

通常我们会为网站设置一些访问认证,设置需要用户认证访问的,一般主要应用在企业内部人员的访问地址上,例如企业网站后台

#例子:

#配置基本用户认证
[root@ctos3 conf]# cat nginx.conf
server {
listen ;
server_name localhost;
location / {
root html;
index index.html index.htm;
auth_basic "guoke";
auth_basic_user_file /application/nginx/conf/htpasswd; }
} #提示:默认没有htpasswd这个命令,需要安装httpd才有
[root@ctos3 conf]# yum install httpd -y
[root@ctos3 conf]# which htpasswd
/usr/bin/htpasswd #创建用户和密码
[root@ctos3 conf]# htpasswd -bc /application/nginx/conf/htpasswd guoke guoke123
Adding password for user guoke [root@ctos3 conf]# chmod /application/nginx/conf/htpasswd #修改权限
[root@ctos3 conf]# chown nginx /application/nginx/conf/htpasswd [root@ctos3 conf]# /application/nginx/sbin/nginx -t #检查语法
nginx: the configuration file /application/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx/conf/nginx.conf test is successful

#访问效果

#参数讲解:

auth_basic:设置认证提示字符串
auth_basic_user_file:用于设置认证的密码文件

盘点Linux运维常用工具(二)-web篇之nginx的更多相关文章

  1. 盘点Linux运维常用工具(一)-web篇之httpd

    #前言:想把自己学的各种服务进行分类归档起来,于是就写了盘点Linux运维常用工具,Linux方面使用到的web应用服务有httpd(apache).nginx.tomcat.lighttpd,先了解 ...

  2. linux 运维常用工具表

    https://code.google.com/p/httperf/  ※测量Web服务器的性能 ./configure   make &&make install http://ww ...

  3. Linux运维常用150个命令

    Linux运维常用150个命令 转载自:www.cnblogs.com/bananaaa/p/7774467.html 命令 功能说明 线上查询及帮助命令(2个) man 查看命令帮助,命令的词典,更 ...

  4. Linux运维常用的几个命令介绍【转】

    Linux运维常用的几个命令介绍 1. 查看系统内核版本​ [root@funsion geekxa]# cat /etc/issue CentOS release 6.5 (Final) Kerne ...

  5. Linux运维-常用操作-培训用例

    一.服务器环境 Centos 7.9 二.常用连接工具(免费) 1.Finalshell 2.MobaXterm 3.Putty + WinSCP 三.Linux  系统目录结构 /bin :是 Bi ...

  6. Linux运维之shell脚本进阶篇

    一.if语句的使用 1)语法规则 if [条件] then 指令 fi 或 if [条件];then 指令 fi 提示:分号相当于命令换行,上面两种语法等同特殊写法:if[ -f"$file ...

  7. Unix/Linux运维首选工具Xmanager Enterprise 3.0的使用教程

    管理Uinx和Linux服务器的兄弟们应该很熟悉Xmanager,一个窗口可以同时控制上百台Linux和Unix服务器,功能非常强大!^_^请看: manager是一个简单易用的高性能的运行在Wind ...

  8. 13 款高逼格且实用的 Linux 运维必备工具

    转载于民工哥技术之路 1. 查看进程占用带宽情况 - Nethogs Nethogs 是一个终端下的网络流量监控工具可以直观的显示每个进程占用的带宽. 下载:http://sourceforge.ne ...

  9. Linux运维常用命令详解

    1.ls 文件属性:  -:普通文件  d:目录文件  b:块设备  c:字符设备文件  l:符号连接文件  p:命令管道  s:套接字文件  文件权限: 9位数字,每3位一组  文件硬链接次数  文 ...

随机推荐

  1. 关于Pycharm安装扩展包的方法

    Python中第三方的库(library).模块(module),包(package)的安装方法以及ImportError: No module named 1.pip install .... 一般 ...

  2. JarvisOJ level3_x64

    这一题是和前面x86的差不多,都是利用了同一个知识点,唯一的区别就是使用的堆栈地址不同,x86是直接使用堆栈来传递参数的,而x64不同 x64的函数调用时整数和指针参数按照从左到右的顺序依次保存在寄存 ...

  3. Python sorted函数详解(高级篇)

    sorted() 函数对所有可迭代的对象进行排序操作. sort 与 sorted 区别: sort 是应用在 list 上的方法,sorted 可以对所有可迭代的对象进行排序操作. list 的 s ...

  4. Ionic3 Demo

    本文为原创文章,转载请标明出处 最近又开源了一个小 Demo,基于 Ionic 3.9.2.注册登录功能使用的是 WildDog 野狗通信云,大家可以放心的注册登录玩.电影相关数据来源自"某 ...

  5. Python实现线程交替打印字符串

    import threading con = threading.Condition() word = u"12345上山打老虎" def work(): global word ...

  6. SQL语言基础及数据库的创建

    一.数据类型:1.二进制数据二进制数据以十六进制形式存储.二进制数据最多能存8000个英文字符,4000个汉字字符. 2.字符数据char:存100,不足100补足.varcha:存多少占多少. 3. ...

  7. Apache JMeter--1基础介绍

    一.JMeter 介绍 近期公司要做jmeter调研,将性能测试推广到全部测试组,便一边学习一边做记录. Apache JMeter是100%纯JAVA桌面应用程序,是一款优秀的开源性能测试工具,被设 ...

  8. 你相信吗:新药可以让X战警变成现实

           不管男人还是女人.大人还是小孩,心目中都有一个超级英雄梦,梦想着有一天能够具有超级英雄的能力.直到今天,你相信吗?现在医学工作者已经发现通过一种新药可以让人拥有X战警里一些超级英雄的能力 ...

  9. windowserver 2012安装openssh

    下载https://github.com/PowerShell/Win32-OpenSSH/releases解压放到C:\Program Files\OpenSSH-Win64 进入到C:\Progr ...

  10. redis笔记之一

    NoSQL简介 全称是Not Only SQL,泛指菲关系型数据库,它是通过键值对存储数据并且将数据存储在内存中.而像mysql,sql server这些通过关系表存数据的就叫关系型数据库 为什么需要 ...