文章目录

  • 1. nginx简介
  • 2.配置文件的结构
  • 3. 提供静态文件
  • 首先,根据host和port找到对应的server 块
  • 然后,根据path找到对应的location
  • 4.设置简单的代理服务器
  • 5. 设置FastCGI代理
  • 什么是FastCGI?
  • 参考文献


1. nginx简介

nginx的主要功能:

  • 负载均衡
  • 反向代理

2.配置文件的结构

分为 simple directives(简单指令) 和 block directives(块指令)。前者为空格隔开的键值对,后者为{}包裹的语句。如果一个块指令可以在括号内包含其他指令,则称为context(上下文)

nginx 中的contextevents,http, server, 和 location

events 和 http是同一级别,属于main contenxt

http包裹server,server 包裹 location

注释使用#,这点与python一样。

# simple directives
listen 9000;
server_name www.baidu.com;

# block directives
event {
......
}
http {
	......
	server {
		......
		location {
			......
		}
	}
}

3. 提供静态文件

通常,配置文件可以包括几个server 块,这些server 块通过它们侦听的port和server_name名称来区分。 一旦nginx决定了哪个服务器处理请求,它就会根据server 块内定义的location指令的参数测试请求头中指定的URI。

http {
	server {
		listen 8080; # 监听8080端口
		server_name www.baidu.com; # 服务器名,也可以是ip地址
		root /home/your/test

		location /images/ {
			root /home/your/project; # 根据此路径在本地找images目录,常为请求的图片文件
		}

		location /static/ {
			root /home/your/project; # 根据此路径在本地找static目录,常为请求的html静态文件。
		}
		
		location / {

		}
	}

	server {
		listen 8081;
		server_name www.baidu.com;
				
		location / {

		}
	}

	server {
		listen 8080;
		server_name www.google.com;
				
		location / {

		}
	}
}

URL的构造通常为protocol://host:port/path?query_params

首先,根据host和port找到对应的server 块

例如:

http://www.baidu.com:8080 会走第一个server 块
http://www.baidu.com:8081 会走第二个server 块
http://www.google.com:8080 会走第三个server 块

然后,根据path找到对应的location

根据请求URL中的host和port找到对应的server 块,进入对应的 server 块 之后,根据请求的URL中的path部分找到对应location 块,

例如:
http://www.baidu.com:8080/staic/index.html 则会进入/static这个location 块中,然后将path和拼接,得到了本地文件路径,/home/your/project/static/index.html

http://www.baidu.com:8080/images/balabala.png 则会进入/images这个location 块中,然后将path和拼接,得到了本地文件路径,/home/your/project/images/balabala.png

其他的对此server 块的请求都会进入/localtion 块。

注意:如果有多个location 块都匹配上了,则会选择匹配中前缀最长的那个location 块。而例子中的/这个location 块只有在其他location 块都没匹配上时,才会匹配。

4.设置简单的代理服务器

server 块 中的root 和 location 块 中的root

若server 块 中的 location 块 没有指明root的值,则当URL匹配到此location 块之后,在查找本地目录文件时,会在server块下的root路径下找,否则就使用 location 块 自己指明的root的值。

server {
    location / {
        proxy_pass http://localhost:8080;
    }

    location /images/ {
        root /data;
    }
    
	location ~ \.(gif|jpg|png)$ {
    	root /data/images;
	}
}

~\.(gif|jpg|png)$是一个正则表达式,它匹配所有以.gif,.jpg或.png结尾的URL。 正则表达式应以 ~ 开头。 将相应的请求将映射到/ data / images目录。

5. 设置FastCGI代理

nginx可用于将请求路由到FastCGI服务器,该服务器运行使用各种框架和编程语言(例如PHP)构建的应用程序。

什么是FastCGI?

待补充

CGI(Common Gateway Interface)全称是“通用网关接口”,是一种让客户端(web浏览器)与Web服务器(nginx等)程序进行通信(数据传输)的协议
CGI可以用任何一种具有标准输入、输出和环境变量的语言编写,如php、perl、tcl等。
不同类型语言写的程序只要符合cgi标准,就能作为一个cgi程序与web服务器交互,早期的cgi大多都是c或c++编写的。
一般说的CGI指的是用各种语言编写的能实现该功能的程序。

FastCGI(Fast Common Gateway Interface)全称是“快速通用网关接口
是通用网关接口(CGI)的增强版本,由CGI发展改进而来,主要用来提高CGI程序性能,
类似于CGI,FastCGI也是一种让交互程序与Web服务器通信的协议
FastCGI致力于减少网页服务器与CGI程序之间互动的开销,从而使服务器可以同时处理更多的网页请求(提高并发访问)。
同样的,一般说的FastCGI指的也是用各种语言编写的能实现该功能的程序。

在设置FastCGI代理的配置时,不再使用proxy_pass这一类的指令,而是使用fastcgi_pass这一类的指令。

fastcgi_pass 用来指明被代理的服务器的host和port。
fastcig_param 用来指明要传递给被代理的服务器的参数。

参考文献

[1] nginx官方文档 [2] 深入理解nginx 推荐有时间的看看这本书,真的很好 [3] 关于CGI和FastCGI的理解 [4] Nginx开发从入门到精通