配置文件结构
在nginx配置文件中包含着多个被内置指令控制的模块。指令又分为指令块(simple directives)和块级指令,指令块包含声明(或功能)和变量,声明(或功能)和变量以空格分隔以分号(;)结尾(ps: listen 80)。块级指令和指令块有相同的结构,块级指令不是分号结尾,并且在{}中包含一组附加说明。如果块级指令中包含其他指令的话,这个块级指令就叫上下文(context);
如果一个指令在其他上下文外的话就被认为在main上下文中。events和http在main的上下文里,server in http, location在server里。
使用#作为注释。
个人理解
在nginx中有一个main的context(一个程序的起点),其他的指令块在他的context里,而所有的块级指令就是除了simple directive的所有指令(ps: server和location)都属于块级指令,声明要用分号隔开,注释用#
处理静态资源
web服务器的一个重要功能就是传输文件(例如:图片和静态html)。这里有一个例子,响应客户端请求返回不同目录下的不同类型的文件。要完成这个需求就要在配置文件中设置http块下的server块中的两处location块。
首先,在/data/www文件夹下创建index.html文件,然后在/data/images/文件夹上传不同的图片。
然后,打开nginx.conf配置文件,nginx.conf默认有些server的例子,大部分被注释掉了。添加如下代码:
http{
server{
}
}
通常,配置文件中包含若干个server块,这些server块监听着不同的端口和域名。一旦nginx接收到一个请求,他就会去找和这个请求中参数匹配的location块。
http{
server{
location / {
root /data/www;
}
}
}
location后的“/”被定义为统一资源标识符的前缀。为了匹配请求的资源,我们需要使用root指令定义资源目录,将资源路径映射到系统文件路径,如果在server块中有多个符合的location块,nginx会选择一个前缀最长的location,也就是说其他的匹配都失败,nginx才会选择这个最短的也就是“/”前缀的location块。当然如果连这个也没写那就是404了。另外,location的前缀是支持正则表达式的。
http{
server{
# 优先级最低,只有其他不符合才使用
location / {
root /data/www;
}
# 优先级高
location /images/ {
root /data;
}
}
}
设置代理服务器(重头戏)
nginx最广泛使用的功能就是作为代理服务器。代理服务器可以接收请求并把它们传递给被代理的服务器,接收被代理服务器返回的响应,再传给客户端。就是类似小学时传纸条,你传给你同桌,同桌再传给你心意的女同学,女同学把写好的纸条再传给你同桌,同桌再给你。
客户端 --->请求 ---> nginx ---> 传递请求 ---> 服务器
服务器 --->响应 ---> nginx ---> 传递响应 ---> 客户端
server {
location / {
proxy_pass http://localhost:8080/;
}
location ~ \.(gif|jpg|png)$ {
root /data/images;
}
}
由于文档上说的很简单我就不翻译了,简单说下就是location里的proxy_pass指令后面的参数就是被代理服务器的地址。客户端的所有请求都先经过nginx再传递给被代理服务器。
proxy_pass指令的所有配置项
nginx配置文件方案
只是随便设置了下,不是最佳方案,小孩子千万别学啊。
# user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 120 100;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 6;
gzip_types text/html text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml;
gzip_vary on;
include /etc/nginx/conf.d/*.conf;
}
/etc/nginx/conf.d/example.conf;
upstream www.demo.com {
ip_hash;
server localhost:7001 weight=1 fail_timeout=2s max_fails=2;
server localhost:7002 weight=1 fail_timeout=2s max_fails=2;
}
server {
listen 80;
server_name www.demo.com;
charset utf-8;
access_log logs/host.access.log main;
location / {
index index.html index.htm;
proxy_pass http://www.demo.com;
proxy_redirect default;
proxy_connect_timeout 2s;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_redirect default;
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
client_body_buffer_size 10m;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
本文深入探讨Nginx配置文件的结构,包括指令块、块级指令和上下文的概念,详细讲解如何通过location块处理静态资源请求,以及如何设置代理服务器进行请求转发,最后提供了一个Nginx配置文件的实例。

被折叠的 条评论
为什么被折叠?



