nginx配置详解(二)

nginx配置详解(二)


配置中常用的语句

  1. if判断语句 :在location中使用if语句可以实现条件判断,其通常有一个return语句,且一般与有着last或break标记的rewrite规则一同使用

     判断条件:
     正则表达式匹配:
     	~:与指定正则表达式模式匹配时返回“真”,判断匹配与否时区分字符大小写;
     	~*:与指定正则表达式模式匹配时返回“真”,判断匹配与否时不区分字符大小写;
     	!~:与指定正则表达式模式不匹配时返回“真”,判断匹配与否时区分字符大小写;
     	!~*:与指定正则表达式模式不匹配时返回“真”,判断匹配与否时不区分字符大小写;
     
     文件及目录匹配判断:
     	-f, !-f:判断指定的路径是否为存在且为文件;
     	-d, !-d:判断指定的路径是否为存在且为目录;
     	-e, !-e:判断指定的路径是否存在,文件或目录均可;
     	-x, !-x:判断指定路径的文件是否存在且可执行;
     实例:
     location / {
     	if ($request_method == “PUT”) {
     		proxy_pass http://test.wyx.com:8080;
     	} 
     
     	if ($request_uri ~ "\.(jpg|gif|jpeg|png)$") {
     		proxy_pass http://image;
     		break;
     	}
     }
     upstream imageservers {
     	server 192.168.0.172:80 weight 2;
     	server 192.168.0.171:80 weight 3;
     }
    

nginx中常用的配置变量

$args, 请求中的参数
$content_length, HTTP请求信息里的”Content-Length”;
$content_type, 请求信息里的”Content-Type”;
$document_root, 针对当前请求的根路径设置值;
$document_uri, 与$uri相同;
$host, 请求信息中的”Host”,如果请求中没有Host行,则等于设置的服务器名;
$limit_rate, 对连接速率的限制;
$request_method, 请求的方法,比如”GET”、”POST”等;
$remote_addr, 客户端地址;
$remote_port, 客户端端口号;
$remote_user, 客户端用户名,认证用;
$request_filename, 当前请求的文件路径名
$request_body_file, ??
$request_uri, 请求的URI,带参数;
$query_string, 与$args相同;
$scheme, 所用的协议,比如http或者是https,比如rewrite  ^(.+)$  $scheme://example.com$1  redirect;
$server_protocol, 请求的协议版本,”HTTP/1.0″或”HTTP/1.1″;
$server_addr, 服务器地址,如果没有用listen指明服务器地址,使用这个变量将发起一次系统调用以取得地址(造成资源浪费);
$server_name, 请求到达的服务器名;
$server_port, 请求到达的服务器端口号;
$uri, 请求的URI,可能和最初的值有不同,比如经过重定向之类的。

生产环境常用的配置

user                              nginx;
worker_processes                  4;
worker_rlimit_nofile              51200;进程最大打开文件数

error_log                         logs/error.log  notice;

pid                               /var/run/nginx.pid;

events {
  use                             epoll;
  worker_connections              51200;worker所处理的最大连接数
}

http {
  server_tokens                   off;
  include                         mime.types;

  proxy_redirect                off;不对重定向的url进行修改
  proxy_set_header              Host $host;
  proxy_set_header              X-Real-IP $remote_addr;
  proxy_set_header              X-Forwarded-For $proxy_add_x_forwarded_for;
  client_max_body_size          20m;请求体最大值
  client_body_buffer_size       256k;分配给请求数据256buffer
  proxy_connect_timeout         90;
  proxy_send_timeout            90;
  proxy_read_timeout            90;
  proxy_buffer_size             128k;
  proxy_buffers                 4 64k;
  proxy_busy_buffers_size       128k;
  proxy_temp_file_write_size    128k;

  default_type                    application/octet-stream;
  charset                         utf-8;
  # 为存储承载客户端请求正文的临时文件定义存储目录
  client_body_temp_path           /var/tmp/client_body_temp 1 2;
  proxy_temp_path                 /var/tmp/proxy_temp 1 2;
  fastcgi_temp_path               /var/tmp/fastcgi_temp 1 2;
  uwsgi_temp_path                 /var/tmp/uwsgi_temp 1 2;
  scgi_temp_path                  /var/tmp/scgi_temp 1 2;

  ignore_invalid_headers          on;忽略不合法的HTTP头部语法
  server_names_hash_max_size      256;服务器hash表的大小
  server_names_hash_bucket_size   64;服务器hash表的大小
  client_header_buffer_size       1k;用于处理客户端 header 大小,使用默认的 1K 便可
  # 显示大客户端 header 的最大值和大小,4 header 和 4k buffer 应该够用
  large_client_header_buffers     4 4k;
  connection_pool_size            256;连接池大小
  request_pool_size               64k;请求连接池大小

  output_buffers                  2 128k; 设置从磁盘读取缓冲区响应的数量和大小
  postpone_output                 1460;

  client_header_timeout           1m; 用于设置发送主体的超时时间
  client_body_timeout             3m; 用于配置客户端 header 请求之后的发送时间
  send_timeout                    3m; 用于设定客户端的超时响应时间


  log_format main                 '$server_addr $remote_addr [$time_local] $msec+$connection '
                                  '"$request" $status $connection $request_time $body_bytes_sent "$http_referer" '
                                  '"$http_user_agent" "$http_x_forwarded_for"';

  open_log_file_cache               max=1000 inactive=20s min_uses=1 valid=1m;

  access_log                      logs/access.log      main;
  log_not_found                   on;


  sendfile                        on;
  tcp_nodelay                     on;允许 Nginx 将多个 buffer 作为单独数据包发送
  tcp_nopush                      off;用于激活 TCP 栈的 TCP_CORK 选项

  reset_timedout_connection       on;
  keepalive_timeout               10 5;
  keepalive_requests              100;

  # gzip压缩配置
  gzip                            on;
  gzip_http_version               1.1;
  gzip_vary                       on;
  gzip_proxied                    any;
  gzip_min_length                 1024;
  gzip_comp_level                 6;
  gzip_buffers                    16 8k;
  gzip_proxied                    expired no-cache no-store private auth no_last_modified no_etag;
  gzip_types                      text/plain application/x-javascript text/css application/xml application/json;
  gzip_disable                    "MSIE [1-6]\.(?!.*SV1)";


  upstream tomcat8080 {
    ip_hash;

    server                        192.168.0.172:8080 weight=1 max_fails=2;
    server                        192.168.0.173:8080 weight=1 max_fails=2;
    server                        192.168.0.171:8080 weight=1 max_fails=2;
  }

  server {
    listen                        80;
    server_name                   www.wyx.com;
    # config_apps_begin
    root                          /data/webapps/htdocs;
    access_log                    /var/logs/webapp.access.log     main;
    error_log                     /var/logs/webapp.error.log      notice;

    location / {
    
      location ~* ^.*/favicon.ico$ {
        root                      /data/webapps;
        expires                   180d;
        break;
      }
    
      if ( !-f $request_filename ) {
        proxy_pass                http://tomcat:8080;
        break;
      }
    }

    error_page                    500 502 503 504  /50x.html;
      location = /50x.html {
      root                        html;
    }
  }

  server {
    listen                        8081;
    server_name                   nginx_status;

      location / {
          access_log                  off;
          deny                        all;
          return                      503;
      }

      location /status {
          stub_status                 on;
          access_log                  off;
          allow                       127.0.0.1;
          allow                       192.168.0.172;
          deny                        all;
      }
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值