理论部分:

cd /opt
mkdir app adwnload logs work backup
app : 代码目录
download : 网上下载的源码包等
logs : 自定义日志
work : shell 脚本
backup : 默认配置文件的备份

关闭 iptables规则

iptables -L : 查看是否有
iptables -F : 关闭iptables 规则

停用selinux

通过 getenforce : 查看是否停用
setenforce 0 : 关闭

Nginx命令

如果出现 “ Failed to start nginx.service: Access denied ”,那么有一下几种可能

端口被占用,那就很简单了,清理占用端口的进程或者修改Nginx监视端口
使用 sudo nginx -c nginx.conf,再 sudo nginx -s reload 先指定一下配置文件
sudo find nginx.conf,查找一下配置文件。像我这里就出现了有两个nginx的情况,一个是 /usr/local/nginx/sbin/nginx(我应该使用的),另一个是/etc/nginx 里面的nginx(我以为应该使用的)找到对应nginx文件,再 -c 其对应的 conf文件

Nginx是一个开源且高性能,可靠的http中间件,代理服务。
中间件:可以直接调用操作系统,也可以调用应用。使网站更有层次性,便于开发维护。中间件可以获取web请求,并把请求分发给操作系统或者应用,也可以分发给其他中间件,也可以通过应用发给其他中间件。

Nginx:开源且高性能,可靠的http中间件,代理服务意味着:

源代码开放,可以通过官网获取源代码,进行二次开发。淘宝就是基于Nginx1.6开发的。
高性能,支持海量并发请求的web server
服务稳定,不会因为本身代码原因导致问题。

常见其他的HTTP服务:

HTTPD-Apache基金会
IIS-微软
GWS-Google

为什么选择Nginx:

IO多路复用epoll

IO复用:解决的是并发性的问题。大家都在访问某个网站,对于后台而
言就有多个请求,对于中间件就产生多个IO流。
对于IO流,操作系统中有并行处理,和串行处理。可以用多线程的方式
处理,也有io多路复用。多线程处理会造成资源损失,因为
管理多个线程本身就消耗资源。io多路复用就是在一个线程里面交替并
发的完成。复用指的是重复使用同一个线程。
IO多路复用的实现方式select, poll, epoll。

轻量级:功能模块少,代码模块化。

CPU亲和。减少额外的性能损耗。
是一种把CPU核心和Nginx工作进程保定方式,把每个worker进程固定
在一个CPU上执行,减少CPU开支。

sendfile工作机制。

Nginx快速搭建:

Mainline version    开发版
Stable version    稳定版
Legacy version    历史版本

vim /etc/yum.repos.d/nginx.repo
复制一下代码:

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/OS/OSRELEASE/$basearch/
gpgcheck=0
enabled=1

其中将 OS 改成你的系统 这里我用的是centos   OSRELEASE改成你的操作系统版本 我用的是centos7 所以 这里改成 7
所以这里改成:
baseurl=http://nginx.org/packages/centos/7/$basearch/

yum list |grep nginx   查看yum源中nginx版本

yun install nginx    安装nginx

通过nginx -v 查看版本

通过yum安装,其实是安装一个一个rpm包

sudo /usr/local/nginx/sbin/nginx -s reload

rpm -ql nginx  查看已经安装的服务所配置的文件以及对应的目录

查看输出,在 etc , usr , var 三个目录下都有安装

Linux 操作系统下 etc 目录安装的是系统的核心配置文件,同理Nginx中也适用。
以下只要没写的框框,就是同上:

在nginx中的nginx.conf文件:cd /etc/nginx/ 之后 vim nginx.conf

