Linux----------nginx详细配置文件

[toc] 此文章借鉴(小马哥 Maximilian Kalbfell)html

1、nginx配置文件结构(一)

2、nginx配置文件结构(二)

nginx配置文件分为5个区块:node

  • 事件区块:全局设置,部分设置得指令将影响其它全部部分得设置。
  • HTTP区块:http的全局设置。
  • server区块:server部分的指令主要用于指定虚拟主机域名、IP和端口
  • location区块:location部分用于匹配网页位置
  • upstream区块:upstream的指令用于设置一系列的后端服务器,设置反向代理及后端服务器的负载均衡

3、事件区块

4、http区块

http {
include mime.types;                                                                                                                                             #设定mime类型,类型由mime.type文件定义
default_type application/octet-stream;                       
access_log /usr/local/nginx/log/nginx/access.log;                                                                                               #设定日志格式
sendfile on;                                                                                                                                                          #指令指定 nginx 是否调用 sendfile 函数
#autoindex on;                                                                                                                                                     #开启目录列表访问,合适下载服务器,默认关闭。
tcp_nopush on;                                                                                                                                                    ##防止网络阻塞
keepalive_timeout 60;                                                                                                                                          ##keepalive超时时间
tcp_nodelay on;                                                                                                                                                   ##提升数据的实时响应性
gzip on;                                                                                                                                                                ##开启gzip压缩
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;                                                                                                                                              ##压缩级别大小,最大为9,值越小,CPU处理更快,值越大,消耗CPU比较高。
client_max_body_size 10m;                                                                                                                                ##容许客户端请求的最大单文件字节数
client_body_buffer_size 128k;                                                                                                                             ##缓冲区代理缓冲用户端请求的最大字节数,
proxy_connect_timeout 90;                                                                                                                                 ## #nginx跟后端服务器链接超时时间(代理链接超时)
proxy_send_timeout 90;                                                                                                                                      ##后端服务器数据回传时间(代理发送超时)
proxy_read_timeout 90;                                                                                                                                       ###链接成功后,后端服务器响应时间(代理接收超时)
proxy_buffer_size 4k;                                                                                                                                          ####设置代理服务器(nginx)保存用户头信息的缓冲区大小
proxy_buffers 4 32k;                                                                                                                                            ### #proxy_buffers缓冲区,网页平均在32k如下的话,这样设置
proxy_busy_buffers_size 64k;                                                                                                                             ####高负荷下缓冲大小(proxy_buffers*2)
open_file_cache max=102400 inactive=20s;                                                                                                      ###这个将为打开文件指定缓存,默认是没有启用的
open_file_cache_valid 30s;                                                                                                                                 ####这个是指多长时间检查一次缓存的有效信息。
open_file_cache_min_uses 1;                                                                                                                             ###inactive参数时间内文件的最少使用次数
include vhosts.conf;                                                                                                                                             ###包含其它配置文件,如自定义的虚拟主机
}

5、upstream区块

语法:upstream name {.......} name是自定义的一个名字,而{}则是须要定义的内容,只能在http块定义,不能在server块里定义。定义完以后可在location块 下写入以下代码进行调用:http://name 4种负载均衡算法:nginx

  • 1 轮询算法(默认): 每一个请求按时间顺序逐一分配到不一样的后端服务器,若是后端服务器down掉,能自动剔除。 upstream name { server 192.168.1.1:80 ; server 192.168.1.2:81 ; }算法

  • 2 ip_hash算法:每一个请求按访问IP的hash结果分配,同一个IP客户端固定访问一个后端服务器; upstream name { ip_hash; //添加参数支持哈希 server 192.168.1.1:80 ; server 192.168.1.2 :81 ; }后端

  • 3 权重算法:指定轮询概率,weight和访问比率成正比。用于后端服务器性能不均的状况 upstream name { server 192.168.1.1:80 weight=1; server 192.168.1.2:81 weight=9; }缓存

  • 4 最少链接(least_conn):把请求转发给链接数较少的后端服务器进行处理 upstream name { least_conn; server 192.168.1.1:80 weight=1; server 192.168.1.2:81 weight=1; }服务器

6、location区块

nginx反向代理介绍 反向代理:以代理服务器来接受链接请求,而后将请求转发给内部网络上的服务器(真实服务器);并将获得的结果返回给请求方。(此时代理服务器对外就表现为一个独立的服务器)网络

语法:
1.直接proxy_pass 详细地址
location /
           {
           proxy_pass http://192.168.1.1:80;
           }

2.结合负载均衡upstream
upstream name {192.168.1.1:80}
           location /
                        {
                        proxy_pass http://name;
                        }


##反向代理实例:
真实内容服务器nginx配置文件以下:
location / {
             add_header X-Frame-Options DENY;
             root /opt/test/;
             proxy_set_header X-Client-Really-IP $remote_addr;
             index index.html index.htm;
    }
代理服务器nginx配置文件以下:
location / {
          proxy_pass http://10.0.1.7:80;
          root html;
          index index.html index.htm;
}

6.1 rewrite

相关文章
相关标签/搜索