See all Nginx in one small picture

Release: 2023-08-02 17:28:58
forward
497 people have browsed it

See all Nginx in one small picture


See all Nginx in one small picture

#nginx installation

Nginx installation is completed, there is no sbin directory

cd into the nginx-1.18.0 directory and execute

[root@centos7 nginx-1.18.0]# ./configure --prefix=/usr/local/nginx[root@centos7 nginx-1.18.0]# make[root@centos7 nginx-1.18.0]# make install
Copy after login

ps: –prefix=path defines a directory to store files on the server, which is the installation directory of nginx. By default, /usr/local/nginx

is used. You will see that there is an nginx directory at the same level as the nginx1.12.2 you installed in the local directory, and there is an sbin directory in it.

基础指令

listen:该指令用于配置网络监听。

listen *:80 | *:8080      #监听所有80端口和8080端口listen  IP_address:port   #监听指定的地址和端口号listen  IP_address        #监听指定ip地址所有端口listen port               #监听该端口的所有IP连接
Copy after login

server_name:该指令用于虚拟主机的配置。

a. 基于名称的虚拟主机配置

server_name   name ...;
Copy after login

b. 基于 IP 地址的虚拟主机配置

server_name 192.168.1.1
Copy after login

location:该指令用于匹配 URL。

location指令的作用是根据用户请求的URI来执行不同的应用,也就是根据用户请求的网站URL进行匹配,匹配成功即进行相关的操作。

牛逼啊!接私活必备的 N 个开源项目!赶快收藏吧
Copy after login

location的语法

  • =开头表示精确匹配

  • 如 A 中只匹配根目录结尾的请求,后面不能带任何字符串。

  • ^~ 开头表示uri以某个常规字符串开头,不是正则匹配

  • ~ 开头表示区分大小写的正则匹配;

  • ~* 开头表示不区分大小写的正则匹配

  • / 通用匹配, 如果没有其它匹配,任何请求都会匹配到

Location正则案例

#精确匹配,/后面不能带任何字符
server {
        listen       80;
        server_name  www.itmayiedu.com;
        #精确匹配,注解后面不能带任何字符
        location =/ {
            proxy_pass http://127.0.0.1:8080;
            index  index.html index.htm;
        }
}

 #匹配所有以/开头请求
server {
        listen       80;
        server_name  www.itmayiedu.com;
       #匹配所有以/开头请求 
        location / {
            proxy_pass http://127.0.0.1:8080;
            index  index.html index.htm;
        }
    }
###  以开头/itmayiedu_8080拦截  默认开启不区分大小写
    server {
        listen       80;
        server_name  www.itmayiedu.com;
        ###  以开头/itmayiedu_8080 最终跳转到http://127.0.0.1:8080/;
        location /itmayiedu_8080/ {
            proxy_pass http://127.0.0.1:8080/;
            index  index.html index.htm;
        }
        ###  以开头/itmayiedu_8080 最终跳转到http://127.0.0.1:8081/;
        location /itmayiedu_8081/ {
            proxy_pass http://127.0.0.1:8081/;
            index  index.html index.htm;
        }
    }
### 开头区分大小写
Copy after login

proxy_pass:该指令用于设置被代理服务器的地址。可以是主机名称、IP地址加端口号的形式。

语法结构如下:

proxy_pass URL;
Copy after login

index:该指令用于设置网站的默认首页。

域名重定向

server
{
    listen 80 ;
    server_name mxiaoqi.top aaa.com;
    if ( $host = mxiaoqi.top )
    #增加判断条件,当访问域名是mxiaoqi.top的时候
    {
        rewrite /(.*)  http://aaa.com/$1 permanent;
        #把mxiaoqi.top/后面的内容重新写到aaa.com/后面如果后面有多段则使用$2、$3以此类推
        #permanent是转发状态码
    }
    index index.html index.htm index.php;
    root /data/wwwroot/mxiaoqi.top;
    }
Copy after login

反向代理

使用 nginx 反向代理 www.123.com 直接跳转到127.0.0.1:8080

server {
         listen       80;
         server_name  www.123.com;

         location / {
             proxy_pass http://127.0.0.1:8080;
                          # 欢迎页面,按照从左到右的顺序查找页面
             index  index.html index.htm index.jsp;
         }
     }
Copy after login

监听80端口,访问域名为www.123.com,不加端口号时默认为80端口,故访问该域名时会跳转到127.0.0.1:8080路径上。

限流配置

漏桶算法与令牌桶算法区别:主要区别在于“漏桶算法”能够强行限制数据的传输速率,

而“令牌桶算法”在能够限制数据的平均传输速率外,还允许某种程度的突发传输。在“令牌桶算法”中,只要令牌桶中存在令牌,那么就允许突发地传输数据直到达到用户配置的门限,因此它适合于具有突发特性的流量。

Nginx按请求速率限速模块使用的是漏桶算法,即能够强行保证请求的实时处理速度不会超过设置的阈值。

  • limit_req_zone 用来限制单位时间内的请求数,即速率限制,采用的漏桶算法 "leaky bucket"。

  • limit_req_conn 用来限制同一时间连接数,即并发限制。