http {
    include       /etc/nginx/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"';//“log_format”定义日志类型

access_log  /var/log/nginx/access.log  main;

sendfile        on;
    #tcp_nopush     on;

keepalive_timeout  65;        #设置客户端服务端访问超时时间,默认65s

#gzip  on;

include /etc/nginx/conf.d/*.conf;
}

查看default.conf,cd /etc/nginx/conf.d    之后    vim default.conf

listen        代表监听的端口,这里默认80端口

server-name    主机名(可以替换)

location        一个server中可以有多个location,当没有其他的访问路径时默认是“/” root是存放路径,index 是html文件

error_page    错误类型,以及其对应的错误网站

修改完配置文件之后需要重启nginx

server{
    listen 80; # 监听端口
    server_name localhost; # 服务名字/域名访问地址 baidu.com

#charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

location / { # 服务名字 与其 对应的访问路径,如果是 ‘/’代表所有访问都通过下面的路径
        root   /usr/share/nginx/html; # 根 路径
        index  index.html index.htm; # 默认访问的页面,如果没有index.html则访问index.htm
    }

#error_page  404              /404.html;

# redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html; # 错误号与访问错误对应的的文件,可以手动添加
    location = /50x.html { 
        root   /usr/share/nginx/html; # 错误对应的文件的位置
    }

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}   
}

日志

response

request - 包括请求行,请求头部,请求数据
response - 包括状态行,消息包头,响应正文

这个时候 安装一个curl,用于测试
curl 我们的网站    会返回一个html代码    这个就是我们发出request请求后,服务端response给我们的响应包里的报文
curl -v 网站 >/dev/null    返回的值重定向到linux的一个空设备中,这样就可以看到 请求行,请求方法等等等等

“>”是request。“<”是response

> GET / HTTP/1.1             #请求方式位GET方式
> User-Agent: curl/7.35.0.   #请求头是curl
> Host: www.csdn.net         #访问的网址是csdn
> Accept: */*

< HTTP/1.1 200 OK             #200 是正常访问之后返回的值
* Server openresty is not blacklisted
< Server: openresty
< Date: Tue, 04 Sep 2018 05:32:19 GMT
< Content-Type: text/html; charset=UTF-8
< Transfer-Encoding: chunked
< Connection: keep-alive
< Keep-Alive: timeout=20

Nginx的日志类型

包括:error.log(处理http请求的错误的状态以及nginx本身运行错误的状态,根据级别不同分别记录)
access_log(记录Nginx每次hhtp请求的访问状态,用于分析客户请求,或者对客户行文进行分析)。
log_format(实现分析用户交互,请求之类的)

nginx的log中记录了很多的信息,这些信息可以被认为是nginx中的变量,
log_format就是将这些变量组织到一起,记录到access.log中。
log_format 只能配置到http模块下。
配置语法大概如下:

Syntax: log_format name [escape=default|json] string ...;
Default:log_format combined "...";
Context:http。#必有
在 /etc/nginx/nginx.conf文件中看到 error_log ,后面的文件位置就是配置错误日志的地方
warn表示warning,是一个级别
在/etc/nginx/nginx.conf文件中看到access.log,后面表示access.log的路径
main表示nginx的log_format的main

Nginx变量

编辑/etc/nginx/nginx.conf 文件,使其能够输出请求头

htt{
    include /etc/nginx/mime.types;
    default_type application/octet-stream;
    log_format main '$http_user_agent' '$remote_addr - $remote_user [$time_local] "$request" '
                    '$status $body_bytes_send "$http_referer" '
                    '"$http_user_agent" "$http_x_forwarded_for"';
    access_log /var/log/nginx/access.log main;
}

这里 在 log_format中添加了 ‘$http_user_agent’,表示我们要查看的是 请求中的 User-Agent
对于其他参数,就是大写换成小写,‘-’换成‘_’。
这里User-Agent 换成 user_agent
使用nginx -t -c /etc/nginx/nginx.conf 检查文件是否书写正确

-t:检查
-c:文件路径

之后使用 nginx -s reload -c /etc/nginx/nginx.conf 重新加载配置
使用curl访问一下本机 localhost
通过查看/var/log/nginx/access.log文件可以查看到刚刚添加的请求头参数已经输出了
curl/7.29.0127.0.0.1 - - [21/Apr/2019:18:16:19 +0800] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
# 在access.log文件中查看到的信息

Nginx内置变量(太多了,直接丢官网上的)

http://nginx.org/en/docs/http/ngx_http_log_module.html#access_log

再看回nginx.conf文件中的log_format

每个内容最外层都有一个单引号,进行多个变量的囊括
变量之间的 ’ - ’ 作为分隔符
remote_addr: 表示客户端地址
remote_user: 客户端请求nginx认证的用户名
time_local: nginx的时间
request :request头的请求行(请求方式,http协议等)
status: response返回的状态
body_bytes_sent:服务端相应的body信息的大小
http_referer:上一级url地址,即从哪个页面跳转过来的
http_user_agent:http头信息
http_x_forwarded_for:记录每一级用户通过http请求里面的对应的http信息

Nginx官方模块

命令 nginx -V 查看nignx编译的模块

–with:Nginx开启的模块和编译进去的模块

http_stub_status_module

用于展示Nginx当前处理连接的状态
用于监控Nginx当前的连接的信息
需要再server或者location下配置

配置语法:

Syntax: stub_status; # 写法
Default:--    # 默认值
Context:server, location # 应该配置在什么地方

配置:

location /mystauts{ 
    stub_status;
}
访问 localhost/mystauts 得到链接的状态数

Active connections: 1  #Nginx 当前活跃的状态数
server accepts handled requests 
 2 2 3  # Nginx 接受握手的总次数 处理的连接数 总的请求数
# 一般 握手数 与 连接数相同,表示没有丢失
Reading: 0 Writing: 1 Waiting: 0
# 上面三个表示当前的状态
# 第一个 正在读的个数 第二个 正在往Nginx写的个数 第三个等待个数

http_random_index_module 模块

  • 目录中随机选择一个文件作为主页

  • 很少用,但是也是有用的,比如随机生成不同主题的主页给用户不同体验

  • 配置语法

Syntax:random_index on|off; # 有 on 和 off可供选择
Default:random_index off; # 默认关闭
Context:location # 配置在location中

  • 这里在 /opt/code 文件夹中创建 1.html ,.html, 3.html三个文件
  • 实战经历

location / {
    root   /opt/code;
    #index  index.html index.htm;
    random_index on;
}
ngnix -tc nginx.conf 和 nginx -s reload 一下

http_sub_module 模块

Nginx服务端在给客户端response的http内容的时候,对http的内容进行替换

用的也比较少,常用语法:

Syntax : sub_filter string replacement;
# string 表示要替换的内容
# replacement 替换后的对象,替换后的内容
Default : --
Context:http, server, location

Synatx : sub_filter_last_modified on|off;
# Nginx的服务端完成与客户端(浏览器),每次请求校验服务端内容是否有所更新
# 一般记录一串时间格式
Default : sub_filer_last_modified off;
Context : http, server, location;

Synatx : sub_filter_once on|off;
# 匹配所有html中的第一个,还是匹配所有指定的字符串
Default : sub_filter_once on;
# on 只修改匹配道德第一个 html内容
# off 把所有html指定内容都进行匹配
Context : htpp,server, locaiton
实战经历,创建测试html

<html>
    <head>
        <meta charset="UTF-8">
        <title>test</title>
    </head>
    <body>
        <a>qwer</a>
        <a>1234</a>
        <a>5678</a>
        <a>qwer</a>
    </body>
</html>

location /{
    root /opt/code;
    index index.html index.tm;
    sub_filter '<a>qwer</a>' '<a>asdf</a>';
    # 更新 <a>qwer</a> 为 <a>asdf</a>
}

  • -tc 和 -s 一下,可能需要手动刷新浏览器,删除本地缓存
  • 再次访问这个index.html 中,第一个qwer 变成了 asdf
  • 如果想要所有qwer都变呢

location /{
    root /opt/code;
    index index.html index.tm;
    sub_filter '<a>qwer</a>' '<a>asdf</a>';
    sub_filter_once off; # 让其全局替换
    # 更新 <a>qwer</a> 为 <a>asdf</a>
}

Nginx的请求限制

  • 连接请求限制

    • limit_conn_module
  • 请求频率限制
    -limit_req_module

  • Http请求建立在一次TCP链接基础上

  • 一次TCP请求至少产生一次Http请求

配置语法:

limit_conn_zone:

Syntax : limit_conn_zone key zone=name:size;
# 如果要限制连接,就需要存储各个连接的状态,因此需要开辟空间存储
# limit_conn_zone 就是开辟的空间
# key 为 限制的变量,例如ip等
# zone 为 name为申请空间名字,为下面的limit_conn中的zone做准备
# size 为开辟空间的大小
Default: --
Context : http

limit_conn:

Syntax : limit_conn zone number;
# zone 为上述的 开辟空间的 zone 的 name
# number 限制同一时间最多有多少个连接数
Dafault : --
Context : http,server, location;

limit_req_zone:

Syntax : limit_req_zone key zone=name:size rate=rate;
# 这里name,size同上,是为了后面做准备
# rate 速率,表示请求限制是多大,一般以秒为单位。表示每秒限制多少个
Default:--
Context:http

limit_req:

Syntax : limit_req zone=name [burst=number] [nodelay]
# zone 为上述的 zone 的 name
# 后面两个 [] 中的参数默认不需要配置

Default: --
Context:http,server,location

  • 书写nginx.conf 文件
  • 只写了与限制有关的

http{
    limit_conn_zone $binary_remote_addr zone=conn_zone:1m;
    # 这样的配配置,表明以ip为key,来限制每个ip访问的时候,最多只能有一个在线,否则其余的都要返回不可用。
    limit_req_zone $binary_remote_addr zone=req_zone:1m rate=1r/s;
    # binary_remote_addr 是访问的客户端ip地址
    # remote_addr也是客户端地址,但是binary_remote_addr比remote_addr节省10个字节的空间
    # 申请空间名字是req_zone,大小为1m
    # 限制 同一个ip 一秒只能访问一次
    server {
        listen       80;
        server_name  localhost;

location / {
            root   root;
            # limit_conn conn_zone 1;
            # limit_req zone=req_zone burst=3 nodelay;
            # limit_req zone=req_zone burst=3;
            # limit_req zone=req_zone;
            index  index.html index.htm;
            #random_index on;
        }
    }
}

单独解开 limi_req zone=req_zone 的注释

  • 运行ab -n 20 -c 20 http:127.0.0.1/
  • 总共发生20次请求,同一时间请求20次

ab 命令运行后,命令行出现提示
其中 Non-2xx responses: 19,表示非200的请求有19次

This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient).....done

