本站教程收集整理的这篇文章主要介绍了nginx配置详解(二),本站教程本站觉得挺不错的,现在分享给大家,也给大家做个参考。
Nginx配置详解(二)
配置中常用的语句
-
if判断语句 :在LOCATIOn中使用if语句可以实现条件判断,其通常有一个return语句,且一般与有着last或break标记的rewrite规则一同使用@H_197_15@
判断条件: 正则表达式匹配: ~:与指定正则表达式模式匹配时返回“真”,判断匹配与否时区分字符大小写; ~*:与指定正则表达式模式匹配时返回“真”,判断匹配与否时不区分字符大小写; !~:与指定正则表达式模式不匹配时返回“真”,判断匹配与否时区分字符大小写; !~*:与指定正则表达式模式不匹配时返回“真”,判断匹配与否时不区分字符大小写; 文件及目录匹配判断: -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相同;
$scheR_225_11845@e, 所用的协议,比如http或者是https,比如rewrite ^(.+)$ $scheR_225_11845@e://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;进程最大打开文件数
香港vpserror_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;
}
}
}
本站总结
以上是本站教程为你收集整理的nginx配置详解(二)全部内容,希望文章能够帮你解决nginx配置详解(二)所遇到的程序开发问题。
如果觉得本站教程网站内容还不错,欢迎将本站教程推荐给好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。