limit_req_zone 参数配置

Syntax:    limit_req zone=name [burst=number] [nodelay];
Default:    —
Context:    http, server, location
Copy after login

limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;

  • 第一个参数:<span style="margin: 0px;padding: 0px;outline: 0px;max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;font-size: 15px;">$binary_remote_addr</span>表示通过remote_addr这个标识来做限制,“binary_”的目的是缩写内存占用量,是限制同一客户端ip地址。

  • 第二个参数:zone=one:10m表示生成一个大小为10M,名字为one的内存区域,用来存储访问的频次信息。

  • 第三个参数:rate=1r/s表示允许相同标识的客户端的访问频次,这里限制的是每秒1次,还可以有比如30r/m的。

limit_req zone=one burst=5 nodelay;

  • 第一个参数:zone=one 设置使用哪个配置区域来做限制,与上面limit_req_zone 里的name对应。

  • 第二个参数:burst=5,重点说明一下这个配置,burst爆发的意思,这个配置的意思是设置一个大小为5的缓冲区当有大量请求(爆发)过来时,超过了访问频次限制的请求可以先放到这个缓冲区内。

  • 第三个参数:nodelay,如果设置,超过访问频次而且缓冲区也满了的时候就会直接返回503,如果没有设置,则所有请求会等待排队。

ngx_http_limit_conn_module 参数配置

这个模块用来限制单个IP的请求数。并非所有的连接都被计数。只有在服务器处理了请求并且已经读取了整个请求头时,连接才被计数。

Syntax:    limit_conn zone number;
Default:    —
Context:    http, server, location