Server Software:        nginx/1.14.0
Server Hostname:       127.0.0.1
Server Port:            80

Document Path:          /
Document Length:        169 bytes

Concurrency Level:      20
Time taken for tests:   0.004 seconds
Complete requests:      20
Failed requests:        19
   (Connect: 0, Receive: 0, Length: 19, Exceptions: 0)
Write errors:           0
Non-2xx responses:      20
Total transferred:      14208 bytes
HTML transferred:       10372 bytes
Requests per second:    4663.09 [#/sec] (mean)
Time per request:       4.289 [ms] (mean)
Time per request:       0.214 [ms] (mean, across all concurrent requests)
Transfer rate:          3235.02 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   0.2      1       1
Processing:     1    2   0.3      2       3
Waiting:        0    2   0.4      2       2
Total:          1    3   0.4      3       3

Percentage of the requests served within a certain time (ms)
  50%      3
  66%      3
  75%      3
  80%      3
  90%      3
  95%      3
  98%      3
  99%      3
 100%      3 (longest request)

查看 /var/log/nginx/error.log 日志文件

[error] 9893#0: *169 limiting requests, excess: 0.997 by zone "req_zone",

由此可见,我们配置限制请求成功,只有第一次请求成功,而后面19次都失败了

单独解开 limit_req zone=req_zone burst=3 nodelay; 的注释

burst = 3:客户端在超过了指定速率之后,余留的3个释放到下一秒执行,可以起到访问限速的作用
nodelay:除了上面余留的三个下一秒执行,其他的全部返回503
运行ab -n 20 -c 20 http:127.0.0.1/
总共发生20次请求,同一时间请求20次
ab 命令运行后,命令行出现提示
其中 Non-2xx responses: 16,表示非200的请求有16次

Concurrency Level:      20
Time taken for tests:   0.004 seconds
Complete requests:      20
Failed requests:        16
   (Connect: 0, Receive: 0, Length: 16, Exceptions: 0)
Write errors:           0
Non-2xx responses:      16

由此可见,总共20次请求,除了1个正常访问,3个延迟请求,其他16个全部返回503错误

单独解开limit_conn conn_zone 1;

限制同一时刻只允许一个ip的连接

Nginx中间件架构

基于IP的访问控制 - http_access_module:允许某些IP访问,不允许某些IP访问
基于用于的新人登录 - http_auch_basic_module

http_access_module

Syntax : allow address | CIDR | unix: |all;
# address:允许的 ip 地址
# CIDR:允许的网段
# unix     :允许socket方式访问
# all : 允许所有
Default:----
Context:http,server,location,limit_except

Syntax :deny address | CIDR | unix:|all;
# 这里是禁止访问,参数同上
Default:--
Context:http,server,location,limit_except
配置访问限制

~ ^/ 是Nginx 的一个正则匹配
可以自行百度:https://www.cnblogs.com/koal/p/6915106.html

location ~ ^/admin.html {
    root html;
    deny 61.136.151.252;
    allow all;
    index index.html;
}
通过访问127.0.0.1/admin.html 会出现403错误

location ~ ^/admin.html {
    root html;
    allow 61.136.151.252;
    deny all;
    index index.html;
}
设置只有我能访问,其他ip都不能访问

http_access_module的局限性

Nginx网站服务(一)相关推荐

  1. Nginx网站服务与LNMP架构部署(详解)

    Nginx网站服务与LNMP架构部署 1.Nginx服务基础 2.编译安装Ngiax 3.Nginx的运行控制 4.配置文件nginx.conf 5.Nginx虚拟主机 6.LNMP架构部署 1.源码 ...

  2. nginx 根据目录指定root_部署Nginx网站服务实现访问状态统计以及访问控制功能

    Nginx专为性能优化而开发,最知名的优点是它的稳定性和低系统资源消耗,以及对HTTP并发连接的高处理能力,单个物理服务器可支持30000-50000个并发请求. Nginx的安装文件可以从官方网站h ...

  3. Web服务器群集--Nginx网站服务(运行控制,访问状态统计,基于授权和客户端的访问控制,基于域名,端口,IP的虚拟web主机访问)

    文章目录 前言 一:Nginx服务基础 1.1:Nginx概述 1.2:Nginx编译安装(过程) 1.3:运行控制(实验过程) 1.4:配置文件nginx.conf 1.5:Nginx的访问状态统计 ...

  4. 回顾Nginx网站服务配置

    Nginx简介 Nginx ("engine x") 是一个高性能的 HTTP 和反向代理服务器.Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.r ...

  5. Nginx网站服务(安装nginx、平滑升级nginx、nginx各种访问配置)

    文章目录 一.Nginx概述 1.什么是nginx? 2.Nginx应用场景 3.Nginx应用企业 4.nginx的HTTPD七层代理和四层代理 二.Nginx和Apache的区别 三.编译安装Ng ...

  6. 企业常用的Nginx网站服务相关配置

    目录 一,内容概述 Nginx Apache 二,Apache三种工作模式 三,Linux的I/O 四,编译安装Nginx服务 五,认识 Nginx 服务的主配置文件 nginx.conf 六,访问状 ...

  7. Nginx网站服务配置(Nginx服务基础,访问状态统计,访问控制,虚拟主机)

    编译安装Nginx服务 关闭防火墙,将安装 ngnix 所需的软件包上传到 /opt 目录下 安装依赖包 编译安装Nginx 创建运行用户.组 检查.启动.重载配.停止Nginx 增加Nginx系统服 ...

  8. 企业常用的Nginx网站服务相关配置——极其详细

    文章目录 一.Nginx概述 二.编译安装Nginx编译安装 1.关闭防火墙,将安装 ngnix 所需的软件包上传到 /opt 目录下 2.安装依赖包 3.创建运行用户.组 4.编译安装Nginx 5 ...

  9. 企业中常用的Nginx网站服务相关配置

    文章目录 前言: 一.Nginx概述 二.编译安装Nginx 服务 三.认识 Nginx 服务的主配置文件 nginx.conf 四.访问状态统计配置 五.基于授权的访问控制 六.基于客户端的访问控制 ...

  10. Nginx网站服务配置

    目录 一.Nginx概述 二.编译安装nginx服务 2.1 关闭防火墙,将nginx所需压缩包上传到/opt目录下 2.2 安装依赖包 2.3 创建运行用户和组,便于管理 2.4 解压软件包,编译安 ...

最新文章

  1. Oracle 数据库利用sql语句判断某个表是否是临时表实例演示,达梦数据库查询出所有临时表
  2. 'demangle.h' file not found
  3. Strategy 定义一系列算法或策略,把它们封闭起来,并且使它们相互可以替换。各算法或策略可以独立于客户程序而变化。...
  4. python polar contour_13.极坐标系(Polar coordinate system)
  5. 不同包之间的继承extends
  6. 开个坑: gitk和git容易被滥用的 cherry-pick 功能
  7. python的selenium模块_python-爬虫-selenium模块
  8. url-pattern主要有四种匹配方式
  9. 思科CISCO常用命令汇总
  10. Python拉宾米勒(判断素数)
  11. 李宏毅学习笔记42.More about Domain Adaptation. Part I. Domain Adaptation
  12. staf linux运行模式,IBM 自动化测试框架STAF介绍
  13. 存储系统基础知识介绍
  14. 应用与系统稳定性第五篇---Watchdog原理和问题分析
  15. oracle 远程连接配置
  16. java基础:Java七大外企经典面试精讲视频
  17. Jetson Nano目标检测手把手实战教程(pytorch训练、tensorrt推理,含完整代码和数据)
  18. java面试题复习02
  19. 上期所API头文件二、ThostFtdcUserApiDataType.h---API常量及类型的定义(源代码6.3.19版)
  20. 无线远距离WiFi模组,CV5200远程实时传输应用,无线mesh自组网方案

热门文章

  1. 你可能会用到的实用网站
  2. 天语 W619 一键ROOT 教程
  3. SpringBoot+Vue实现excel导入带格式化的时间参数(moment格式化明天日期并设置el-date-picker默认值)
  4. Canvas判断线段是否重叠
  5. java中的terminated_Java State.TERMINATED屬性代碼示例
  6. 小学生用什么样的台灯比较好?推荐高品质、好用的学生党台灯
  7. SAP内表转json json转内表方法(自定义实现 ZCL_JSON)
  8. 逍遥安卓微信连不上服务器,逍遥安卓模拟器使用常见问题
  9. 三国志吞食天地2(街机)的全人物通关研究
  10. 你了解互联网APP搜索和推荐的背后逻辑么?