Nginx指令配置概述

2019-06-14  本文已影响0人  鸿雁长飞光不度

1. nginx配置文件组成

2.配置文件案例

user nginx; #设置所有可以运行nginx的用户为nginx,不限制可以注释或者设置为nobody
worker_processes 8;#nginx工作进程数,也可以设置为auto
worker_rlimit_nofile 1000000;
#worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000
worker_cpu_affinity 0001 0010 0100 1000 0001 0010 0100 1000;

error_log /var/log/nginx/error.log; #错误日志存放的路径
pid /var/run/nginx.pid; #nginx进程id存放的文件

#加载nginx模块的
include /usr/share/nginx/modules/*.conf;
#events块
events {
    use epoll;#所选择的事件驱动模型`select,poll,kqueue,epoll等等`
    worker_connections  8096;#每一个worker process同时开启的最大连接数,不仅仅包括和前端用户的连接,而是包含所有可能的链接。
    accept_mutex on; #开启的时候对多个nginx进程连接序列化,防止多个进程对连接争抢,解决`惊群现象`。默认是on
    multi_accept on; #允许同时接收多个网络连接,默认是off
}

http {
   #定义日志文件的格式,只能出现在http块
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    log_format  fail2ban  '$remote_addr - $remote_user [$time_local] "$request"';
    #访问日志
    access_log  /var/log/nginx/access.log  main  buffer=512k;

    client_header_buffer_size 8k;
    client_header_timeout 10;
    client_body_buffer_size 256k;
    client_body_timeout 10;
    large_client_header_buffers 4 8k;
    client_max_body_size 20m;
    send_timeout 10;
    #用户建立连接后,nginx服务器保持连接打开的时间,
    # keepalive timeout [header_timeout]服务器端保持的
    #服务器端保持连接的时间设置为120s,发送给用户端应答报文头部的超时时间为100秒
    #keepalive_timeout 120s 100s;
    #keepalive_timeout 30;
    # 通过某一个连接向nginx服务器发送请求的次数
    keepalive_requests 5000;
    reset_timedout_connection on;

    proxy_connect_timeout    60;
    proxy_read_timeout       600;
    proxy_send_timeout       600;

    proxy_buffering off;
    proxy_buffer_size 128k;
    proxy_buffers 64 128k;
    proxy_busy_buffers_size 256k;
    proxy_temp_file_write_size 256k;
    proxy_max_temp_file_size 128m;

    #proxy_cache_path /home/nginx_cache levels=1:2 keys_zone=nginx_cache:128m max_size=2g inactive=5m;
    #proxy_cache_key "$scheme$request_method$host$request_uri$is_args$args";
    #proxy_cache_valid 200 302 5m;
    #proxy_cache_valid 404 1m;

    fastcgi_connect_timeout 15;
    fastcgi_send_timeout 60;
    fastcgi_read_timeout 60;
    fastcgi_buffer_size 64k;
    fastcgi_buffers 32 64k;
    fastcgi_busy_buffers_size 256k;
    fastcgi_temp_file_write_size 512k;
   ### 配置允许用sendfile方式传输文件
    sendfile            on; 
    #sendfile_max_chunk_size 10000; #每个worker process每次调用sendfile传输最大不能超过这个值,默认是0表示无限制,可以在server、http、location模块配置。
  
  tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    server_tokens off;
    #定义MIME-Type,用于nginx根据mime-type识别资源。
    include             /etc/nginx/mime.types;
    #配置用于处理前端请求的mime-type,此指令可以在http,localtion,server块里面配置
    default_type        application/octet-stream;

    gzip on;
    gzip_min_length  1k;
    gzip_buffers     4 16k;
    gzip_http_version 1.1;
    gzip_comp_level 5;
    gzip_vary on;
    gzip_disable "MSIE [1-6]\.";
 
    
    limit_req_zone $binary_remote_addr  zone=allips:10m rate=10r/s;
    limit_conn_zone $binary_remote_addr zone=addr:10m;
    
    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # limit_req_zone $binary_remote_addr.$cookie_PHPSESSID zone=addr_session:50m rate=15r/m;
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf; #引入其他配置文件
}
server_name 支持基于IP的虚拟主机配置
linux支持IP别名增加,
ifconfg eth1 xxx.xxx.xxx.xxx netmask 255.255.255.0 up

3. 常见配置块

3.1location块的配置

语法结构

location [= | ~|~*|^~] uri {}

nginx对uri里面编码的字符串会兼容处理,%20表示空格,直接写空格会匹配到。

3.2配置请求根目录:root

root path;#path指令可以设置除了$document_root,realpath_root外的大多数预设服务器变量。

root可以在http块、server块、location块配置。

#当收到/data/index.html的时候,会在/localtiontest1/data下找到index.html响应请求。
location /data/
{
    root /localtiontest1;
}

3.3更改location的URI

alias path; path是修改后的根路径,变量取值和root命令的path参数取值相同。

location ~ ^/data/(.+\.(html|htm))$
{
    alias /locationtest1/other/$1;
}

当location收到/data/index.html请求时,服务器从/locationtest1/other下寻找index.html并响应请求。已经不在/data目录下寻找,该指令和root能起到相同的效果

3.4配置网站的首页

location ~ ^/data/(.+)/web/ $
{
   index index.$1.html index.html;
}

3.5设置错误界面

error_page code ...[=[response]] uri

 error_page 410=301 /empty.gif

error_page之后重定向

#不用网站根目录下的404.html,用/myserver/errorpages/404.html
location /404.html 
{
    root /myserver/errorpages/
}

3.6基于IP配置nginx的访问权限

nginx_http_access_module模块提供控制。

格式:allow address |CIDR | all;

address:表示允许访问的客户端IP,只支持单个IP
CIDR: 通过子网掩码的形式确定的地址,比如 202.80.18.23/25 ,前面25位表示网络地址,后的表示主机地址
all: 表示全部允许
deny address | CIDR | all

这两个指令可以在http块、server块、location块中配置。解析从上到下解析,遇到匹配就不在继续匹配。

3.7基于密码配置nginx访问权限

auth_basic string | off; #string表示开启,off表示关闭
auth_basic_user_file file; #用于设置包含用户名和密码信息文件路径,
密码文件支持明文或者密码加密的文件

name1:password1:comment

采用加密的密码可以用linux指令

htpasswd -c -d /nginx/conf/pass_file username 
上一篇 下一篇

猜你喜欢

热点阅读