네 속도 전송 보안 체인 열린 응답 리디렉션 마지막 휴식 루트를 균형 limit_rate 상류로드, alias 명령의 차이

limit_rate 한계에게 데이터 전송 클라이언트의 속도를 사용합니다 **

1, 편집 /etc/nginx/nginx.conf

location / {
            root   /var/www/nginx/;
            index  index.html index.htm;
            limit_rate  2k;  #对每个连接的限速为2k/s
}

2의 nginx 프록시 에이전트를 사용

server {
    listen       80;
    server_name  localhost;

    location / {
    proxy_pass http://192.168.62.157:80;
#    proxy_redirect default;
    proxy_set_header Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    proxy_connect_timeout 30;
    proxy_send_timeout 60;
    proxy_read_timeout 60;
    }
}

proxy_pass :真实服务器的地址,可以是ip也可以是域名和url地址
#proxy_redirect :如果真实服务器使用的是的真实IP:非默认端口。则改成IP:默认端口。(可选)
proxy_set_header:重新定义或者添加发往后端服务器的请求头
proxy_set_header X-Real-IP :启用客户端真实地址(否则日志中显示的是代理在访问网站)
proxy_set_header X-Forwarded-For:记录代理地址

proxy_connect_timeout:后端服务器连接的超时时间发起三次握手等候响应超时时间
proxy_send_timeout:后端服务器数据回传时间就是在规定时间之内后端服务器必须传完所有的数据
proxy_read_timeout :nginx接收upstream(上游/真实) server数据超时, 默认60s, 如果连续的60s内没有收到1个字节, 连接关闭。像长连接

2 상류 구성을로드 밸런싱

upstream youngfitapp { 
      server 192.168.62.157:8080;  #也可以是域名
      server 192.168.62.158:8080 backup;  #热备 
        ip_hash;  ip_hash:nginx会让相同的客户端ip请求相同的服务器。
    }
 server {
        listen 80;
        server_name localhost;
        location / {         
           proxy_pass  http://youngfitapp;
        }
}
server 192.168.62.157:8080 weight=2 max_fails=2 fail_timeout=2;
- down,表示当前的server暂时不参与负载均衡。
- backup,预留的备份机器。当其他所有的非backup机器出现故障或者忙的时候,才会请求backup机器,因此这台机器的压力最轻。
- max_fails,允许请求失败的次数,默认为1。当超过最大次数时,返回错误。
- fail_timeout,在经历了max_fails次失败后,暂停服务的时间单位秒。max_fails可以和fail_timeout一起使用。

1.9.0 Nginx의 시간에, 4 층의 프로토콜 (네트워크 계층과 전송 계층) 전달 제,로드 밸런싱을 구현하도록 구성된 스트림 모듈을 추가한다. HTTP의 유사한 용도로 사용 스트림 모듈은 우리가, proxy_pass에 의해 우리의 요청을 전달 여러 백엔드 서비스,로드 밸런싱을 추가하여 상류 다음과 같은 TCP 나 UDP 리스너와 같은 프로토콜의 집합을 구성 할 수 있습니다.

#4层tcp负载
stream {
			upstream myweb {
                hash $remote_addr consistent;
                server 172.17.14.2:8080;
                server 172.17.14.3:8080;
        }
        server {
            listen 80;
            proxy_connect_timeout 10s;
            proxy_timeout 30s;
            proxy_pass myweb;
        }
}
1 ip_hash

서버를 사용할 수없는 경우를 제외하고 소스 주소 해싱 알고리즘을 사용하여 ip_hash, 같은 클라이언트 요청은 항상 같은 백 엔드 서버로 전송됩니다.

ip_hash 구문 :

upstream backend {
    ip_hash;
    server backend1.example.com;
    server backend2.example.com;
    server backend3.example.com down;
}
2, sticky_cookie_insert

사용 서버의 그룹을 전달 동일한 서버에 같은 클라이언트의 요청에 이어질 수있는 세션 선호도를 활성화 sticky_cookie_insert. 그리고 ip_hash 차이가 클라이언트를 결정하기 위해 IP 기반으로하지 않습니다하지만, 쿠키 판단하기 때문이다. 그래서 부하 불균형에 동일한 클라이언트 리드에서 이러한 ip_hash을 피하십시오. (달성하기 위해 타사 모듈을 도입 할 필요)

끈적 모듈 (또한 액세스 도메인 이름을 기준으로 해석 될 수있다)

