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>

  

  

nginx之HTTP模块配置的更多相关文章

  1. Nginx使用SSL模块配置https

    背景 开发微信小程序,需要https域名,因此使用Nginx的SSL模块配置https 步骤 一.去域名管理商(如腾讯云.阿里云等)申请CA证书 二.在Nginx中配置,一般情况下域名管理商会提供配置 ...

  2. nginx使用ssl模块配置支持HTTPS访问

    默认情况下ssl模块并未被安装,如果要使用该模块则需要在编译nginx时指定–with-http_ssl_module参数. 需求: 做一个网站域名为 www.localhost.cn 要求通过htt ...

  3. nginx使用ssl模块配置HTTPS支持

    默认情况下ssl模块并未被安装,如果要使用该模块则需要在编译时指定–with-http_ssl_module参数,安装模块依赖于OpenSSL库和一些引用文件,通常这些文件并不在同一个软件包中.通常这 ...

  4. nginx使用ssl模块配置支持HTTPS访问【解决ssl错误】

    默认情况下ssl模块并未被安装,如果要使用该模块则需要在编译nginx时指定–with-http_ssl_module参数. 需求:做一个网站域名为 www.localhost.cn 要求通过http ...

  5. nginx使用ssl模块配置HTTPS支持 <转>

    默认情况下ssl模块并未被安装,如果要使用该模块则需要在编译时指定–with-http_ssl_module参数,安装模块依赖于OpenSSL库和一些引用文件,通常这些文件并不在同一个软件包中.通常这 ...

  6. Nginx基础 - 常用模块配置

    1.Nginx状态监控http_stub_status_module记录Nginx客户端基本访问状态信息 location /mystatus { stub_status on; access_log ...

  7. nginx使用ssl模块配置支持HTTPS访问,腾讯云申请免费证书

    开始我尝试用 let's encrypt 让http 变 https 官方:https://github.com/certbot/certbot 参考:https://www.cnblogs.com/ ...

  8. Linux下安装php环境并且配置Nginx支持php-fpm模块

    因为php安装需要编译,所以服务器应该保证gcc和g++环境的安装 首先释放安装包: tar -xvzf php-5.6.27.tar.gz cd php-5.6.27 接下来进行参数配置,配置前如果 ...

  9. Nginx访问限制模块limit_conn_zone 和limit_req_zone配置使用

    nginx可以通过limit_conn_zone 和limit_req_zone两个组件来对客户端访问目录和文件的访问频率和次数进行限制,另外还可以善用进行服务安全加固,两个模块都能够对客户端访问进行 ...

随机推荐

  1. 493. Reverse Pairs(BST, BIT, MergeSort)

    Given an array nums, we call (i, j) an important reverse pair if i < j and nums[i] > 2*nums[j] ...

  2. Python学习笔记(正则表达式)

    \b - 表示以什么开头或结尾 \d - 匹配数字 \w - 匹配字母或数字或下划线或汉字(我试验下了,发现3.x版本可以匹配汉字,但2.x版本不可以) \s - 匹配任意的空白符 ^ - 匹配字符串 ...

  3. mac搭建TensorFlow环境

    1.首先安装Anaconda,下载地址:https://www.anaconda.com/download/#macos,根据需要下载所需的版本. 2.安装TensorFlow,安装命令:pip in ...

  4. Python-8-print和import进阶

    1.打印多个参数 用逗号隔开: >>> print('Age:', 42) Age: 42 参数之间自动插入了一个空格字符 >>> name = 'Gumby' & ...

  5. rand()函数的用法

    C++中rand() 函数的用法 1.rand()不需要参数,它会返回一个从0到最大随机数的任意整数,最大随机数的大小通常是固定的一个大整数. 2.如果你要产生0~99这100个整数中的一个随机整数, ...

  6. Codeforces Round #566 (Div. 2) B. Plus from Picture

    链接: https://codeforces.com/contest/1182/problem/B 题意: You have a given picture with size w×h. Determ ...

  7. Codeforces #564div2 E1(记忆化搜索)

    虽然不是正解但毕竟自己做出来了还是记下来吧- 对每个人分别dfs得到其期望,某两维的组合情况有限所以Hash一下避免了MLE. #include <cstdio> #include < ...

  8. DotNetAnywhere

    DotNetAnywhere:可供选择的 .NET 运行时   原文 : DotNetAnywhere: An Alternative .NET Runtime作者 : Matt Warren译者 : ...

  9. Kestrel服务器启动并处理Http请求

    从Hosting开始   知识点: 1.Kestrel服务器启动并处理Http请求的过程. 2.Startup的作用. 源码飘香: 总结: asp.net core将web开发拆分为多个独立的组件,大 ...

  10. Windows3

    windows安装后的配置 没有网络适配器, 将USB中的驱动精灵的安装程序安装在win上, 启动精灵, 提示无法连接到网络, 使用Android类型的手机中的QQ浏览器扫码下载 win会有一些开机自 ...