Nginx include和Nginx指令的使用

2023-07-12,,

Nginx include和Nginx指令的使用

1、nginx include

主配置文件nginx.conf中指定包含其他扩展配置文件,从而简化nginx主配置文件,实现多个站点功能

[root@Web01 conf]# cat nginx.conf

worker_processes 1;

events {

worker_connections 1024;

}

http {

include mime.types;

default_type application/octet-stream;

sendfile on;

keepalive_timeout 65;

include /application/nginx/conf/extra/*.conf;

}

在/application/nginx/conf/extra下创建多个配置文件

批量生成扩展主页配置文件进行测试,编写脚本

mkdir -p /application/nginx/conf/extra        #创建存放每个站点文件的目录

生成的扩展页面在/application/nginx/conf/extra目录下

[root@Web01 conf]# mkdir -p /service/scripts

[root@Web01 conf]# vim /service/scripts/auto.sh

[root@Web01 conf]# cat /service/scripts/auto.sh

#!/bin/bash

for i in `seq 11 20`;

do

echo "server {

listen 80;

server_name ${i}.test.com;

location / {

root html/$i;

index index.html index.htm;

}

}" >>/application/nginx/conf/extra/${i}.conf

mkdir -p /application/nginx/html/$i

echo "$i test">>/application/nginx/html/$i/index.html

done

[root@Web01 conf]#

添加浏览器所在系统本地host解析

[root@Web01 conf]# seq -f %g.test.com 11 20|xargs        #生成hosts解析内容

11.test.com 12.test.com 13.test.com 14.test.com 15.test.com 16.test.com 17.test.com 18.test.com 19.test.com 20.test.com

[root@Web01 conf]#

编辑Windows系统的host解析

浏览器访问,不同域名对应不同页面

 

2、nginx指令使用

2.1. upstream

声明一组可以被proxy_pass和fastcgi_pass引用的服务器;这些服务器可以使用不同的端口,并且也可以使用Unix Socket;也可以为服务器指定不同的权重。如:

upstream web_pool {

server coolinuz.9966.org weight=5;

server 172.23.136.148:8080 max_fails=3 fail_timeout=30s;

server unix:/tmp/backend3;

}

2.2. server

语法:server name [parameters]

其中的name可以是FQDN,主机地址,端口或unix套接字;如果FQDN解析的结果为多个地址,则每个地址都会被用到。

2.3. proxy_pass

语法:proxy_pass URL;

该指令用于指定代理服务器的地址和URL将被映射为的URL或地址和端口。即用来指定后端服务器的地址或URL[端口]。

2.4. proxy_set_header

语法:proxy_set_header header value;

该指令允许重新定义和添加一些将被转移到被代理服务器的请求头部信息。

例如:

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

注意:$proxy_add_x_forwarded_for包含客户端请求头中的"X-Forwarded-For",与$remote_addr用逗号分开,如果没有"X-Forwarded-For" 请求头,则$proxy_add_x_forwarded_for等于$remote_addr

2.5. proxy_read_timeout

语法:proxy_read_timeout time;

这个指令设置Nginx与后端服务器建立连接后。等待后端服务器的响应时间

2.6. proxy_send_timeout

语法:roxy_send_timeout time;

该指令指定请求转移到后端服务器的超时时间。整个传输的要求时间不超过超时时间,但只有两次写操作之间。如果在此时间之后的后端服务器将不采取新的数据,然后nginx将关闭连接。

2.7. proxy_connect_timeout

语法:proxy_connect_timeout time;

该指令用来设置分配到后端服务器的连接超时时间。

2.8. proxy_buffers

语法: proxy_buffers the_number is_size;

该指令设置缓冲区的数目和大小,缺省情况下,一个缓冲区的大小和页面大小相同。

2.9. proxy_buffer_size

语法:proxy_buffer_size buffer_size;

代理缓冲区,该指令用于保存用用户的头部信息。

2.10. proxy_busy_buffers_size

语法:proxy_busy_buffers_size size;

用于当系统负载较大,缓冲区不够用时,可以申请更大的proxy_buffers

2.11. proxy_temp_file_write_size

语法:proxy_temp_file_write_size size;

用于指定缓存临时文件的大小

Nginx include和Nginx指令的使用的相关教程结束。

《Nginx include和Nginx指令的使用.doc》

下载本文的Word格式文档,以方便收藏与打印。