nginx
静态小文件(1M),支持高并发,同时占用的资源很少,3w并发,10个进程,内存150M
配置简单,灵活,轻量
高并发(静态小文件),静态几万的并发
占用资源小,2W并发,开10个线程服务,内存消耗几百M
支持epoll模型,使得nginx可以支持高并发
nginx的应用场景:
静态服务(图片,视频服务)html,js,css,jpg,gif等
动态服务,nginx+fastcgi的方式运行php,jsp
反向代理,负载均衡,日PV2000w下
nginx虚拟主机:
在配置文件中,一个server标签就是一个虚拟主机
1、基于域名的虚拟主机,通过域名区分主机==>应用:外部网站
2、基于端口的虚拟主机,通过端口来区分虚拟主机==>应用:公司内部网站,网站后台
3、基于IP的虚拟主机,几乎不用
安装nginx
1.安装pcre
nginx日志
cat /var/log/messages
cat /usr/local/nginx/logs/error.log
conf配置文件
html网站目录
logs日志
sbin启动命令
user nobody nobody; worker_processes 8; worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000; worker_rlimit_nofile 65535; error_log /usr/local/nginx/logs/nginx_error.log crit; pid /usr/local/nginx/logs/nginx.pid; events { use epoll; worker_connections 65535; } http { include mime.types; default_type application/octet-stream; charset utf-8; server_names_hash_bucket_size 128; client_header_buffer_size 32k; large_client_header_buffers 4 32k; client_max_body_size 8m; sendfile on; tcp_nopush on; keepalive_timeout 10; server_tokens off; tcp_nodelay on; add_header Server-ID $hostname; fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; fastcgi_buffer_size 256k; fastcgi_buffers 2 256k; fastcgi_busy_buffers_size 256k; fastcgi_temp_file_write_size 256k; fastcgi_intercept_errors on; #add for compress gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.0; gzip_comp_level 6; gzip_types text/plain application/x-javascript text/css application/xml; gzip_vary on; postpone_output 1460; gzip_proxied any; gzip_disable "MSIE [1-6]\."; client_body_buffer_size 512k; proxy_connect_timeout 600; proxy_read_timeout 600; proxy_send_timeout 600; proxy_buffer_size 8k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k; proxy_temp_path /gomeo2o/tmp/proxy_temp_dir; proxy_cache_path /gomeo2o/tmp/proxy_cache_dir levels=1:2 keys_zone=cache_one:9192m inactive=1d max_size=30g; proxy_set_header Host $host; proxy_set_header Proxy-Client-IP $remote_addr; proxy_set_header X-Forwarded-For $http_x_forwarded_for; server { listen 80; server_name localhost; location / { return 200 "<h1>not found</h1>"; expires 30d; } } log_format nginxjson '{"@timestamp":"$time_iso8601",' '"@fields": { ' '"@version":"1",' '"host":"$server_addr",' '"remote_addr":"$remote_addr",' '"http_x_forwarded_for":"$http_x_forwarded_for",' '"request_method":"$request_method",' '"domain":"$host",' '"url.raw":"$uri",' '"url":"$scheme://$http_host$request_uri",' '"status":"$status",' '"server_protocol":"$server_protocol",' '"size":$body_bytes_sent,' '"responsetime":$request_time,' '"http_referer":"$http_referer",' '"upstr_addr": "$upstream_addr",' '"upstr_status": "$upstream_status",' '"ups_resp_time": "$upstream_response_time",' '"x_gomeplus_token":"$http_x_gomeplus_token",' '"x_gomeplus_access_token":"$http_x_gomeplus_access_token",' '"accept":"$http_accept",' '"agent": "$http_user_agent"}}'; access_log /usr/local/nginx/logs/access.log nginxjson; include sites-enabled/*.conf; }
基于域名的虚拟主机
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name www.etiantian.org; location / { root html/www; index index.html index.htm; } } server { listen 80; server_name bbs.etiantian.org; location / { root html/bbs; index index.html index.htm; } } }
基于域名的虚拟主机配置
www.etiantian.org站点目录/html/www
bbs.etiantian.org站点目录/html/bbs
a.配置文件
b.站点目录
mkdir ../html/{www,bbs} -p
echo 'www.etiantian.org' > ../html/www/index.html
echo 'bbs.etiantian.org' > ../html/bbs/index.html
c.重新加载nginx
/usr/local/nginx/sbin/nginx -t 检查语法
/usr/local/nginx/sbin/nginx -s reload 重新加载
d.配置hosts
利用include优化nginx配置文件
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; #nginx vhosts config include extra/www.conf; include extra/bbs.conf; }
cd /usr/local/nginx/conf
mkdir extra
vim bbs.conf
server { listen 80; server_name bbs.etiantian.org; location / { root html/www; index index.html index.htm; } }
虚拟主机别名配置
只需要在server_name后面加上主机名,用空格隔开
server { listen 80; server_name www.etiantian.org etiantian.org; location / { root html/www; index index.html index.htm; } }
一个集群,监控节点,可以给每台主机一个别名
nginx状态
编译安装时加上这个参数--with-http_ssl_module
extra/status.conf
server{ listen 80; server_name status.etiantian.org; location / { stub_status on; access_log off; } }
include extra/status.conf加到http中
Active connections: 2 server accepts handled requests 24 24 14 Reading: 0 Writing: 1 Waiting: 1
Active connections: 2 nginx正处理的活动连接数
accepts:nginx启动到现在共处理了24个连接
handled:共成功创建24次握手请求丢失数=握手数-连接数
requests:共处理了14次请求
Reading:读取到客户端的Header信息数
Writing:返回给客户端的Header信息数
Waiting:已经处理完正在等候下一次请求指令的驻留连接
nginx日志
- 错误日志
常见的有debug|info|notice|warn|error|crit|alert|emerg
一般是warn|error|crit这三个级别之一,注意不要配置info等低级别,会带来磁盘I/O消耗
可以放在main,http,server,location
worker_processes 1; error_log logs/error.log error;#logs/error.log日志位置 error日志级别
- 访问日志
log_format 用来定义记录日志的格式,放置在http下
access_log 用来指定日志文件的路径及使用何种日志格式记录日志
1.
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"';
2.
vim /extra/www.conf,在server外加上access_log logs/access_www.log main;#logs/access_www.log日志位置,main和上面的log_format对应
v
access_log off #这里的off,表示不记录访问日志
输出的日志格式
10.144.52.178 - - [18/Jan/2017:15:17:39 +0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKi
t/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36" "-"
在高并发场景下
访问日志轮询分割
企业应用场景