limit_conn_zone $binary_remote_addr zone=addr:10m;
server {
    location /download/ {
        limit_conn addr 1;
    }
Copy after login

一次只允许每个IP地址一个连接。

负载均衡

#user  nobody;
worker_processes  1;
error_log  logs/error.log;# 开启日志
pid        logs/nginx.pid;
...
    upstream lyf {
    server 192.168.37.220:8001; # 3个tomcat服务
    server 192.168.37.220:8002;
    server 192.168.37.220:8003;
    }
    server {
        listen       80;
        server_name  192.168.37.220;# 监听ip
        location / {
            proxy_pass   http://lyf;  # 设置代理
            index  index.html index.htm;
        }
}
Copy after login

keepalive 长连接提高吞吐量
keepalived :设置长连接处理的数量
proxy_http_version :设置长连接http版本为1.1
proxy_set_header :清除connection header 信息

upstream tomcats { 
# server 192.168.1.173:8080 max_fails=2 fail_timeout=1s; 
server 192.168.1.190:8080; 
# server 192.168.1.174:8080 weight=1; 
# server 192.168.1.175:8080 weight=1; 
keepalive 32; 
}
server {
listen 80; 
server_name www.tomcats.com; 
location / { 
proxy_pass http://tomcats; 
proxy_http_version 1.1; 
proxy_set_header Connection ""; 
} 
}
Copy after login

工作方式

轮询方式是Nginx负载默认的方式

权重方式 指定每个服务的权重比例,weight和访问比率成正比

upstream  dalaoyang-server {
       server    localhost:10001 weight=1;
       server    localhost:10002 weight=2;
}
Copy after login

iphash

每个请求都根据访问ip的hash结果分配,经过这样的处理,每个访客固定访问一个后端服务,如下配置(ip_hash可以和weight配合使用)。

upstream  dalaoyang-server {
       ip_hash; 
       server    localhost:10001 weight=1;
       server    localhost:10002 weight=2;
}
Copy after login

最少连接

将请求分配到连接数最少的服务上。

upstream  dalaoyang-server {
       least_conn;
       server    localhost:10001 weight=1;
       server    localhost:10002 weight=2;
}
Copy after login

fair

按后端服务器的响应时间来分配请求,响应时间短的优先分配。

upstream  dalaoyang-server {
       server    localhost:10001 weight=1;
       server    localhost:10002 weight=2;
       fair;  
}
Copy after login

Consul+upsync+Nginx 实现无需raload动态负载均衡 https://www.cnblogs.com/a1304908180/p/10697278.html

传统的负载均衡,如果Upstream参数发生变化,每次都需要重新加载nginx.conf文件,

因此扩展性不是很高,所以我们可以采用动态负载均衡,实现Upstream可配置化、动态化,无需人工重新加载nginx.conf。

LVS+Keepalived+Nginx+Tomcat搭建高可用双机主从热备集群

https://blog.csdn.net/dsen726/article/details/89519013

需要明确的是:

  • Nginx两台是主备关系,只有一台在工作。后面的tomcat是集群,同时工作的。

  • keepalived是同时安装在两台Nginx上的,不过文件配置不一样

  • 这里的双机热备是指LVS,Nginx则是集群

keepalived

Health check and failover are the two core functions of keepalived. The so-called health check uses TCP three-way handshake, ICMP request, HTTP request, UDP echo request, etc. to keep alive the actual server behind the load balancer (usually the server that carries the real business); while failed switching is mainly the application For load balancers configured in active and standby mode, VRRP is used to maintain the heartbeat of the active and standby load balancers. When a problem occurs with the active load balancer, the standby load balancer carries the corresponding services, thereby minimizing traffic loss and Provide stability of services. In addition, when searching for public accounts, Linux should learn how to reply "monkey" in the background and get a surprise gift package.

LVS is the abbreviation of Linux Virtual Server, which means Linux virtual server and is a virtual server cluster system. lvs is currently integrated into Linux.

See all Nginx in one small picture

See all Nginx in one small picture

See all Nginx in one small picture

Why do you need LVS Nginx?

1. ngix (seven-layer application layer network load balancing)

1. Asynchronous forwarding, request data and corresponding data must go through ngix , ngix establishes a connection with the client

2. Poll all tomcat servers to ensure that the request is successful or the last tomcat server fails the request

2. lvs (network layer four-layer load balancing)

1. Synchronous forwarding accepts request data, lvs forwards it to the server, and the server directly establishes a connection with the client

nginx has to bear all the traffic. When one nigx cannot bear it, an nginx cluster needs to be built. ngix ngix The outer ngix still has to bear all traffic.

lvs ngix:lvs synchronous forwarding will not accept corresponding data. When LVS adopts DR mode, it does not need to respond to the content returned by the server. (Usually the request data is relatively small, and the response data will be relatively large)

See all Nginx in one small picture

#

静态资源配置

  location ~ .*\.(jpg|gif|png)$ {
        gzip on;
        gzip_http_version 1.1;
        gzip_comp_level 2;
        gzip_types text/plain application/javascript image/jpeg image/gif image/png;
        root   /usr/share/nginx/images;
    }

    location ~ .*\.(txt|xml)$ {
        gzip on;
        gzip_http_version 1.1;
        gzip_comp_level 1;
        gzip_types text/plain application/javascript image/jpeg image/gif image/png;
        root   /usr/share/nginx/code;
    }
  location ~ ^/download {
        gzip_static on;
        tcp_nopush on;
        root /opt/app/code;
    }
Copy after login

sendfile on | off,文件读取配置

默认sendfile是关闭的,可以配置在http,server,location,if in location中

tcp_nopush on | off,多个包整合

默认是关闭状态,可以在http,server,location中配置,它的作用是在sendfile开启的情况下,提高网络包的传输效率。什么意思呢,假设服务端收到请求,需要推送10个包,为了提高传输效率,这10个包不会一个一个返回给客户端,而是将10个包攒够了后一起返回回去。

tcp_nodelay on | off,网络包的实时性传输

默认开启,可以在http,server,location中配置,它的作用是在keepalive链接下,提高网络包的传输实时性。

gzip on | off,压缩

默认是关闭状态,可以在http,server,location,if in location中配置,作用是压缩传输。一般来说浏览器是可以对压缩后的内容进行解压的。

gzip_comp_level level;压缩级别

默认的压缩级别是1,可以在http,server,location中配置,级别配置的越高,压缩的越好,但是压缩会耗费服务端的计算资源,所以要控制好压缩级别

gzip_http_version 1.0 | 1.1,压缩对http协议的支持

默认对HTTP/1.1协议的请求才会进行gzip压缩,可以配置在http,server,location中配置。当用户想要读取一个1.html文件,首先会在目录中找寻1.html.gz是否存在,所以这就导致了磁盘资源的浪费,必须要存储两份文件。

###静态资源访问
    server {
      listen       80;
      server_name  static.itmayiedu.com;
      location /static/imgs {
           root F:/;
           index  index.html index.htm;
       }
    }
   ###动态资源访问
     server {
      listen       80;
      server_name  www.itmayiedu.com;

      location / {
         proxy_pass http://127.0.0.1:8080;
         index  index.html index.htm;
       }
    }
Copy after login

跨域配置

See all Nginx in one small picture

跨域就是在原站点访问域名不同的其他站点,同源策略会阻止一个域的javascript脚本和另外一个域的内容进行交互。所谓同源(即指在同一个域)就是两个页面具有相同的协议(protocol),主机(host)和端口号(port)。

CORS 是跨域资源分享(Cross-Origin Resource Sharing)的缩写。它是 W3C 标准,属于跨源 AJAX 请求的根本解决方法。

1、普通跨域请求:只需服务器端设置Access-Control-Allow-Origin

2、带cookie跨域请求:前后端都需要进行设置

#允许跨域请求的域,*代表所有
add_header &#39;Access-Control-Allow-Origin&#39; *;
#允许带上cookie请求 
add_header &#39;Access-Control-Allow-Credentials&#39; &#39;true&#39;; 
#允许请求的方法,比如 GET/POST/PUT/DELETE 
add_header &#39;Access-Control-Allow-Methods&#39; *; 
#允许请求的header 
add_header &#39;Access-Control-Allow-Headers&#39; *;
Copy after login

防盗链

#对源站点验证
valid_referers *.imooc.com; 
#非法引入会进入下方判断 
if ($invalid_referer) { return 404; }
Copy after login

source: https://www.yuque.com/molizhuzhu/thrgrk/rtslmc

The above is the detailed content of See all Nginx in one small picture. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:Linux中文社区
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!