upstream backend {
    server backend1.example.com;
    server backend2.example.com;
    sticky_cookie_insert srv_id expires=1h domain=3evip.cn path=/;
}  #访问域名 3evip.cn  会转到上面两台服务器上

server {
    listen 80;
    server_name 3evip.cn;
    location / {
		proxy_pass http://backen;
    }
}
expires:设置浏览器中保持cookie的时间
domain:定义cookie的域
path:为cookie定义路径

도난 방지 체인 구성

[root@nginx-server ~]# vim /etc/nginx/nginx.conf
# 日志格式添加"$http_referer"
log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                         '$status $body_bytes_sent "$http_referer" '
                         '"$http_user_agent" "$http_x_forwarded_for"';
# valid_referers 使用方式                         
Syntax: 	valid_referers none | blocked | server_names | string ...;
Default: 	—
Context: server, location
server {
    listen       80;
    server_name  localhost;
    location ~  .*\.(gif|jpg|png|jpeg)$ {
        root  /usr/share/nginx/html;

        valid_referers none blocked *.qf.com 192.168.1.10;
                if ($invalid_referer) {
                        return 403;
                }
        }
}
因为none允许为空值访问,所以加不加ip都可以访问,如果把none擦除,就不可以了
重载nginx服务
- none : 允许没有http_refer的请求访问资源;

- blocked : 允许不是http://开头的,不带协议的请求访问资源---被防火墙过滤掉的;

- server_names : 只允许指定ip/域名来的请求访问资源(白名单);

  准备两台机器,一张图片网站服务器
2.2 다시 쓰기 플래그

발현에 따라 명령은 URI를 재 지정하거나 문자열을 수정할 수 있습니다. 그것은에 적용 할 수있는 서버, 위치, 경우 의 지원으로 표시 플래그 마커 플래그 마지막 재 작성 명령 환경의 각 라인 :

last 			    相当于Apache里的[L]标记,表示完成rewrite。默认为last。 还继续匹配
break 				本条规则匹配完成后,终止匹配,不再匹配后面的规则
redirect 			返回302临时重定向,浏览器地址会显示跳转后的URL地址
permanent 		    返回301永久重定向,浏览器地址会显示跳转后URL地址
 http://www.testpm.com/a/1.html ==> http://www.testpm.com/b/2.html
server {
    listen       80;
    server_name  www.testpm.com;

        location /a {
        root /html;
        index   1.html index.htm;
        rewrite .* /b/2.html permanent;
        }

        location /b {
        root    /html;
        index   2.html index.htm;
        }

}
# http://www.youngfit.com/a/1.html ==> http://jd.com/a/1.html
location /a {
        root /html;
        if ( $host ~* youngfit.com ){
        rewrite .* http://jd.com$request_uri permanent;
        }
}
server {
    listen       80;
    server_name  localhost;
    access_log  /var/log/nginx/last.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
    
    location /break/ {
        root /usr/share/nginx/html;
        rewrite .* /test/break.html break;
    }
    
    location /last/ {
        root /usr/share/nginx/html;
        rewrite .* /test/last.html last;
    }

    location /test/ {
        root /usr/share/nginx/html;
        rewrite .* /test/test.html break;
    }
}
[root@localhost conf.d]# cd /usr/share/nginx/html/
[root@localhost html]# mkdir test
[root@localhost html]# echo "last" > test/last.html
[root@localhost html]# echo "break" > test/break.html
[root@localhost html]# echo "test" > test/test.html

http://192.168.1.247/break/break.html   break
http://192.168.62.159/last/last.html  test
- last 标记在本条 rewrite 规则执行完后,会对其所在的 server {} 标签重新发起请求;

- break 标记则在本条规则匹配完成后,停止匹配,不再做后续的匹配;

루트, alias 명령의 차이 **

location /img/ {
    alias /var/www/image/;
}
#若按照上述配置的话,则访问/img/目录里面的文件时,ningx会自动去/var/www/image/目录找文件
location /img/ {
    root /var/www/image;
} 
#若按照这种配置的话,则访问/img/目录下的文件时,nginx会去/var/www/image/img/目录下找文件。
  • 별칭 별칭은 정의 된 디렉토리입니다
  • 루트는 최상위 디렉토리를 정의한다.
게시 48 개 원래 기사 · 원의 찬양 (18) · 전망 3649

추천

출처blog.csdn.net/wx912820/article/details/104864787