#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
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连接
server_name:该指令用于虚拟主机的配置。
a. 基于名称的虚拟主机配置
server_name name ...;
b. 基于 IP 地址的虚拟主机配置
server_name 192.168.1.1
location:该指令用于匹配 URL。
location指令的作用是根据用户请求的URI来执行不同的应用,也就是根据用户请求的网站URL进行匹配,匹配成功即进行相关的操作。
牛逼啊!接私活必备的 N 个开源项目!赶快收藏吧
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; } } ### 开头区分大小写
proxy_pass:该指令用于设置被代理服务器的地址。可以是主机名称、IP地址加端口号的形式。
语法结构如下:
proxy_pass URL;
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; }
反向代理
使用 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; } }
监听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
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; }
一次只允许每个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; } }
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 ""; } }
工作方式
轮询方式是Nginx负载默认的方式
权重方式 指定每个服务的权重比例,weight和访问比率成正比
upstream dalaoyang-server { server localhost:10001 weight=1; server localhost:10002 weight=2; }
iphash
每个请求都根据访问ip的hash结果分配,经过这样的处理,每个访客固定访问一个后端服务,如下配置(ip_hash可以和weight配合使用)。
upstream dalaoyang-server { ip_hash; server localhost:10001 weight=1; server localhost:10002 weight=2; }
最少连接
将请求分配到连接数最少的服务上。
upstream dalaoyang-server { least_conn; server localhost:10001 weight=1; server localhost:10002 weight=2; }
fair
按后端服务器的响应时间来分配请求,响应时间短的优先分配。
upstream dalaoyang-server { server localhost:10001 weight=1; server localhost:10002 weight=2; fair; }
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.
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)
静态资源配置
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; }
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; } }
跨域配置
跨域就是在原站点访问域名不同的其他站点,同源策略会阻止一个域的javascript脚本和另外一个域的内容进行交互。所谓同源(即指在同一个域)就是两个页面具有相同的协议(protocol),主机(host)和端口号(port)。
CORS 是跨域资源分享(Cross-Origin Resource Sharing)的缩写。它是 W3C 标准,属于跨源 AJAX 请求的根本解决方法。
1、普通跨域请求:只需服务器端设置Access-Control-Allow-Origin
2、带cookie跨域请求:前后端都需要进行设置
#允许跨域请求的域,*代表所有 add_header 'Access-Control-Allow-Origin' *; #允许带上cookie请求 add_header 'Access-Control-Allow-Credentials' 'true'; #允许请求的方法,比如 GET/POST/PUT/DELETE add_header 'Access-Control-Allow-Methods' *; #允许请求的header add_header 'Access-Control-Allow-Headers' *;
防盗链
#对源站点验证 valid_referers *.imooc.com; #非法引入会进入下方判断 if ($invalid_referer) { return 404; }
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!

本篇文章给大家带来了关于nginx的相关知识,其中主要介绍了nginx拦截爬虫相关的,感兴趣的朋友下面一起来看一下吧,希望对大家有帮助。

高并发系统有三把利器:缓存、降级和限流;限流的目的是通过对并发访问/请求进行限速来保护系统,一旦达到限制速率则可以拒绝服务(定向到错误页)、排队等待(秒杀)、降级(返回兜底数据或默认数据);高并发系统常见的限流有:限制总并发数(数据库连接池)、限制瞬时并发数(如nginx的limit_conn模块,用来限制瞬时并发连接数)、限制时间窗口内的平均速率(nginx的limit_req模块,用来限制每秒的平均速率);另外还可以根据网络连接数、网络流量、cpu或内存负载等来限流。1.限流算法最简单粗暴的

实验环境前端nginx:ip192.168.6.242,对后端的wordpress网站做反向代理实现复杂均衡后端nginx:ip192.168.6.36,192.168.6.205都部署wordpress,并使用相同的数据库1、在后端的两个wordpress上配置rsync+inotify,两服务器都开启rsync服务,并且通过inotify分别向对方同步数据下面配置192.168.6.205这台服务器vim/etc/rsyncd.confuid=nginxgid=nginxport=873ho

nginx php403错误的解决办法:1、修改文件权限或开启selinux;2、修改php-fpm.conf,加入需要的文件扩展名;3、修改php.ini内容为“cgi.fix_pathinfo = 0”;4、重启php-fpm即可。

跨域是开发中经常会遇到的一个场景,也是面试中经常会讨论的一个问题。掌握常见的跨域解决方案及其背后的原理,不仅可以提高我们的开发效率,还能在面试中表现的更加

nginx部署react刷新404的解决办法:1、修改Nginx配置为“server {listen 80;server_name https://www.xxx.com;location / {root xxx;index index.html index.htm;...}”;2、刷新路由,按当前路径去nginx加载页面即可。

linux版本:64位centos6.4nginx版本:nginx1.8.0php版本:php5.5.28&php5.4.44注意假如php5.5是主版本已经安装在/usr/local/php目录下,那么再安装其他版本的php再指定不同安装目录即可。安装php#wgethttp://cn2.php.net/get/php-5.4.44.tar.gz/from/this/mirror#tarzxvfphp-5.4.44.tar.gz#cdphp-5.4.44#./configure--pr

nginx禁止访问php的方法:1、配置nginx,禁止解析指定目录下的指定程序;2、将“location ~^/images/.*\.(php|php5|sh|pl|py)${deny all...}”语句放置在server标签内即可。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.