root与alias
location / {
alias /app/html/;
index index.html index.htm;
}
两者区别:
alias 是目录别名,root是最上层目录的定义 alias 后必须用"/"结束,不然找不到文件,root可有可无
反向代理解决跨域
LVS:四层负载均衡,基于tcp ip和端口号 实现负载均衡
Nginx:七层负载均衡,对http协议 实现负载均衡
nginx的优化
基本配置优化
查看CPU核数
cat /proc/cpuinfo| grep "cpu cores"| uniq
worker_processes 4 ;
events{
accept_mutex on;
multi_accept on;
worker_connections 1024;
use epoll;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
反向代理设置keepalive
转发请求 提高效率
upstream backend{
server 192.168.111.101:9001;
server 192.168.111.101:9002;
server 192.168.111.101:9003;
keepalive 300;
}
server {
listen 80;
server_name localhost;
location /{
proxy_pass http://backend;
}
}
压缩
server {
listen 80;
server_name localhost;
gzip on;
gzip_buffers 32 4K;
gzip_comp_level 6;
gzip_min_length 100;
gzip_types application/javascript text/css text/xml application/json;
gzip_disable "MSIE [1-6]\.";
gzip_vary on;
gzip_static on;
location / {
proxy_pass http://127.0.0.1:8080;
}
}
配置文件内容详细介绍:
gzip配置的常用参数
gzip on|off ; #是否开启gzipgzip_buffers 32 4K| 16 8K #缓冲(压缩在内存中缓冲几块? 每块多大?)gzip_comp_level [1-9] #推荐6 压缩级别(级别越高,压的越小,越浪费CPU计算资源)gzip_disable #正则匹配UA 什么样的Uri不进行gzipgzip_min_length 200 # 开始压缩的最小长度(再小就不要压缩了,意义不在)gzip_http_version 1.0|1.1 # 开始压缩的http协议版本(可以不设置,目前几乎全是1.1协议)gzip_proxied # 设置请求者代理服务器,该如何缓存内容gzip_types text/plain application/xml # 对哪些类型的文件用压缩 如txt,xml,html ,cssgzip_vary on|off # 是否传输gzip压缩标志
缓存
proxy_cache_path "./meite_cachedata" levels=1:2 keys_zone=meitecache:256m inactive=1d max_size=1000g;
server {
listen 80;
server_name localhost;
location /details {
proxy_cache meitecache;
proxy_cache_valid 200 206 304 301 302 1d;
proxy_cache_key $request_uri;
add_header X-Cache-Status $upstream_cache_status;
proxy_pass http://127.0.0.1:8080;
index index.html index.htm;
}
}
操作系统优化
vi /etc/sysctl.conf
配置内容
net.ipv4.tcp_syncookies=1
net.core.somaxconn=1024
net.ipv4.tcp_fin_timeout=10
net.ipv4.tcp_tw_reuse=1
net.ipv4.tcp_tw_recycle=0
加载配置!
sysctl -p
|