nginx之HTTP模块配置

 listen   指令只能使用与server字段里

如果本地调用可以监听本地Unix套接字文件,性能更加,因为不用走内核网络协议栈

 listen unix:/var/run/nginx.sock;  

  针对端口加地址的监听;表示之匹配127.0.0.1的8000端口请求

listen 127.0.0.1:8000;

  监听本机所有IP的端口

listen 8000;

  或者这么表示

listen *:8000;

  监听IPV6地址

listen [::]:8000 ipv6only=on;

  处理HTTP请求头部流程

内核与客户端建立好tcp 连接后,根据客户端的访问的端口判断交给系统那个系统处理,这是我们的nginx监听的端口(448或者80),是客户端请求的端口,这是内核会根据负载均衡算法选择一个work进程里的一个epoll_wait方法会返回已建立好的句柄,这是一个读事件,读取请求,跟据请求调用accept方法分配连接内存池,接下来就是http模块处理 调用ngx_http_init_connection方法读取事件添加epll_ctl中,并添加一个定时器,如果60秒没有收到请求,就会超时;读取用户请求数据从内核,然后在内核的用户态分配内存,默认分配1k空间可以设置,

接收到用户请求后会分配请求内存池,默认4K可以做调整,然后用状态机解析请求的行,解析的时候如果发现url地址1k内存放不下,nginx会自动扩充内存,默认最大扩充到4 8k,表示先把那1k的数据复制8k里,用剩下的7k在去接受用户剩下的url,如果还不够就会在分配8k,默认最大分配32k,靠nginx内置变量标识url,然后解析http的header部分,在分配大内存主意这个大内存与URL的大内存共用的,标识头部确定那个server块处理请求,当标识完全部header后,就移除定时器,开始11个阶段的http请求处理

nginx的正则

   元字符

. 可以匹配除换行符以外的任意字符

\w 可以匹配字母或者数字会在下划线或者数字

\s  匹配任意的空白字符

\d 匹配数字

\b 匹配单词开始或结束

^匹配字符串的开始

$匹配字符串的结束

重复

* 重复零次或多次

+ 重复1次或更多次

?重复零次或一次

{n} 重复n次

{n,}重复n次或者更多次

{n,m}重复n到m次

实例

 server name 指令

     server {
        server_name chenxi.com www.cx.com;
        server_name_in_redirect off;  
        return 302 /redirect; 
        }
[root@nginx conf]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.10.90 chenxi.com www.cx.com
[root@nginx conf]# nginx -s reload
[root@nginx conf]# curl http://www.cx.com -I
HTTP/1.1 302 Moved Temporarily
Server: nginx/1.15.12
Date: Sat, 08 Jun 2019 22:16:19 GMT
Content-Type: text/html
Content-Length: 146
Location: http://www.cx.com/redirect 默认返回的是你访问的域名加后面的redirect
Connection: keep-alive
调整配置文件   
  server {
        server_name chenxi.com www.cx.com;
        server_name_in_redirect on;   改成on
        return 302 /redirect;
        }
测试
[root@nginx conf]# nginx -s reload
[root@nginx conf]# curl http://www.cx.com -I
HTTP/1.1 302 Moved Temporarily
Server: nginx/1.15.12
Date: Sat, 08 Jun 2019 22:19:27 GMT
Content-Type: text/html
Content-Length: 146
Location: http://chenxi.com/redirect   返回的是主域名跳转
Connection: keep-alive

  其他用法

.chenxi.com可以匹配chenxi.com和*.chenxi.com

_匹配所有

“”匹配没有传递的Host头部

server匹配顺序

精确匹配  

*.在前面的泛域名

*.在后面的泛域名

文件中顺序正则匹配的域名

default server

  第一个listen指定的default 

http 的11个阶段

   

realip模块可以获取真实客户端地址

如何拿到用户真实IP

 拿到用户IP如何使用

 默认不会编译到nginx中的, --with-http_realip_module  将模块编译到nginx中

模块指令的介绍

set_real_ip_from address | CIDR | unix:;

可用范围:httpserverlocation

默认值为空

表示从这个IP发来的请求从头部去取用户的IP;定义的是前端代理或者cdn地址

real_ip_header field | X-Real-IP | X-Forwarded-For | proxy_protocol; 定义要取得变量默认X-Real-IP

可用范围:http,server,location

real_ip_recursive on | off;    表示如过客户端IP与代理IP相同之间跳过
real_ip_recursive off; 默认
 可用范围:http,server,location

  修改配置文件查看效果

     server{
        server_name chenxi.com www.cx.com;
        error_log logs/myerror.log debug;
        set_real_ip_from 192.168.10.90;  
        real_ip_recursive off;
        real_ip_header X-Forwarded-For;
        location /{
                return 200 "Client real ip : $remote_addr\n";
        }
}
nginx -s reload
测试
[root@nginx conf]# curl  -H "X-Forwarded-For: 1.1.1.1,192.168.10.90" chenxi.com
Client real ip : 192.168.10.90

  修改配置文件开启real_ip_recursive on 查看效果

     server{
        server_name chenxi.com www.cx.com;
        error_log logs/myerror.log debug;
        set_real_ip_from 192.168.10.90;
        real_ip_recursive on;
        real_ip_header X-Forwarded-For;
        location /{
                return 200 "Client real ip : $remote_addr\n";
        }
}
nginx -s reload
[root@nginx conf]# curl  -H "X-Forwarded-For: 1.1.1.1,192.168.10.90" chenxi.com
Client real ip : 1.1.1.1   触发了动作使用之前的地址

  官网介绍http://nginx.org/en/docs/http/ngx_http_realip_module.html#set_real_ip_from

 http_rewrite_module 模块介绍

return 指令介绍

实例

server {
        server_name haha.com;
        listen 8080;
        root html/;
        error_page 404 /403.html;
        #return 405;
        location /{
                #return 404 "find nothing!\n";
        }
}
 nginx -s reload  加载测试

[root@nginx html]# echo "sdddf" > 403.html
[root@nginx vhost]# curl http://haha.com:8080/aa.html
sdddf
修改配置文件
server {
        server_name haha.com;
        listen 8080;
        root html/;
        error_page 404 /403.html;
        #return 405;
        location /{
                return 404 "find nothing!\n";
        }
}
 nginx -s reload  加载测试
[root@nginx vhost]# curl http://haha.com:8080/aa.html
find nothing!

  修改配置

[root@nginx vhost]# vim test.conf 

server {
        server_name haha.com;
        listen 8080;
        root html/;
        error_page 404 /403.html;
        return 405;
        location /{
                return 404 "find nothing!\n";
        }
}
[root@nginx conf]# nginx -s reload
[root@nginx vhost]# curl http://haha.com:8080/aa.html
<html>
<head><title>405 Not Allowed</title></head>
<body>
<center><h1>405 Not Allowed</h1></center>
<hr><center>nginx/1.15.12</center>
</body>
</html>

  

  

猜你喜欢

转载自www.cnblogs.com/rdchenxi/p/10992394.html